Replies: 3 comments 2 replies
-
Fun, your snippet of code above caused me to stumble upon a mypy crash. |
Beta Was this translation helpful? Give feedback.
-
In answer to your question: I see no reason to explicitly disallow |
Beta Was this translation helpful? Give feedback.
-
On a similar note, I'm writing a typing parsing & manipulation library. The natural type for my function input/output would be def parse_type_hints(type_hints: Dict[str, TypeAlias]):
...
parse_type_hints(typing.get_type_hints(cls)) But TypeError: Plain typing.TypeAlias is not valid as type argument |
Beta Was this translation helpful? Give feedback.
-
There was previously a discussion on whether
TypeAlias
es should be allowed within classes. The consensus was to allow this, and indeed the following works fine with mypy and pyright (I haven't checked other type checkers):However, this doesn't work in dataclasses, because the dataclass thinks the type alias is a field (because it has a type annotation that is not
ClassVar
).So, an obvious idea would be to wrap
TypeAlias
inClassVar
:However, this currently fails (python 3.10):
I did also open an issue on bugs.python.org in order to support
TypeAlias
directly in dataclasses (i.e., making@dataclass
ignore fields with aTypeAlias
annotation):But there was no enthusiasm for this idea.
So, are
TypeAlias
es just not possible in dataclasses? (By the way, leaving out the annotation entirely to writeShortName = LongEnumName
doesn't work in mypy because mypy doesn't recognize it as a type alias, but it does seem to work in pyright.)Beta Was this translation helpful? Give feedback.
All reactions