Dear all,
I received plenty of interesting comments regarding my previous question, so that I indulge myself in another one.
In file a.py
, you import TypeAlias
from module typing
from typing import TypeAlias
In file b.py
, you import all of module a
and use TypeAlias
from a import *
t:TypeAlias = int
Then Pyright
raises an error:
error: Expression of type "Type[int]" cannot be assigned to declared type "TypeAlias"
"Type[type]" is incompatible with "Type[TypeAlias]"
However, if I import TypeAlias direcly in module b, it works!
# from a import *
from typing import TypeAlias
t:TypeAlias = int
It also works if I use the extended name of TypeAlias
, that is, typing.TypeAlias
# from a import *
import typing
t:typing.TypeAlias = int
In the typing.py
source code, it is mentioned that TypeAlias
is not a type-as-usual, but kind of a “Special marker”. It is emphasized that its use is strictly restricted to the form above, but nothing is specified about importation.
The official doc refers to PEP613, but I am not sure to understand all of it. At least, word “import” does not appear in the document.
Could anybody explain how it should be used properly?
Best regards,
Luc.