Closed michael-123123 closed 2 hours ago
It's not clear what you're trying to accomplish.
__dataclass_fields__
is private. Use dataclasses.fields()
and you'll see it's not a normal field. You've not initialized the class field to anything.
This is the normal way to initialize a ClassVar.
>>> @dataclass
... class B:
... sentinel: ClassVar = 3
Hi @ericvsmith. Thanks for the quick reply.
I realize that.
Let me clarify that I wanted to have a class variable that is of type Field
.
The point I was making with the bug report is that if you want a ClassVar
of type Field
--
then it is neither a class var nor an attribute of the instance.
This may be some kind of undefined/undocumented behavior.
I would expect this to just be a regular class variable of type Field (like in class A
in my example)
Alternatively - I would expect some error message that I have an uninstantiated field - similar to non class var fields that are not initialized.
Actually I am closing this bug -- this behavior is documented. The workaround (in case anyone is interested) is:
@dataclass
class B:
x: ClassVar[Field] = field(default=field())
Bug report
Bug description:
Hi,
I searched issues and couldn't find anything similar to this. I'm not 100% sure it's a bug - possibly this is desired behavior?
To reproduce:
Output:
Upon inspection of
B.__dataclass_fields__
thesentinel
field does appear inB
. Which is somewhat confusing behavior.CPython versions tested on:
3.13
Operating systems tested on:
Linux