-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
is_dataclass() returns True for non-dataclass subclass of dataclass #119260
Comments
I think the current behavior makes sense. A subclass of a dataclass still possesses all the characteristics of the dataclass even if the subclass is not decorated with |
I agree with @blhsing -- I think the current behavior is probably better. This behavior should be explicitly tested and documented, though; currently it isn't. |
Agreed with @carljm, especially the part about adding tests and docs. I'm surprised there's no test for this. |
I was able to declare additional fields in subclasses.
|
Your subclass also has the |
My bad, corrected the example in the comment above 😃. Also I have a PR on the way ! |
No, you were able to add a normal Python class attribute to a subclass, which is sufficient to fool your test because of Python's normal behavior where instance attribute access falls back to the class. The class attribute you added in the subclass is not a dataclass field: >>> Z.__dataclass_fields__.keys()
dict_keys(['y'])
>>> Z(y=1, z=2)
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
Z(y=1, z=2)
~^^^^^^^^^^
TypeError: X.__init__() got an unexpected keyword argument 'z' |
…ion and Tests (#119480) Co-authored-by: Carl Meyer <[email protected]>
…mentation and Tests (pythonGH-119480) (cherry picked from commit bf4ff3a) Co-authored-by: Aditya Borikar <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
…mentation and Tests (pythonGH-119480) (cherry picked from commit bf4ff3a) Co-authored-by: Aditya Borikar <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
…umentation and Tests (GH-119480) (#119760) gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests (GH-119480) (cherry picked from commit bf4ff3a) Co-authored-by: Aditya Borikar <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
…umentation and Tests (GH-119480) (#119761) gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests (GH-119480) (cherry picked from commit bf4ff3a) Co-authored-by: Aditya Borikar <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
Thanks @adiaholic for the PR! |
…mentation and Tests (python#119480) Co-authored-by: Carl Meyer <[email protected]>
…mentation and Tests (python#119480) Co-authored-by: Carl Meyer <[email protected]>
Bug report
Bug description:
If a dataclass has a subclass that is not itself a dataclass,
is_dataclass()
returns True on the subclass and its instances:Documentation of is_dataclass() for reference: https://docs.python.org/3.13/library/dataclasses.html#dataclasses.is_dataclass
Intuitively this seems wrong: Z is not itself a dataclass. In pyanalyze I wrote a replacement for
is_dataclass()
because I needed to check whether the exact class was a dataclass:Changing the CPython behavior might be impossible for compatibility reasons, but in that case, we should document and test this edge case.
cc @ericvsmith @carljm for dataclasses.
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
The text was updated successfully, but these errors were encountered: