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

Fix incorrect TypeVar default expansion order (Fixes #20336)#20343

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

Open
ayushdoesdev wants to merge1 commit intopython:master
base:master
Choose a base branch
Loading
fromayushdoesdev:fix-20336-typevar-default-swap

Conversation

@ayushdoesdev
Copy link

This PR fixes how mypy handles generic classes with default type parameters when they refer to themselves inside the class body. ItFixes#20336

Before this change, if you wrote something like:

classFoo[X,Y,Z=object]:deftest(self,swapped:Foo[Y,X])->None:reveal_type(swapped)

mypy would show:

Revealed type is "Foo[X, Y, builtins.object]"

It ignored the fact thatFoo[Y, X] swaps the type variables.

With this change, mypy now keeps the order that the user wrote:

Revealed type is "Foo[Y, X, builtins.object]"

The fix is infix_instance inmypy/typeanal.py.
It checks for cases where all the type arguments are the class’s own type variables (likeFoo[Y, X]) and skips the extra expansion step that was reordering them.

Copy link
Collaborator

@sterliakovsterliakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Was this generated by some kind of LLM?

Please add a few testcases (at least from the original issue and from comments there).

@ayushdoesdev
Copy link
Author

Hi@sterliakov , no I took help but did code on my own because they were not able to help
and yes did code formatting with them

Ok I will add the test case

sterliakov reacted with thumbs up emoji

@github-actions
Copy link
Contributor

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

@ayushdoesdev
Copy link
Author

@sterliakov something up with .mypy_cache? does it show previous output even after changes?

@sterliakov
Copy link
Collaborator

Mhm, you can runmypy ... --cache-dir=/dev/null to rule that out, or remove.mypy_cache dir manually. Not applicable to tests, everything is cleaned up after a test run.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@sterliakovsterliakovsterliakov requested changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Swapping some type variables gives incorrect results

2 participants

@ayushdoesdev@sterliakov

[8]ページ先頭

©2009-2025 Movatter.jp