-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Avoid generating diagnostics with per-file ignores #18801
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
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PLC0415 | 7 | 7 | 0 | 0 | 0 |
RUF100 | 2 | 1 | 1 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+8 -1 violations, +0 -0 fixes in 2 projects; 53 projects unchanged)
apache/airflow (+7 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL
+ airflow-core/tests/unit/models/test_renderedtifields.py:205:9: PLC0415 `import` should be at the top-level of a file + airflow-core/tests/unit/serialization/serializers/test_serializers.py:249:9: PLC0415 `import` should be at the top-level of a file + providers/google/tests/unit/google/cloud/hooks/test_bigquery.py:157:9: PLC0415 `import` should be at the top-level of a file + providers/google/tests/unit/google/cloud/hooks/test_bigquery.py:158:9: PLC0415 `import` should be at the top-level of a file + providers/google/tests/unit/google/cloud/hooks/test_bigquery_system.py:40:13: PLC0415 `import` should be at the top-level of a file + providers/google/tests/unit/google/cloud/hooks/test_bigquery_system.py:44:13: PLC0415 `import` should be at the top-level of a file + providers/weaviate/tests/unit/weaviate/hooks/test_weaviate.py:393:9: PLC0415 `import` should be at the top-level of a file
ibis-project/ibis (+1 -1 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview
+ ibis/backends/tests/signature/typecheck.py:8:1: RUF100 Unused `noqa` directive (non-enabled: `D205`, `D415`, `D400`) - ibis/backends/tests/signature/typecheck.py:8:1: RUF100 Unused `noqa` directive (unused: `D205`, `D415`, `D400`)
Changes by rule (2 rules affected)
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PLC0415 | 7 | 7 | 0 | 0 | 0 |
RUF100 | 2 | 1 | 1 | 0 | 0 |
CodSpeed Instrumentation Performance ReportMerging #18801 will not alter performanceComparing Summary
|
I'm a bit confused by both the ecosystem and benchmark results. It's at least plausible that I broke per-file ignores, but I don't see PLC0415 in Airflow's main |
&& !per_file_ignores.contains(Rule::RedirectedNOQA) | ||
&& !exemption.includes(Rule::RedirectedNOQA) | ||
{ | ||
if context.enabled(Rule::RedirectedNOQA) && !exemption.includes(Rule::RedirectedNOQA) { |
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.
Hmm. The behavior here might be different? It explicitly checked if the rule is disabled in the per file ignores (not sure why?)
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 cloned the airflow repo and I'm not getting any hits for PLC0415 with this branch locally. Hopefully it will go away when I push another commit. I thought enabled
should have that folded in now, but maybe there is a subtle difference.
We override the rule selection for airflow to all, see ruff/python/ruff-ecosystem/ruff_ecosystem/defaults.py Lines 23 to 26 in 10a78fb
You can also see the command in the ecosystem results:
|
crates/ruff_linter/src/linter.rs
Outdated
let use_imports = !directives.isort.skip_file | ||
&& settings | ||
.rules | ||
&& diagnostics |
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.
Nit: This reads a bit strange. It gives the impression that we iterate over diagnostics (the enabled diagnostics).
I'm inclined to rename diagnostics
to context
, and rename all methods to is_rule_enabled
, iter_enabled_rules
...
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 agree, I'm not sure why I called this diagnostics
in the first place...
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.
Should I rename Checker::enabled
and Checker::any_enabled
too? I was trying to be consistent with those for the method names.
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 think so. It's not checker
that's enabled, it's whether a rule is enabled.
Thanks, I can reproduce this locally now. There are 2579 hits for PLC0415 on 0.12, so I'll try to see if there's anything different about the 7 new ones. |
I think the Airflow hits have to do with PLC0415 checking if TID253 is enabled: ruff/crates/ruff_linter/src/rules/pylint/rules/import_outside_top_level.rs Lines 65 to 69 in beff9c7
Airflow does have per-file ignores for TID253 that cover all of the new hits. It seems like That also explains why I wasn't reproducing this with just |
This does sounds plossible and, judging by the comment, the new behavior seems more correct |
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.
Nice!
source_file: &'a SourceFile, | ||
settings: &'a LinterSettings, | ||
source_file: SourceFile, | ||
rules: RuleTable, |
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 found it somewhat convenient that LintContext
stores the settings, as I think it will allow us to pass LintContext
in most places where we currently pass Checker
.
But I guess it makes sense not to store the settings if we also store them on Checker
, although I think we should just remove it from Checker
. Either way. This seems unrelated to this PR and not urgent but maybe something for a contributor issue.
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.
That makes sense. I just thought it might be confusing to have access to a RulesTable
through either self.settings.rules
or just self.rules
. These fields are all private anyway, though.
If we want to keep the settings on the context, it might be convenient to partially revert the removal here, otherwise someone will have to plumb all the lifetimes back through. The rest sounds like a good follow-up like you said.
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.
Okay I restored the settings field with an expect(unused)
and a TODO, just to preserve all of the lifetimes for a follow-up. I can open an issue too and possibly tag it help-wanted.
a0eddd8
to
deb304e
Compare
* main: (21 commits) [`flake8-logging`] Avoid false positive for `exc_info=True` outside `logger.exception` (`LOG014`) (#18737) [`flake8-pie`] Small docs fix to `PIE794` (#18829) [`pylint`] Ignore __init__.py files in (PLC0414) (#18400) Avoid generating diagnostics with per-file ignores (#18801) [`flake8-simplify`] Fix false negatives for shadowed bindings (`SIM910`, `SIM911`) (#18794) [ty] Fix panics when pulling types for `ClassVar` or `Final` parameterized with >1 argument (#18824) [`pylint`] add fix safety section (`PLR1714`) (#18415) [Perflint] Small docs improvement to `PERF401` (#18786) [`pylint`] Avoid flattening nested `min`/`max` when outer call has single argument (`PLW3301`) (#16885) [`ruff`] Added `cls.__dict__.get('__annotations__')` check (`RUF063`) (#18233) [ty] Use `HashTable` in `PlaceTable` (#18819) docs: Correct collections-named-tuple example to use PascalCase assignment (#16884) [ty] ecosystem-analyzer workflow (#18719) [ty] Add support for `@staticmethod`s (#18809) unnecessary_dict_kwargs doc - a note on type checking benefits (#18666) [`flake8-pytest-style`] Mark autofix for `PT001` and `PT023` as unsafe if there's comments in the decorator (#18792) [ty] Surface matched overload diagnostic directly (#18452) [ty] Report when a dataclass contains more than one `KW_ONLY` field (#18731) [`flake8-pie`] Add fix safety section to `PIE794` (#18802) [`pycodestyle`] Add fix safety section to `W291` and `W293` (#18800) ...
Summary
This PR avoids one of the three calls to
NoqaCode::rule
from #18391 by applying per-file ignores in theLintContext
. To help with this, it also replaces all direct uses ofLinterSettings.rules.enabled
with aLintContext::enabled
(orChecker::enabled
, which defers to its context) method. There are still some direct accesses tosettings.rules
, but as far as I can tell these are not in a part of the code where we can really access aLintContext
. I believe all of the code reachable fromcheck_path
, where the replaced per-file ignore code was, should be converted to the new methods.Test Plan
Existing tests, with a single snapshot updated for RUF100, which I think actually shows a more accurate diagnostic message now.