You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test.py:6: error: Variable "test.ABC.ABCInputType" is not valid as a type
test.py:6: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)
The use of type aliases in classes seems both like a pretty legitimate use case to me (consider for example the ability to have an input type for the class as an attribute of the class as above) and also seems consistent with both the typing documentation and mypy's documentation, neither of which seem to mention that type aliases can only appear at the top level (only that they need to be plain assignments, as they are in the above code).
Yeah that doc section you linked is incomplete. The PR that added it closed the relevant issue but didn't address all points, in particular the special case about plain names at class scope creating variables.
What causes List[Any] to work there but List to fail? Shouldn't those be identical? And is there any equivalent of this fix that works for the more generic case where I just want some alias?
Yeah that doc section you linked is incomplete. The PR that added it closed the relevant issue but didn't address all points, in particular the special case about plain names at class scope creating variables.
I did recall that when doing the docs PR we did mention about the alias should appear at top level but somehow I must have forgotten to include that in the final version. I'll file a PR to fix that.
The problem as stated in the issue above is solved now with PEP 613
Thanks! I'm closing this now since the original issue is now solved for most cases. Please feel free to open a new issue for the dataclasses- specific issue.
Activity
ilevkivskyi commentedon Aug 3, 2020
Yeah that doc section you linked is incomplete. The PR that added it closed the relevant issue but didn't address all points, in particular the special case about plain names at class scope creating variables.
cc @TH3CHARLie
Btw the fix is simply to use:
evhub commentedon Aug 3, 2020
What causes
List[Any]to work there butListto fail? Shouldn't those be identical? And is there any equivalent of this fix that works for the more generic case where I just want some alias?ilevkivskyi commentedon Aug 3, 2020
A trick that should work if you want to force type alias for plain name at class scope is use a single element union.
TH3CHARLie commentedon Aug 3, 2020
I did recall that when doing the docs PR we did mention about the alias should appear at top level but somehow I must have forgotten to include that in the final version. I'll file a PR to fix that.
tmke8 commentedon Apr 5, 2022
The problem as stated in the issue above is solved now with PEP 613:
-> no errors
However, this does not work in dataclasses (or any dataclass-like class), where you have to use
ClassVarto exclude the variable from being usedThere's no error in mypy 0.942, but it thinks the type of
xisAny.AlexWaygood commentedon Apr 5, 2022
Thanks! I'm closing this now since the original issue is now solved for most cases. Please feel free to open a new issue for the dataclasses- specific issue.