-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[ty] Add blanket ignore comment rule #26426
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
+189
−3
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fa691cf
Add blanket ignore comment rule
MichaReiser 971021c
Always skip unused blanket ignores
MichaReiser 5346db9
Simplify blanket suppression filtering
MichaReiser 75cea44
Explain unused blanket ignore behavior
MichaReiser 46b17dd
Organize unused ignore tests
MichaReiser 971eed5
Always report blanket ignore comments
MichaReiser 83cd54e
Remove blanket ignore interaction note
MichaReiser 276e7f0
Refine blanket ignore documentation
MichaReiser a88b751
Update blanket ignore stabilization version
MichaReiser d60437b
Simplify blanket ignore mdtest documentation
MichaReiser c168d2c
Clarify unused blanket ignore behavior
MichaReiser 66ad3c2
Update crates/ty_python_semantic/resources/mdtest/suppressions/blanke…
MichaReiser 6f985de
Avoid parsing documentation as ignore comment
MichaReiser 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
crates/ty_python_semantic/resources/lint_docs/blanket-ignore-comment.md
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,22 @@ | ||
| ## What it does | ||
|
|
||
| Checks for `ty: ignore` comments that don't specify which rules to ignore. | ||
|
|
||
| ## Why is this bad? | ||
|
|
||
| A blanket `ty: ignore` comment suppresses every type-checking diagnostic on the | ||
| applicable line or file. Specifying rule codes documents which diagnostics are | ||
| expected and prevents the comment from silencing unrelated errors. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```py | ||
| # error | ||
| value = unknown # ty: ignore | ||
| ``` | ||
|
|
||
| Use instead: | ||
|
|
||
| ```py | ||
| value = unknown # ty: ignore[unresolved-reference] | ||
| ``` |
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
72 changes: 72 additions & 0 deletions
72
crates/ty_python_semantic/resources/mdtest/suppressions/blanket_ignore.md
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,72 @@ | ||
| # Blanket `ty: ignore` comments | ||
|
|
||
| The optional `blanket-ignore-comment` rule requires `ty: ignore` comments to include specific rule | ||
| codes. | ||
|
|
||
| ```toml | ||
| [rules] | ||
| blanket-ignore-comment = "error" | ||
| ``` | ||
|
|
||
| ## Line-level ignores | ||
|
|
||
| ```py | ||
| # error: [blanket-ignore-comment] | ||
| a = unresolved # ty: ignore | ||
|
|
||
| b = unresolved # ty: ignore[unresolved-reference] | ||
|
|
||
| # `blanket-ignore-comment` covers only `ty: ignore`; we can also add `blanket-type-ignore-comment`, | ||
| # parallel to `unused-ignore-comment` and `unused-type-ignore-comment`. In the meantime, blanket | ||
| # `type: ignore` can also be caught by Ruff's PGH003 rule. | ||
| c = unresolved # type: ignore | ||
| ``` | ||
|
|
||
| ## Unused ignore comments | ||
|
|
||
| When `unused-ignore-comment` is enabled, an unused `ty: ignore` comment without rule codes triggers | ||
| both rules. | ||
|
|
||
| ```py | ||
| # error: [unused-ignore-comment] "Unused `ty: ignore` without a code" | ||
| d = 1 # ty: ignore[] | ||
|
|
||
| # error: [blanket-ignore-comment] | ||
| # error: [unused-ignore-comment] "Unused blanket `ty: ignore` directive" | ||
| e = 1 # ty: ignore | ||
| ``` | ||
|
|
||
| ## Suppression diagnostics | ||
|
|
||
| Suppression-related diagnostics are checked before `blanket-ignore-comment`. A blanket ignore that | ||
| suppresses an `ignore-comment-unknown-rule` or `invalid-ignore-comment` diagnostic therefore counts | ||
| as used: | ||
|
|
||
| ```py | ||
| # The nested ignore contains an unknown rule. | ||
| # error: [blanket-ignore-comment] | ||
| a = 1 # ty: ignore # ty: ignore[not-a-rule] | ||
|
|
||
| # The nested ignore is invalid. | ||
| # error: [blanket-ignore-comment] | ||
| b = 1 # ty: ignore # ty: ignore[*] | ||
| ``` | ||
|
|
||
| ## File-level ignores | ||
|
|
||
| The rule also detects file-level blanket ignores: | ||
|
|
||
| ```py | ||
| # error: [blanket-ignore-comment] | ||
| # ty: ignore | ||
|
|
||
| a = unresolved | ||
| ``` | ||
|
|
||
| ## Suppressing the rule | ||
|
|
||
| A blanket ignore can be suppressed by a code-specific ignore: | ||
|
|
||
| ```py | ||
| a = unresolved # ty: ignore # ty: ignore[blanket-ignore-comment] | ||
| ``` | ||
9 changes: 8 additions & 1 deletion
9
crates/ty_python_semantic/resources/mdtest/suppressions/ty_ignore.md
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.