Replies: 1 comment 1 reply
-
When you refer to a generic type alias in your code, you need to provide type arguments for it. If you don't provide type arguments, type checkers must (according to the typing spec) assume that you intended the type arguments to be In your code sample, you've defined the type alias When a TypeVar is allocated as a global variable, it has no meaning until it is bound to a scope. A scope can be a generic class, a generic function, or a generic type alias. In your code sample, you're binding some of these type variable to multiple different scopes. For example, If you're using Python 3.12, you may want to switch to the new PEP 695 syntax for defining generics. It eliminates much of this confusion and will feel much more natural if you're used to programming with generics in other languages. Mypy recently added experimental support for this new syntax. |
Beta Was this translation helpful? Give feedback.
-
I'm working on a minimal example (in Py3.8) of a decorator with these qualities:
mypy --strict
I finally came up with this (I'm new to Python type hinting, needed a little help from STerliakov on SO to get me across the finish line):
This works fine, exactly what I need. It would be a little more readable if the repeeated, wordy
Callable[]
expressions could be replaced with aliases; note the locations marked with# *Type aliases in generics break mypy
in the listing.The above uses the generic class internals support in
mypy
, of course. With the commented-out type aliases, I'm trying to use the generic type aliases support, but get the following errors. What am I doing wrong?Program run:
mypy --strict
run:Beta Was this translation helpful? Give feedback.
All reactions