Skip to content

Bypass class attribute lookup for PEP 695 typevars - #20292

Merged
ilevkivskyi merged 3 commits into
python:masterfrom
A5rocks:use-locals-always
Oct 14, 2026
Merged

ilevkivskyi merged 3 commits into
python:masterfrom
A5rocks:use-locals-always

Conversation

@A5rocks

@A5rocks A5rocks commented Nov 23, 2025 •

Copy link
Copy Markdown
Collaborator

Fixes #20283

This is a bit gross, but I'm not sure how to better do this. The problem is that we look up class attributes before looking at locals, but then put the type vars in a new locals scope. I guess one idea would be to define the typevars at usage time? But this is less of a change.

@github-actions

This comment has been minimized.

@sterliakov sterliakov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice one! This part definitely should not use lookup, we are only interested in locals, and (except for one bad corner case) only in the most recent locals entry. Traversing further is only needed when we have

class Foo[T]:
    class Bar[T]:  # Error here, but typevar is still needed
        ...

Comment thread mypy/semanal.py Outdated
Comment thread mypy/semanal.py Outdated
@ilevkivskyi

Copy link
Copy Markdown
Member

@A5rocks I think it makes sense to implement comments by @sterliakov, also there is a little merge conflict, but otherwise this LG.

@A5rocks

A5rocks commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator Author

Done! Sorry for the wait.

@github-actions

Copy link
Copy Markdown
Contributor

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

@ilevkivskyi
ilevkivskyi merged commit 457f913 into python:master Oct 14, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assertion failure crash: assert isinstance(node.node, TypeVarLikeExpr)

4 participants