-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add support for TypeAliasType #16614
Comments
|
Thank you for the information! |
Do we really want to support this? I thought that I don't think that we need to promote the usage OP is proposing. |
It’s useful same way using typing extensions helps. If you are on 3.11 or lower then you can’t use type syntax, but it would be nice to use new type aliases especially as they have explicit type variables and there are some things that are only possible with new syntax. type First[S, T] = S is an alias that is possible to write using TypeAliasType but impossible in 3.11 or earlier without it. I thought PEP exposing this as public and documented thing was to allow for this. Additionally typing extensions has it too which it wouldn’t need if intended to be internal. Lastly pyright does support this. |
Yes, we discussed this on typing-sig and it's intended for this usage to be supported on Python 3.11 and lower. |
#16926 added support for this, but I noticed that it's not supported within classes: from typing_extensions import TypeAliasType
class A:
T1 = TypeAliasType("T1", int)
x: A.T1 = 2 playground: https://mypy-play.net/?mypy=master&python=3.12&gist=f0b984556ec1fa1fa5cdef645bf3561f I'm not 100% sure this should be supported, but it does work on pyright. |
Yes, this should be supported anywhere that type alias definitions are allowed. That includes class bodies but not function bodies. |
mypy seems to allow explicit PEP 613 type aliases in a function body https://mypy-play.net/?mypy=latest&python=3.12&gist=911b732940310c98fb618098e386547b even though PEP 613 explicitly states:
about this example def bar() -> None:
x: TypeAlias = ClassName I am not sure if mypy allowing type aliases within a function body is a deliberate design choice or an oversight but I believe that whatever the chosen behavior, using PEP 613 |
Bug Report
Creating a simple TypeAliasType results in mypy reporting a type error.
To Reproduce
Playground:
https://mypy-play.net/?mypy=latest&python=3.11&gist=294b3a573ab591aa3026f7c05645c809
Gist:
https://gist.github.com/mypy-play/294b3a573ab591aa3026f7c05645c809
Expected Behavior
No type error should be reported.
Actual Behavior
mypy reports:
main.py:3: error: Variable "__main__.TestType" is not valid as a type [valid-type]
Your Environment
The text was updated successfully, but these errors were encountered: