Skip to content

[ty] fix lookup order of class variables before they are defined#19743

Merged
carljm merged 2 commits intoastral-sh:mainfrom
mtshiba:fix-875-class-var-lookup
Aug 5, 2025
Merged

[ty] fix lookup order of class variables before they are defined#19743
carljm merged 2 commits intoastral-sh:mainfrom
mtshiba:fix-875-class-var-lookup

Conversation

@mtshiba
Copy link
Collaborator

@mtshiba mtshiba commented Aug 4, 2025

Summary

This is a follow-up to #19321.

If we try to access a class variable before it is defined, the variable is looked up in the global scope, rather than in any enclosing scopes.

Closes astral-sh/ty#875.

Test Plan

New tests in narrow/conditionals/nested.md.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2025

mypy_primer results

Changes were detected when running on open source projects
pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/pytester.py:1176:27: error[unresolved-reference] Name `ret` used when not defined
- Found 495 diagnostics
+ Found 496 diagnostics
No memory usage changes detected ✅

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Aug 4, 2025
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Looks good, thank you!

@carljm
Copy link
Contributor

carljm commented Aug 5, 2025

The ecosystem hit looks like a true positive to me.

@carljm carljm merged commit 64bcc8d into astral-sh:main Aug 5, 2025
39 checks passed
@mtshiba mtshiba deleted the fix-875-class-var-lookup branch August 7, 2025 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

names bound at some point in a class scope, but not at point of access, fall back to global scope

3 participants