-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Closed as not planned
Labels
Description
Bug Report
Unable to create TypeVar using unpacked types as constraints instead of explicit list of types.
In my specific instance I was trying to constraint a TypeVar using *get_args() of a TypeAlias defined before, but this problems occurs also using a explicit tuple of types.
To Reproduce
from __future__ import annotations
from typing import TypeAlias, TypeVar, get_args, TypeGuard
A: TypeAlias = int | float
d = (int,float)
#T = TypeVar("T", int, float) # Correctly working if uncommented
T = TypeVar("T", *d)
#T = TypeVar("T", *get_args(A)) # Same results using this tuple of types instead of d
def is_valid(value:A, t:type[T])->TypeGuard[T]:
return isinstance(A, t)
def is_int(value:A) -> TypeGuard[int]:
return is_valid(value, int)
def is_float(value:A) -> TypeGuard[float]:
return is_valid(value, float)
def is_str(value:A) -> TypeGuard[str]:
return is_valid(value, str) # Should give error if T follows the type constraints
a: A = 34
reveal_type(a) # Union[builtins.int, builtins.float]
if is_valid(x:= a, int):
reveal_type(x) # Should give builtins.int instead gives type is "T?"
if is_int(x:=a):
reveal_type(x) # Gives type is "builtins.int" in both casesExpected Behavior
No mypy error, with working types constraints.
Actual Behavior
In both cases the following error is identified by mypy:
Unexpected argument to "TypeVar()" [misc]at T definitionVariable "__main__.T" is not valid as a type [valid-type]at is_valid definition
If using the explicit int, float type constraints code works as expected.
Your Environment
- Mypy version used: mypy 0.960
- Python version used: 3.10
Reactions are currently unavailable