forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error message for bound typevar in TypeAliasType (python#17053)
Follow up to python#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.
- Loading branch information
Showing
3 changed files
with
55 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters