Skip to content
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

Closed
ks-mw opened this issue Dec 4, 2023 · 8 comments · Fixed by #17038
Closed

Add support for TypeAliasType #16614

ks-mw opened this issue Dec 4, 2023 · 8 comments · Fixed by #17038
Assignees
Labels
feature topic-type-alias TypeAlias and other type alias issues

Comments

@ks-mw
Copy link

ks-mw commented Dec 4, 2023

Bug Report

Creating a simple TypeAliasType results in mypy reporting a type error.

To Reproduce

from typing_extensions import TypeAliasType
TestType = TypeAliasType("TestType", int)
x: TestType = 42

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

  • Mypy version used: 1.7.1
  • Python version used: 3.11, 3.10
@ks-mw ks-mw added the bug mypy got something wrong label Dec 4, 2023
@JelleZijlstra JelleZijlstra added feature and removed bug mypy got something wrong labels Dec 4, 2023
@JelleZijlstra JelleZijlstra changed the title 'Variable "..." is not valid as a type [valid-type]' for TypeAliasType Add support for TypeAliasType Dec 4, 2023
@JelleZijlstra
Copy link
Member

TypeAliasType is not supported yet. See also #15238. PRs welcome!

@ks-mw
Copy link
Author

ks-mw commented Dec 4, 2023

Thank you for the information!

@sobolevn
Copy link
Member

sobolevn commented Dec 8, 2023

Do we really want to support this? I thought that TypeAliasType is an internal thing needed to support dynamic checks for constructs like type A = ... and isinstance(A, TypeAliasType).

I don't think that we need to promote the usage OP is proposing.

@AlexWaygood AlexWaygood added the topic-type-alias TypeAlias and other type alias issues label Dec 8, 2023
@hmc-cs-mdrissi
Copy link
Contributor

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.

@JelleZijlstra
Copy link
Member

Yes, we discussed this on typing-sig and it's intended for this usage to be supported on Python 3.11 and lower.

@tmke8
Copy link
Contributor

tmke8 commented Mar 15, 2024

#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.

@erictraut
Copy link

Yes, this should be supported anywhere that type alias definitions are allowed. That includes class bodies but not function bodies.

@hamdanal
Copy link
Collaborator

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:

Inside bar, the type checker should raise a clear error, communicating to the author that type aliases cannot be defined inside a function.

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 TypeAlias or PEP 695 TypeAliasType should be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants