New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dataclasses: make it an error to have initialized non-fields in a dataclass #76609
Comments
For this class: @dataclass
class C:
x: int = 0
y = 0 Generate a TypeError. 'y' is not a field (as defined by @DataClass). It should either be a regular field (by giving it a type annotation) or a ClassVar field. |
A possible question here is should we give an error for any non-callable name in After some thinking I am actually leaning towards the first option. |
I'm not sure I understand the distinction. You have to look through everything in The trick is what else to exclude. In this class: class C:
x: int = 0
y = 0
def func(self): pass
@staticmethod
def staticmeth() : pass
@classmethod
def classmeth(cls) : pass
@property
def prop(self): pass These are the non-callables: print([k for k in C.__dict__ if not callable(getattr(C, k))]) ['__module__', '__annotations__', 'x', 'y', 'prop', '__dict__', '__weakref__', '__doc__'] How do we only pick out |
Initially I thought about only flagging code like this: @dataclass
class C:
x = field() But not this: @dataclass
class C:
x = 42 Now I think we should probably flag both as errors.
We had a similar problem while developing Protocol class (PEP-544). Currently we just a have a whitelist of names that are skipped: '__abstractmethods__', '__annotations__', '__weakref__', '__dict__', (plus some internal typing API names) |
I liked the original design better, where things without annotations would On Dec 27, 2017 5:19 PM, "Ivan Levkivskyi" <report@bugs.python.org> wrote:
|
With the original proposal the ignored variables without annotations will behave as class variables. This "conflicts" with PEP-526 which requires class variables to be annotated with ClassVar[...]. On the other hand some people may be unhappy that they need to import |
Just to clarify the previous comment, I still think that flagging this @dataclass
class C:
x = field() is important, since simply ignoring a The previous comment is about @dataclass
class C:
x = 42 |
There is no real conflict with PEP-526 though. PEP-526 introduces ClassVar so the type checker can be made to understand. PEP-557 allows omitting ClassVar in case you don't care about type checkers. So I think we should stick with the current spec of PEP-557 (which lets you omit ClassVar), except for this special case:
Agreed. That's a special case and I'm fine with flagging it as an error. |
OK. |
I'm closing this, and will open another issue to raise an error for: @dataclass
class C:
x = field() |
I think we also need a separate issue for not overriding __repr__ etc, if '__repr__' in cls.__dict__. |
Correct. I'm working on that one now. I'll open it tonight or tomorrow. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: