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

TypeAliasType when a ParamSpec should be replaced raised type error #448

Closed
Daraan opened this issue Aug 21, 2024 · 0 comments · Fixed by #449
Closed

TypeAliasType when a ParamSpec should be replaced raised type error #448

Daraan opened this issue Aug 21, 2024 · 0 comments · Fixed by #449

Comments

@Daraan
Copy link
Contributor

Daraan commented Aug 21, 2024

TypeAliasType currently has some bug/limitation when using it together with a ParamSpec in Python <3.10.

Root of the problem is the changed typing._type_check that was changed in Python 3.11 which makes typing_extensions.TypeAliasType.__getitem__ run into the TypeError in older Python versions:

arg: Ellipsis | list[type] # should be valid
-     if not callable(arg):
-        raise TypeError(f"{msg} Got {arg!r:.100}.")
+  if type(arg) is tuple:
+        raise TypeError(f"{msg} Got {arg!r:.100}.")

The following cases work in Python 3.11+ and fail in <= Python3.10

from typing_extensions import TypeAliasType, Callable, ParamSpec, Any

P = ParamSpec('P')
Foo = TypeAliasType("Foo", Callable[P, Any], type_params=(P,))

# Ok
FooInt = Foo[int] # (int) -> Any

# Error
FooInt2 = Foo[[int]] # (int) -> Any

# Error
FooIntInt = Foo[[int, int]] # (int, int) -> Any

# Error
FooWhatever = Foo[...] # (...) -> Any

# Error; should be invalid context
FooWhatever2 = Foo[[...]] # (...) -> Any

# Error; ( but also invalid parameter expression -> should use Concatenate)
FooIntWhatever = Foo[[int, ...]] # (int, ...) -> Any

# OK, correct parameter expression
FooConcatIntWhatever = Foo[Concatenate[int, ...]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant