-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[ty] fix lazy snapshot sweeping in nested scopes #19908
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-08-14 06:00:34.776618614 +0000
+++ new-output.txt 2025-08-14 06:00:34.846619163 +0000
@@ -1,5 +1,5 @@
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
-fatal[panic] Panicked at /home/runner/.cargo/git/checkouts/salsa-e6f3bb7c2a062968/918d35d/src/function/execute.rs:215:25 when checking `/home/runner/work/ruff/ruff/typing/conformance/tests/aliases_typealiastype.py`: `infer_definition_types(Id(1da61)): execute: too many cycle iterations`
+fatal[panic] Panicked at /home/runner/.cargo/git/checkouts/salsa-e6f3bb7c2a062968/918d35d/src/function/execute.rs:215:25 when checking `/home/runner/work/ruff/ruff/typing/conformance/tests/aliases_typealiastype.py`: `infer_definition_types(Id(50b5)): execute: too many cycle iterations`
_directives_deprecated_library.py:15:31: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `int`
_directives_deprecated_library.py:30:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
_directives_deprecated_library.py:36:41: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `Self@__add__` |
|
| if x is not None: | ||
| def closure(): | ||
| reveal_type(x) # revealed: str | None | ||
| x = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon closer analysis, it seems that whether or not narrowing is applied to x depends on whether the closure is exposed or just consumed in the scope (This PR does not take this into account and always assumes that the closure is exposed).
def f(x: str | None):
def _():
if x is not None:
def closure():
reveal_type(x) # revealed: str | None
return closure # expose a closure
x = None
def f(x: str | None):
def _():
if x is not None:
def closure():
reveal_type(x) # revealed: str
return closure() # just consume a closure
x = NoneTo achieve this, an additional mechanism would be required to determine whether a closure is exposed (pyright does not seem to implement this). This would likely be a future work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think in general we consider escape analysis for closures to be out of scope. Perhaps we could tackle it someday, but it's not needed now.
carljm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thank you!
| if x is not None: | ||
| def closure(): | ||
| reveal_type(x) # revealed: str | None | ||
| x = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think in general we consider escape analysis for closures to be out of scope. Perhaps we could tackle it someday, but it's not needed now.
* main: [ty] Represent `NamedTuple` as an opaque special form, not a class (#19915) [ty] Remove incorrect type narrowing for `if type(x) is C[int]` (#19926) Bump Rust MSRV to 1.87 (#19924) Add `else`-branch narrowing for `if type(a) is A` when `A` is `@final` (#19925) [ty] Sync vendored typeshed stubs (#19923) [ty] fix lazy snapshot sweeping in nested scopes (#19908)
Summary
This PR closes astral-sh/ty#955.
Test Plan
New test cases in
narrowing/conditionals/nested.md.