Commit 337bcf9
authored
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.1 parent 4310586 commit 337bcf9
File tree
3 files changed
+55
-25
lines changed- mypy
- test-data/unit
3 files changed
+55
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3521 | 3521 | | |
3522 | 3522 | | |
3523 | 3523 | | |
| 3524 | + | |
3524 | 3525 | | |
3525 | 3526 | | |
3526 | 3527 | | |
| |||
3573 | 3574 | | |
3574 | 3575 | | |
3575 | 3576 | | |
3576 | | - | |
| 3577 | + | |
3577 | 3578 | | |
3578 | 3579 | | |
3579 | 3580 | | |
| |||
3622 | 3623 | | |
3623 | 3624 | | |
3624 | 3625 | | |
| 3626 | + | |
3625 | 3627 | | |
3626 | 3628 | | |
3627 | 3629 | | |
3628 | | - | |
| 3630 | + | |
3629 | 3631 | | |
3630 | 3632 | | |
3631 | 3633 | | |
3632 | 3634 | | |
| 3635 | + | |
3633 | 3636 | | |
3634 | 3637 | | |
3635 | 3638 | | |
| |||
3686 | 3689 | | |
3687 | 3690 | | |
3688 | 3691 | | |
3689 | | - | |
| 3692 | + | |
| 3693 | + | |
| 3694 | + | |
| 3695 | + | |
| 3696 | + | |
3690 | 3697 | | |
3691 | 3698 | | |
3692 | 3699 | | |
| |||
3803 | 3810 | | |
3804 | 3811 | | |
3805 | 3812 | | |
3806 | | - | |
| 3813 | + | |
| 3814 | + | |
| 3815 | + | |
| 3816 | + | |
| 3817 | + | |
| 3818 | + | |
| 3819 | + | |
| 3820 | + | |
3807 | 3821 | | |
3808 | 3822 | | |
3809 | 3823 | | |
3810 | 3824 | | |
3811 | 3825 | | |
3812 | 3826 | | |
3813 | 3827 | | |
3814 | | - | |
| 3828 | + | |
3815 | 3829 | | |
3816 | 3830 | | |
3817 | | - | |
| 3831 | + | |
3818 | 3832 | | |
3819 | 3833 | | |
| 3834 | + | |
3820 | 3835 | | |
3821 | 3836 | | |
3822 | 3837 | | |
| |||
3843 | 3858 | | |
3844 | 3859 | | |
3845 | 3860 | | |
3846 | | - | |
3847 | | - | |
3848 | | - | |
3849 | | - | |
3850 | | - | |
3851 | 3861 | | |
3852 | | - | |
3853 | | - | |
3854 | | - | |
| 3862 | + | |
| 3863 | + | |
| 3864 | + | |
| 3865 | + | |
| 3866 | + | |
| 3867 | + | |
| 3868 | + | |
3855 | 3869 | | |
| 3870 | + | |
| 3871 | + | |
| 3872 | + | |
| 3873 | + | |
3856 | 3874 | | |
3857 | 3875 | | |
3858 | 3876 | | |
| |||
3862 | 3880 | | |
3863 | 3881 | | |
3864 | 3882 | | |
| 3883 | + | |
3865 | 3884 | | |
3866 | | - | |
| 3885 | + | |
3867 | 3886 | | |
3868 | 3887 | | |
3869 | 3888 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | | - | |
| 162 | + | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
215 | | - | |
| 215 | + | |
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
237 | | - | |
| 237 | + | |
238 | 238 | | |
239 | 239 | | |
240 | 240 | | |
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
278 | 284 | | |
279 | 285 | | |
280 | 286 | | |
| |||
329 | 335 | | |
330 | 336 | | |
331 | 337 | | |
332 | | - | |
| 338 | + | |
333 | 339 | | |
334 | 340 | | |
335 | 341 | | |
| |||
357 | 363 | | |
358 | 364 | | |
359 | 365 | | |
360 | | - | |
| 366 | + | |
361 | 367 | | |
362 | 368 | | |
363 | 369 | | |
| |||
376 | 382 | | |
377 | 383 | | |
378 | 384 | | |
379 | | - | |
| 385 | + | |
380 | 386 | | |
381 | 387 | | |
382 | 388 | | |
| |||
386 | 392 | | |
387 | 393 | | |
388 | 394 | | |
389 | | - | |
| 395 | + | |
390 | 396 | | |
391 | 397 | | |
392 | 398 | | |
| |||
1281 | 1287 | | |
1282 | 1288 | | |
1283 | 1289 | | |
1284 | | - | |
| 1290 | + | |
1285 | 1291 | | |
1286 | 1292 | | |
1287 | 1293 | | |
1288 | | - | |
| 1294 | + | |
1289 | 1295 | | |
1290 | 1296 | | |
1291 | 1297 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1195 | 1195 | | |
1196 | 1196 | | |
1197 | 1197 | | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
1198 | 1203 | | |
1199 | 1204 | | |
1200 | 1205 | | |
| |||
0 commit comments