[ty] Offer "Did you mean...?" suggestions for unresolved from imports and unresolved attributes (#18705)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
This commit is contained in:
Alex Waygood 2025-06-17 11:10:34 +01:00 committed by GitHub
parent c7e020df6b
commit 913f136d33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 794 additions and 96 deletions

View file

@ -2167,6 +2167,57 @@ reveal_type(Foo.BAR.value) # revealed: @Todo(Attribute access on enum classes)
reveal_type(Foo.__members__) # revealed: @Todo(Attribute access on enum classes)
```
## Suggestions for obvious typos
<!-- snapshot-diagnostics -->
For obvious typos, we add a "Did you mean...?" suggestion to the diagnostic.
```py
import collections
print(collections.dequee) # error: [unresolved-attribute]
```
But the suggestion is suppressed if the only close matches start with a leading underscore:
```py
class Foo:
_bar = 42
print(Foo.bar) # error: [unresolved-attribute]
```
The suggestion is not suppressed if the typo itself starts with a leading underscore, however:
```py
print(Foo._barr) # error: [unresolved-attribute]
```
And in method contexts, the suggestion is never suppressed if accessing an attribute on an instance
of the method's enclosing class:
```py
class Bar:
_attribute = 42
def f(self, x: "Bar"):
# TODO: we should emit `[unresolved-attribute]` here, should have the same behaviour as `x.attribute` below
print(self.attribute)
# We give a suggestion here, even though the only good candidates start with underscores and the typo does not,
# because we're in a method context and `x` is an instance of the enclosing class.
print(x.attribute) # error: [unresolved-attribute]
class Baz:
def f(self, x: Bar):
# No suggestion is given here, because:
# - the good suggestions all start with underscores
# - the typo does not start with an underscore
# - We *are* in a method context, but `x` is not an instance of the enclosing class
print(x.attribute) # error: [unresolved-attribute]
```
## References
Some of the tests in the *Class and instance variables* section draw inspiration from