-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Offer "Did you mean...?" suggestions for unresolved from imports and unresolved attributes
#18705
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
Merged
Merged
[ty] Offer "Did you mean...?" suggestions for unresolved from imports and unresolved attributes
#18705
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ed75004
Basic implementation of Levenshtein
AlexWaygood 71c04cf
more tests, attempt to port CPython's implementation
AlexWaygood 305866c
get everything compiling and module tests passing
ntBre c1e6488
accept snapshot that Python doesn't give a suggestion for either
ntBre 0b668d7
accept fixed suggestion
ntBre f859ed0
add expected error annotation
ntBre b12cf86
Move to submodule and fix a few nits
AlexWaygood dfe7353
Add suggestions for unresolved attributes too
AlexWaygood 2e0d3be
Port more tests
AlexWaygood f8291c2
fix typos introduced when porting tests
AlexWaygood 9bffbd1
Use `test_case` for other unit tests too
AlexWaygood ab38f8f
use annotation message for both diagnostics
AlexWaygood 1188c55
minor cleanups
AlexWaygood 3ee7125
More tests, fix tests, rename `ide_support` module
AlexWaygood e0e7a4a
apply CPython's handling of suggestions that start with underscores
AlexWaygood a5adbaa
run pre-commit
AlexWaygood 4d357ea
Merge branch 'main' into alex-brent/did-you-mean
AlexWaygood ab16e70
update snapshots
AlexWaygood 8b0b5bc
cargo dev generate-all
AlexWaygood f7cdb88
fix checkout on Windows
AlexWaygood 067300e
Merge branch 'main' into alex-brent/did-you-mean
AlexWaygood 7633862
Merge branch 'main' into alex-brent/did-you-mean
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...test/snapshots/attributes.md_-_Attributes_-_Suggestions_for_obvi…_(bf7b28ef99f0ec16).snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: attributes.md - Attributes - Suggestions for obvious typos | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/attributes.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | import collections | ||
| 2 | | ||
| 3 | print(collections.dequee) # error: [unresolved-attribute] | ||
| 4 | class Foo: | ||
| 5 | _bar = 42 | ||
| 6 | | ||
| 7 | print(Foo.bar) # error: [unresolved-attribute] | ||
| 8 | print(Foo._barr) # error: [unresolved-attribute] | ||
| 9 | class Bar: | ||
| 10 | _attribute = 42 | ||
| 11 | | ||
| 12 | def f(self, x: "Bar"): | ||
| 13 | # TODO: we should emit `[unresolved-attribute]` here, should have the same behaviour as `x.attribute` below | ||
| 14 | print(self.attribute) | ||
| 15 | | ||
| 16 | # We give a suggestion here, even though the only good candidates start with underscores and the typo does not, | ||
| 17 | # because we're in a method context and `x` is an instance of the enclosing class. | ||
| 18 | print(x.attribute) # error: [unresolved-attribute] | ||
| 19 | | ||
| 20 | class Baz: | ||
| 21 | def f(self, x: Bar): | ||
| 22 | # No suggestion is given here, because: | ||
| 23 | # - the good suggestions all start with underscores | ||
| 24 | # - the typo does not start with an underscore | ||
| 25 | # - We *are* in a method context, but `x` is not an instance of the enclosing class | ||
| 26 | print(x.attribute) # error: [unresolved-attribute] | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[unresolved-attribute]: Type `<module 'collections'>` has no attribute `dequee` | ||
| --> src/mdtest_snippet.py:3:7 | ||
| | | ||
| 1 | import collections | ||
| 2 | | ||
| 3 | print(collections.dequee) # error: [unresolved-attribute] | ||
| | ^^^^^^^^^^^^^^^^^^ Did you mean `deque`? | ||
| 4 | class Foo: | ||
| 5 | _bar = 42 | ||
| | | ||
| info: rule `unresolved-attribute` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[unresolved-attribute]: Type `<class 'Foo'>` has no attribute `bar` | ||
| --> src/mdtest_snippet.py:7:7 | ||
| | | ||
| 5 | _bar = 42 | ||
| 6 | | ||
| 7 | print(Foo.bar) # error: [unresolved-attribute] | ||
| | ^^^^^^^ | ||
| 8 | print(Foo._barr) # error: [unresolved-attribute] | ||
| 9 | class Bar: | ||
| | | ||
| info: rule `unresolved-attribute` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[unresolved-attribute]: Type `<class 'Foo'>` has no attribute `_barr` | ||
| --> src/mdtest_snippet.py:8:7 | ||
| | | ||
| 7 | print(Foo.bar) # error: [unresolved-attribute] | ||
| 8 | print(Foo._barr) # error: [unresolved-attribute] | ||
| | ^^^^^^^^^ Did you mean `_bar`? | ||
| 9 | class Bar: | ||
| 10 | _attribute = 42 | ||
| | | ||
| info: rule `unresolved-attribute` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[unresolved-attribute]: Type `Bar` has no attribute `attribute` | ||
| --> src/mdtest_snippet.py:18:15 | ||
| | | ||
| 16 | # We give a suggestion here, even though the only good candidates start with underscores and the typo does not, | ||
| 17 | # because we're in a method context and `x` is an instance of the enclosing class. | ||
| 18 | print(x.attribute) # error: [unresolved-attribute] | ||
| | ^^^^^^^^^^^ Did you mean `_attribute`? | ||
| 19 | | ||
| 20 | class Baz: | ||
| | | ||
| info: rule `unresolved-attribute` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[unresolved-attribute]: Type `Bar` has no attribute `attribute` | ||
| --> src/mdtest_snippet.py:26:15 | ||
| | | ||
| 24 | # - the typo does not start with an underscore | ||
| 25 | # - We *are* in a method context, but `x` is not an instance of the enclosing class | ||
| 26 | print(x.attribute) # error: [unresolved-attribute] | ||
| | ^^^^^^^^^^^ | ||
| | | ||
| info: rule `unresolved-attribute` is enabled by default | ||
|
|
||
| ``` |
69 changes: 69 additions & 0 deletions
69
...es/mdtest/snapshots/basic.md_-_Structures_-_`from`_import_that_h…_(3caffc60d8390adf).snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: basic.md - Structures - `from` import that has a typo | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## foo.py | ||
|
|
||
| ``` | ||
| 1 | from collections import dequee # error: [unresolved-import] | ||
| ``` | ||
|
|
||
| ## bar.py | ||
|
|
||
| ``` | ||
| 1 | from baz import foo # error: [unresolved-import] | ||
| ``` | ||
|
|
||
| ## baz.py | ||
|
|
||
| ``` | ||
| 1 | _foo = 42 | ||
| ``` | ||
|
|
||
| ## eggs.py | ||
|
|
||
| ``` | ||
| 1 | from baz import _fooo # error: [unresolved-import] | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[unresolved-import]: Module `collections` has no member `dequee` | ||
| --> src/foo.py:1:25 | ||
| | | ||
| 1 | from collections import dequee # error: [unresolved-import] | ||
| | ^^^^^^ Did you mean `deque`? | ||
| | | ||
| info: rule `unresolved-import` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[unresolved-import]: Module `baz` has no member `foo` | ||
| --> src/bar.py:1:17 | ||
| | | ||
| 1 | from baz import foo # error: [unresolved-import] | ||
| | ^^^ | ||
| | | ||
| info: rule `unresolved-import` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[unresolved-import]: Module `baz` has no member `_fooo` | ||
| --> src/eggs.py:1:17 | ||
| | | ||
| 1 | from baz import _fooo # error: [unresolved-import] | ||
| | ^^^^^ Did you mean `_foo`? | ||
| | | ||
| info: rule `unresolved-import` is enabled by default | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
..._python_semantic/src/types/ide_support.rs → ..._python_semantic/src/types/all_members.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This module has been renamed from
ide_supporttoall_membersbecause it's no longer just used for autocompletions; it's now also used for diagnostic suggestions.