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
Inconsistency when overriding dataclass_transform
in subclasses
#6489
Comments
dataclass_transform
in subclassesdataclass_transform
in subclasses
What you're doing in the first sample is highly suspect because it combines a metaclass-based and class-based dataclass implementation. Only one of these techniques would ever be used for a given class. If both were actually used in practice, it's not clear which implementation (the one in the metaclass or the one in the base class) would apply. I'm curious why you're doing this. I'm guessing you're trying to work around some other limitation. Pyright is the reference implementation for PEP 681, and it prefers the metaclass over the base class when both techniques are combined. I think that's a defensible behavior. In general, when a PEP doesn't describe a behavior, type checkers should fall back on the reference implementation behavior unless there's good reason for them not to, so mypy should probably be changed to match pyright's behavior in this case. Then again, I think this is enough of an edge case that it probably doesn't matter. I recommend against relying on this behavior. |
That's a good point, makes sense. The use case is related to this PR: pydantic/pydantic#8163, where I may have to take a look at the current |
Ah, I see. That's kind of hacky, but I understand the reasoning behind it. I can't think of a better solution than the one you've come up with. Since it's highly unlikely that anyone else is relying on the current behavior in either mypy or pyright, I'm willing to make the proposed change in pyright. |
Thanks! but pydantic recently introduced changes in the |
OK, sounds good. I'm going to close this for now, but I'm willing to re-open it in the future based on additional input. |
…e metaclass and base class of a class. Previously, the metaclass was preferred. now the base class is preferred. This allows the metaclass defaults to be overridden by a child (base) class. This addresses #6489.
The two following snippets produce different results:
Code sample in pyright playground
Code sample in pyright playground
Unfortunately, the PEP does not describe the expected behavior when overriding
dataclass_transform
in subclasses. But I think it would be great for the first snippet to behave the same way as the second one.Tested with mypy, it is behaving as expected (first snippet doesn't produce an error).
The text was updated successfully, but these errors were encountered: