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

--py312-plus: type ... aliases #836

Closed
asottile opened this issue Jun 10, 2023 · 4 comments
Closed

--py312-plus: type ... aliases #836

asottile opened this issue Jun 10, 2023 · 4 comments

Comments

@asottile
Copy link
Owner

-IntOrStr: TypeAlias = int | str
+type IntOrStr = int | str

from PEP 695

@asottile
Copy link
Owner Author

really annoying part of the PEP that makes this not easily auto-rewritable:

Type alias expressions are not allowed to use traditional type variables (i.e. those allocated with an explicit TypeVar constructor call). Type checkers should generate an error in this case.

T = TypeVar("T")
type MyList = list[T] # Type checker error: traditional type variable usage

but maybe we just do it anyway and the user can deal? shrugs

@asottile
Copy link
Owner Author

it's also unclear to me when quoted forward references would ever be needed inside them. my reading of the PEP seems to indicate they are allowed but completely unnecessary... unclear.

if they're unnecessary -- the PEP 563 rewriter should apply to these as well

@asottile
Copy link
Owner Author

also perhaps even to the TypeVar bounds -- I've currently made them only rewrite if __future__.annotations is present -- but my reading of the PEP seems to mirror my thoughts in #836 (comment) as well so maybe this can be done unconditionally

@asottile
Copy link
Owner Author

asottile commented Jul 3, 2023

wellp, this seems to be poorly specced because it breaks code that uses the type alias at runtime

for instance:

>>> from typing import TypeAlias
>>> a: TypeAlias = int
>>> isinstance(1, a)

but after rewrite it would not work at all:

>>> type a = int
>>> isinstance(1, a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

:(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant