-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Implement typing.final for methods
#21646
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
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e025d6
[ty] Implement `typing.final` for methods
AlexWaygood b885cb7
fix unrelated mdtest failure
AlexWaygood 3ca2675
another edge case
AlexWaygood 47d07e2
another edge case
AlexWaygood 486caf5
another edge case
AlexWaygood 76ccc48
try to optimize
AlexWaygood 408895e
use a standalone query to check if it's a function definition
AlexWaygood 5cc8e66
check if rules are enabled before doing expensive analysis
AlexWaygood 9e65bb2
Merge branch 'main' into alex/final-method
AlexWaygood ce8d67e
Update crates/ty_python_semantic/resources/mdtest/final.md
AlexWaygood 6d40d0e
make recommendation clearer and add autofix
AlexWaygood cb520d5
Add tests for possibly-undefined cases
AlexWaygood 33915c1
Avoid repetition in the primary diagnostic message
AlexWaygood f8eb0d2
todo a couple more things
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
352 changes: 352 additions & 0 deletions
352
...napshots/final.md_-_Tests_for_the_`@typi…_-_A_possibly-undefined…_(fc7b496fd1986deb).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,352 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: final.md - Tests for the `@typing(_extensions).final` decorator - A possibly-undefined `@final` method is overridden | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/final.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | from typing import final | ||
| 2 | | ||
| 3 | def coinflip() -> bool: | ||
| 4 | return False | ||
| 5 | | ||
| 6 | class A: | ||
| 7 | if coinflip(): | ||
| 8 | @final | ||
| 9 | def method1(self) -> None: ... | ||
| 10 | else: | ||
| 11 | def method1(self) -> None: ... | ||
| 12 | | ||
| 13 | if coinflip(): | ||
| 14 | def method2(self) -> None: ... | ||
| 15 | else: | ||
| 16 | @final | ||
| 17 | def method2(self) -> None: ... | ||
| 18 | | ||
| 19 | if coinflip(): | ||
| 20 | @final | ||
| 21 | def method3(self) -> None: ... | ||
| 22 | else: | ||
| 23 | @final | ||
| 24 | def method3(self) -> None: ... | ||
| 25 | | ||
| 26 | if coinflip(): | ||
| 27 | def method4(self) -> None: ... | ||
| 28 | elif coinflip(): | ||
| 29 | @final | ||
| 30 | def method4(self) -> None: ... | ||
| 31 | else: | ||
| 32 | def method4(self) -> None: ... | ||
| 33 | | ||
| 34 | class B(A): | ||
| 35 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 38 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| 39 | | ||
| 40 | # Possible overrides of possibly `@final` methods... | ||
| 41 | class C(A): | ||
| 42 | if coinflip(): | ||
| 43 | # TODO: the autofix here introduces invalid syntax because there are now no | ||
| 44 | # statements inside the `if:` branch | ||
| 45 | # (but it might still be a useful autofix in an IDE context?) | ||
| 46 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 47 | else: | ||
| 48 | pass | ||
| 49 | | ||
| 50 | if coinflip(): | ||
| 51 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 52 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 53 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method1` | ||
| --> src/mdtest_snippet.py:35:9 | ||
| | | ||
| 34 | class B(A): | ||
| 35 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method1` from superclass `A` | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| | | ||
| info: `A.method1` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:8:9 | ||
| | | ||
| 6 | class A: | ||
| 7 | if coinflip(): | ||
| 8 | @final | ||
| | ------ | ||
| 9 | def method1(self) -> None: ... | ||
| | ------- `A.method1` defined here | ||
| 10 | else: | ||
| 11 | def method1(self) -> None: ... | ||
| | | ||
| help: Remove the override of `method1` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 32 | def method4(self) -> None: ... | ||
| 33 | | ||
| 34 | class B(A): | ||
| - def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 35 + # error: [override-of-final-method] | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 38 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method2` | ||
| --> src/mdtest_snippet.py:36:9 | ||
| | | ||
| 34 | class B(A): | ||
| 35 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method2` from superclass `A` | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 38 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| | | ||
| info: `A.method2` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:16:9 | ||
| | | ||
| 14 | def method2(self) -> None: ... | ||
| 15 | else: | ||
| 16 | @final | ||
| | ------ | ||
| 17 | def method2(self) -> None: ... | ||
| | ------- `A.method2` defined here | ||
| 18 | | ||
| 19 | if coinflip(): | ||
| | | ||
| help: Remove the override of `method2` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 33 | | ||
| 34 | class B(A): | ||
| 35 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| - def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 36 + # error: [override-of-final-method] | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 38 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| 39 | | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method3` | ||
| --> src/mdtest_snippet.py:37:9 | ||
| | | ||
| 35 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method3` from superclass `A` | ||
| 38 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| | | ||
| info: `A.method3` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:20:9 | ||
| | | ||
| 19 | if coinflip(): | ||
| 20 | @final | ||
| | ------ | ||
| 21 | def method3(self) -> None: ... | ||
| | ------- `A.method3` defined here | ||
| 22 | else: | ||
| 23 | @final | ||
| | | ||
| help: Remove the override of `method3` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 34 | class B(A): | ||
| 35 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| - def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 37 + # error: [override-of-final-method] | ||
| 38 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| 39 | | ||
| 40 | # Possible overrides of possibly `@final` methods... | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method4` | ||
| --> src/mdtest_snippet.py:38:9 | ||
| | | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 38 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method4` from superclass `A` | ||
| 39 | | ||
| 40 | # Possible overrides of possibly `@final` methods... | ||
| | | ||
| info: `A.method4` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:29:9 | ||
| | | ||
| 27 | def method4(self) -> None: ... | ||
| 28 | elif coinflip(): | ||
| 29 | @final | ||
| | ------ | ||
| 30 | def method4(self) -> None: ... | ||
| | ------- `A.method4` defined here | ||
| 31 | else: | ||
| 32 | def method4(self) -> None: ... | ||
| | | ||
| help: Remove the override of `method4` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 35 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 36 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 37 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| - def method4(self) -> None: ... # error: [override-of-final-method] | ||
| 38 + # error: [override-of-final-method] | ||
| 39 | | ||
| 40 | # Possible overrides of possibly `@final` methods... | ||
| 41 | class C(A): | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method1` | ||
| --> src/mdtest_snippet.py:46:13 | ||
| | | ||
| 44 | # statements inside the `if:` branch | ||
| 45 | # (but it might still be a useful autofix in an IDE context?) | ||
| 46 | def method1(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method1` from superclass `A` | ||
| 47 | else: | ||
| 48 | pass | ||
| | | ||
| info: `A.method1` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:8:9 | ||
| | | ||
| 6 | class A: | ||
| 7 | if coinflip(): | ||
| 8 | @final | ||
| | ------ | ||
| 9 | def method1(self) -> None: ... | ||
| | ------- `A.method1` defined here | ||
| 10 | else: | ||
| 11 | def method1(self) -> None: ... | ||
| | | ||
| help: Remove the override of `method1` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 43 | # TODO: the autofix here introduces invalid syntax because there are now no | ||
| 44 | # statements inside the `if:` branch | ||
| 45 | # (but it might still be a useful autofix in an IDE context?) | ||
| - def method1(self) -> None: ... # error: [override-of-final-method] | ||
| 46 + # error: [override-of-final-method] | ||
| 47 | else: | ||
| 48 | pass | ||
| 49 | | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method2` | ||
| --> src/mdtest_snippet.py:51:13 | ||
| | | ||
| 50 | if coinflip(): | ||
| 51 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method2` from superclass `A` | ||
| 52 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 53 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| | | ||
| info: `A.method2` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:16:9 | ||
| | | ||
| 14 | def method2(self) -> None: ... | ||
| 15 | else: | ||
| 16 | @final | ||
| | ------ | ||
| 17 | def method2(self) -> None: ... | ||
| | ------- `A.method2` defined here | ||
| 18 | | ||
| 19 | if coinflip(): | ||
| | | ||
| help: Remove the override of `method2` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 48 | pass | ||
| 49 | | ||
| 50 | if coinflip(): | ||
| - def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 51 + # error: [override-of-final-method] | ||
| 52 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 53 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method3` | ||
| --> src/mdtest_snippet.py:52:13 | ||
| | | ||
| 50 | if coinflip(): | ||
| 51 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 52 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method3` from superclass `A` | ||
| 53 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| | | ||
| info: `A.method3` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:20:9 | ||
| | | ||
| 19 | if coinflip(): | ||
| 20 | @final | ||
| | ------ | ||
| 21 | def method3(self) -> None: ... | ||
| | ------- `A.method3` defined here | ||
| 22 | else: | ||
| 23 | @final | ||
| | | ||
| help: Remove the override of `method3` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 49 | | ||
| 50 | if coinflip(): | ||
| 51 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| - def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 52 + # error: [override-of-final-method] | ||
| 53 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[override-of-final-method]: Cannot override `A.method4` | ||
| --> src/mdtest_snippet.py:53:13 | ||
| | | ||
| 51 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 52 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| 53 | def method4(self) -> None: ... # error: [override-of-final-method] | ||
| | ^^^^^^^ Override of `method4` from superclass `A` | ||
| | | ||
| info: `A.method4` is decorated with `@final`, forbidding overrides | ||
| --> src/mdtest_snippet.py:29:9 | ||
| | | ||
| 27 | def method4(self) -> None: ... | ||
| 28 | elif coinflip(): | ||
| 29 | @final | ||
| | ------ | ||
| 30 | def method4(self) -> None: ... | ||
| | ------- `A.method4` defined here | ||
| 31 | else: | ||
| 32 | def method4(self) -> None: ... | ||
| | | ||
| help: Remove the override of `method4` | ||
| info: rule `override-of-final-method` is enabled by default | ||
| 50 | if coinflip(): | ||
| 51 | def method2(self) -> None: ... # error: [override-of-final-method] | ||
| 52 | def method3(self) -> None: ... # error: [override-of-final-method] | ||
| - def method4(self) -> None: ... # error: [override-of-final-method] | ||
| 53 + # error: [override-of-final-method] | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@MichaReiser, I feel like I remember Ruff having this issue in the past -- can you remember what the solution was? Ideally we'd mark the autofix as display-only if we can see that it's going to introduce invalid syntax. (Or use a
range_replacementthat replaces the definition withpass.)Uh oh!
There was an error while loading. Please reload this page.
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.
We have a
DisplayOnlyApplicability. I don't think they are shown anywhere by default (ever?). The IDE also filters out manual only edits.I don't think we should add fixes that introduce syntax errors. I'm also not convinced that it's the right fix. It's as likely that the proper fix is to remove the
@finalThere 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.
Right, I was more wondering if Ruff had a nice little routine somewhere to easily detect that this was the only statement in the
if:branch (and therefore that a naive deletion would introduce an unsafe fix)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.
ruff/crates/ruff_linter/src/fix/edits.rs
Lines 25 to 63 in 096e539
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.
ty
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.
seems complicated, I'll leave this for a followup
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.
haha yeah