Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve error message for bound typevar in TypeAliasType#17053

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 1 commit intopython:masterfromhamdanal:typealiastype-class
Mar 28, 2024

Conversation

@hamdanal
Copy link
Collaborator

Follow up to#17038

When a type variable is bound to a class, it cannot be reused in a type alias. Previously inTypeAliasType, this error was reported as "not included in type_params". However in the following example, the error is misleading:

fromtypingimportDict,Generic,TypeVarfromtyping_extensionsimportTypeAliasTypeT=TypeVar("T")classA(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.

@hamdanal
Copy link
CollaboratorAuthor

It would be nice to also get rid of the second remaining error, but this will require adding a special-cased context only for analyzingtype_params and I am not comfortable doing this change without first consulting someone with more experience than me.

@github-actions
Copy link
Contributor

According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JelleZijlstraJelleZijlstra self-requested a reviewMarch 21, 2024 02:00
@JelleZijlstraJelleZijlstra merged commit337bcf9 intopython:masterMar 28, 2024
@hamdanalhamdanal deleted the typealiastype-class branchMarch 30, 2024 13:46
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@JelleZijlstraJelleZijlstraJelleZijlstra approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@hamdanal@JelleZijlstra

[8]ページ先頭

©2009-2025 Movatter.jp