-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Return type of class decorator is ignored #16241
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
Comments
Duplicate of #3135? |
@JelleZijlstra Not exactly. If I write it the other way m.py:12: error: Cannot assign to a type [misc]
m.py:12: error: Incompatible types in assignment (expression has type "type[T]", variable has type "type[X]") [assignment]
m.py:15: error: "X" has no attribute "y" [attr-defined] |
I can see that the decorator is rightly skipping these checks, but the decorator's return type seems to be ignored. I think it should be assigned to the name it decorates (in this case, In an ideal world, I would be able to annotate the decorator: def dataclass[T](cls: type[T]) -> type[T] & type[DataclassInstance]: ... |
This is covered by #11117. |
Those errors look correct. After you define a class, mypy won't let you reassign its name.
That's #3135, right? In any case I don't think mypy is misinterpreting your code. If you annotate your decorator with If your request is that mypy infers an intersection type in this particular case, that seems out of scope until intersections are actually part of the type system. |
Yeah, both of these checks do make sense. No argument there.
Yes, sorry I didn't see this comment, which covers my case.
It depends on what you mean by "dataclass-like semantics". My problem is that
Yes, I know. I'm just pointing that out so that we don't debate about the lost type information (
Yes, exactly right, thanks. In any case, I guess we can close this as a duplicate of #3135. Seems to me that fixing that should fix the two examples I gave above? |
MyPy seems to ignore the return type of class decorators. @AlexWaygood 's suggested I create a minimal repro of this problem:
This makes it impossible to define a dataclass transform that produces ordinary dataclasses:
The text was updated successfully, but these errors were encountered: