Skip to content
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

[pygrep_hooks] Check blanket ignores via file-level pragmas (PGH004) #11540

Merged
merged 11 commits into from
Jun 4, 2024

Conversation

ajesipow
Copy link
Contributor

Summary

Should resolve #11454.

This is my first PR to ruff, so I may have missed something.

If I understood the suggestion in the issue correctly, rule PGH004 should be set to Preview again.

Test Plan

Created two fixtures derived from the issue.

Copy link
Contributor

github-actions bot commented May 25, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+5 -0 violations, +0 -0 fixes in 4 projects; 46 projects unchanged)

apache/airflow (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ docs/exts/exampleinclude.py:1:1: PGH004 Use specific rule codes when using `ruff: noqa`

indico/indico (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ indico/legacy/pdfinterface/base.py:8:1: PGH004 Use specific rule codes when using `ruff: noqa`
+ indico/legacy/pdfinterface/conference.py:8:1: PGH004 Use specific rule codes when using `ruff: noqa`

pytest-dev/pytest (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ testing/code/test_source.py:2:1: PGH004 Use specific rule codes when using `ruff: noqa`

pdm-project/pdm (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ src/pdm/installers/__init__.py:1:1: PGH004 Use specific rule codes when using `ruff: noqa`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PGH004 5 5 0 0 0

@JonathanPlasse
Copy link
Contributor

The stable behavior should be unchanged, the added behavior should only be present in preview.
i.e. The ruff ecosystem results should not change for stable, only preview should have additional violations.
Otherwise, the users of stable will lose the PGH004 rule.

@ajesipow
Copy link
Contributor Author

Thanks for the swift feedback.

I added a check for PreviewMode::Enabled and added tests for the preview case.

Would you like me to also update the docs for BlanketNOQA?

@zanieb zanieb self-requested a review May 25, 2024 17:35
@@ -85,7 +86,19 @@ pub(crate) fn blanket_noqa(
diagnostics: &mut Vec<Diagnostic>,
noqa_directives: &NoqaDirectives,
locator: &Locator,
exemption: &Option<FileExemption>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One downside here is that this is only going to flag the first # ruff: noqa in the file. I assume if you add multiple such comments, only the first is flagged as a diagnostic?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to do something like NoqaDirectives, whereby we parse all of these upfront, then use the parsed exemptions in crates/ruff_linter/src/checkers/noqa.rs (and here).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajesipow - Do you want to give that a try?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ping @charliermarsh.
I haven't found the time yet after work this week, but today is a public holiday over here so I'll look into it today :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed the changes and adapted the tests again accordingly.

Looking forward to your feedback again.

@charliermarsh charliermarsh self-assigned this May 28, 2024
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels May 28, 2024
/// The file is exempt from all rules.
All,
/// The file is exempt from the given rules.
Codes(Vec<NoqaCode>),
Codes(Vec<&'a NoqaCode>),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this as a Vec, but we could consider using a HashSet instead as we're checking if certain NoqaCodes are contained in a few places (one being the loop on line 54 in checkers/noqa.rs).

It's probably fine though as I'd expect there will usually only be very few elements here and checking the small Vec is likely faster compared to the overhead of hashing every time (and building the HashSet in the first place).
We'd also have to derive Hash on NoqaCode.

@charliermarsh
Copy link
Member

Thanks @ajesipow, I will take a look.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@charliermarsh charliermarsh changed the title [pygrep_hooks] Check file-level pragmas (PGH004) [pygrep_hooks] Check blanket ignores via file-level pragmas (PGH004) Jun 4, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) June 4, 2024 03:41
@charliermarsh charliermarsh merged commit b56a577 into astral-sh:main Jun 4, 2024
18 checks passed
Copy link

codspeed-hq bot commented Jun 4, 2024

CodSpeed Performance Report

Merging #11540 will degrade performances by 5.63%

Comparing ajesipow:pgh004-file-level (4e80490) with ajesipow:pgh004-file-level (8574036)

Summary

❌ 1 regressions
✅ 29 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark ajesipow:pgh004-file-level ajesipow:pgh004-file-level Change
linter/all-rules[unicode/pypinyin.py] 2 ms 2.1 ms -5.63%

carljm added a commit that referenced this pull request Jun 4, 2024
* main:
  CI: add job to run tests under minimum supported rust version (msrv) (#11737)
  Lexer should consider BOM for the start offset (#11732)
  Use cursor offset for lexer checkpoint (#11734)
  red-knot: Change `resolve_global_symbol` to take `Module` as an argument (#11723)
  red-knot: Use `parse_unchecked` to get all parse errors (#11725)
  Respect per-file ignores for blanket and redirected noqa rules (#11728)
  [`pygrep_hooks`] Check blanket ignores via file-level pragmas (`PGH004`) (#11540)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PGH004 (blanket-noqa) doesn't checks file-level pragmas
3 participants