Replies: 2 comments 3 replies
-
noting that my preference is that we'd be able to ignore them, which allows for some additional configurational options and potentially better support for legacy code. but if it should be a TypeError, so be it. |
Beta Was this translation helpful? Give feedback.
-
I had to do some historical digging to understand this behavior. The dataclass docs claim that, "A field is defined as a class variable that has a type annotation." However, the error in the "field() with no annotation" scenario was added under the assumption that the user intended the variable to be a field and accidentally left off the annotation or didn't understand that annotations were required. The discussion about this started in https://bugs.python.org/issue32428 and then moved to https://bugs.python.org/issue32505. Since this case is not explicitly called out in PEP 681, this would currently fall in the bucket of "except where stated otherwise in this PEP, classes impacted by However, if we feel that other libraries would also benefit from not erroring in this scenario, we could consider never erroring in this case for |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If I make a dataclass like this, it fails with
TypeError: 'y' is a field but has no type annotation
:What is pep-681's behavior for custom, dataclass-like systems which may (depending on how this discussion goes) ignore such fields from a dataclass perspective, rather than raise
TypeError
? right now, pyright seems to do the same, that is, it ignores fields on the class that don't have annotations, does not consider them as part of the init constructor, but does not report them as typing errors.that is, this passes, given a
@dataclass_transform
class withmapped_column()
as a field descriptor:above, "user_id" is not part of the constructor for Address but no error is reported.
Beta Was this translation helpful? Give feedback.
All reactions