gh-144618: Don't track ClassVar dataclass members as defaults#144619
gh-144618: Don't track ClassVar dataclass members as defaults#144619johnslavik wants to merge 8 commits intopython:mainfrom
ClassVar dataclass members as defaults#144619Conversation
sobolevn
left a comment
There was a problem hiding this comment.
What do you think: which parts of 3rd party libs this can potentially affect? Like pydantic validation / etc.
|
I've asked the pydantic devs to comment. |
|
This will subtly change Pydantic by forcing classvar defaults to be I'd tried to run Pydantic test suite against my change but haven't gotten the time to get it to work and closely verify it. The issue and the change are related to GH-88609. |
|
The Pydantic test suite passes with this change, although the descriptor's However, I feel like changing the defaults of class variables is going to cause issues for other users. As of now, the result of from dataclasses import dataclass
from typing import ClassVar
class Kaboom:
def __get__(self, inst, owner):
return 1
@dataclass
class Foo:
kaboom: ClassVar[Kaboom] = Kaboom()
# On main:
Foo.__dataclass_fields__['kaboom'].default
#> 1
# With this PR:
#> MISSINGWhat about keeping the existing behavior, and if |
|
Thanks a lot for your chiming in @Viicos!
It seems unclear whether we would catch
How strongly? On balance, I'm OK with closing this as too risky. This is an edge case. |
Ah so given that there's no public way to get the field info for class var pseudo-fields, this might be safe enough then! |
|
Hm, looking at the code, this PR also affects a case where from dataclasses import dataclass, field
class F:
def __get__(self, instance, owner):
return field(default=1)
@dataclass
class C:
x: int = F()
print(C())This used to work and I'm not sure this is something we want to support, but I'll fix it anyway. |
I haven't added code for cleaning up
ClassVar[Field] = dataclasses.field(default=MISSING)from class members, because this is an incorrect use of the API.Perhaps it should even fail, but that's out of scope of the issue.
__get__on unusedClassVarvalues #144618