-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[ty] make del x force local resolution of x in the current scope
#19389
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
|
|
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.
Thanks! I think this is a behavior improvement and I'm ok with landing it as is, but it seems to me that our approach to nonlocal might not quite be fully there yet (discussed inline).
e9cac19 to
14f5744
Compare
Fixes astral-sh/ty#769. The preferred approach here is to keep the SemanticIndex simple (`del` of any name marks that name "bound" in the current scope) and to move complexity to type inference (free variable resolution stops when it finds a binding, unless that binding is declared `nonlocal`). As part of this change, free variable resolution will now union the types it finds as it walks in enclosing scopes. This approach is still incomplete, because it doesn't consider inner scopes or sibling scopes, but it improves the common case.
14f5744 to
8a926bb
Compare
|
Ok @carljm, I've reworked this PR and updated the description up top. As you suggested, I've moved the |
| // doesn't mark it `nonlocal`. The name is resolved, and we won't consider | ||
| // any scopes outside of this one. | ||
| return if let Some(type_) = nonlocal_union_builder.try_build() { | ||
| Place::Type(type_, local_boundness).into() |
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.
I'm fuzzy on the difference between Boundness::PossiblyUnbound and Place::Unbound, so please look extra close at what I'm doing here with local_boundness and tell me if this makes sense.
|
The updated ecosystem analysis has 4 new diagnostics that I think are Update: Ok, I think this is a minimized version of the new failures: def f():
x: int = 1
def g():
if not isinstance(x, int):
print(x) # [unresolved-reference]: Name `x` used when not defined <-- only on this PR, not mainI think what's happening is that my new type unioning, maybe plus the way x: int = 1
if not isinstance(x, int):
print(x) |
|
Ok, I've added a bool to distinguish "narrowing constraints result in ruff/crates/ty_python_semantic/src/types/infer.rs Lines 6117 to 6142 in 8db7db3
|
| x = "hello" # error: [invalid-assignment] "Object of type `Literal["hello"]` is not assignable to `int`" | ||
| ``` | ||
|
|
||
| ## The types of `nonlocal` binding get unioned |
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.
These tests are great! Very clear.
* main: [ty] Avoid secondary tree traversal to get call expression for keyword arguments (astral-sh#19429) [ty] Add goto definition to playground (astral-sh#19425) [ty] Add support for `@warnings.deprecated` (astral-sh#19376) [ty] make `del x` force local resolution of `x` in the current scope (astral-sh#19389) # Conflicts: # crates/ty_ide/src/goto.rs
* main: (25 commits) [ty] Sync vendored typeshed stubs (#19461) [ty] Extend tuple `__len__` and `__bool__` special casing to also cover tuple subclasses (#19289) [ty] bump docstring-adder pin (#19458) [ty] Disallow assignment to `Final` class attributes (#19457) Update dependency ruff to v0.12.4 (#19442) Update pre-commit hook astral-sh/ruff-pre-commit to v0.12.4 (#19443) Update rui314/setup-mold digest to 702b190 (#19441) Update taiki-e/install-action action to v2.56.19 (#19448) Update Rust crate strum_macros to v0.27.2 (#19447) Update Rust crate strum to v0.27.2 (#19446) Update Rust crate rand to v0.9.2 (#19444) Update Rust crate serde_json to v1.0.141 (#19445) Fix `unreachable` panic in parser (#19183) [`ruff`] Support byte strings (`RUF055`) (#18926) [ty] Avoid second lookup for `infer_maybe_standalone_expression` (#19439) [ty] Implemented "go to definition" support for import statements (#19428) [ty] Avoid secondary tree traversal to get call expression for keyword arguments (#19429) [ty] Add goto definition to playground (#19425) [ty] Add support for `@warnings.deprecated` (#19376) [ty] make `del x` force local resolution of `x` in the current scope (#19389) ...
Fixes astral-sh/ty#769.
Updated: The preferred approach here is to keep the SemanticIndex simple (
delof any name marks that name "bound" in the current scope) and to move complexity to type inference (free variable resolution stops when it finds a binding, unless that binding is declarednonlocal). As part of this change, free variable resolution will now union the types it finds as it walks in enclosing scopes. This approach is still incomplete, because it doesn't consider inner scopes or sibling scopes, but it improves the common case.