You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve error message for bound typevar in TypeAliasType (#17053)
Follow up to #17038
When a type variable is bound to a class, it cannot be reused in a type
alias. Previously in `TypeAliasType`, this error was reported as "not
included in type_params". However in the following example, the error is
misleading:
```python
from typing import Dict, Generic, TypeVar
from typing_extensions import TypeAliasType
T = TypeVar("T")
class A(Generic[T]):
Ta11 = TypeAliasType("Ta11", Dict[str, T], type_params=(T,))
x: A.Ta11 = {"a": 1}
reveal_type(x)
```
On the master branch:
```
main.py:8: error: Type variable "T" is not included in type_params [valid-type]
main.py:8: error: "T" is a type variable and only valid in type context [misc]
main.py:8: error: Free type variable expected in type_params argument to TypeAliasType [type-var]
main.py:12: note: Revealed type is "builtins.dict[builtins.str, Any]"
Found 3 errors in 1 file (checked 1 source file)
```
With this PR:
```
typealiastype.py:8: error: Can't use bound type variable "T" to define generic alias [valid-type]
typealiastype.py:8: error: "T" is a type variable and only valid in type context [misc]
typealiastype.py:12: note: Revealed type is "builtins.dict[builtins.str, Any]"
Found 2 errors in 1 file (checked 1 source file)
```
This is possible by storing the names of all the declared type_params,
even those that are invalid, and checking if the offending type
variables are in the list.
0 commit comments