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

[refurb] Implement repeated-global (FURB154) #11187

Merged
merged 3 commits into from
Jun 8, 2024

Conversation

alex-700
Copy link
Contributor

Summary

Implement repeated_global (FURB154) lint.
See:

Test Plan

cargo test

Copy link
Contributor

github-actions bot commented Apr 28, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+15 -0 violations, +0 -0 fixes in 3 projects; 47 projects unchanged)

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

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

+ airflow/plugins_manager.py:361:5: FURB154 [*] Use of repeated consecutive `global`
+ airflow/plugins_manager.py:422:5: FURB154 [*] Use of repeated consecutive `global`
+ airflow/plugins_manager.py:476:5: FURB154 [*] Use of repeated consecutive `global`
+ airflow/plugins_manager.py:503:5: FURB154 [*] Use of repeated consecutive `global`
+ airflow/providers/weaviate/hooks/weaviate.py:418:13: FURB154 [*] Use of repeated consecutive `nonlocal`
+ airflow/settings.py:194:5: FURB154 [*] Use of repeated consecutive `global`
+ airflow/settings.py:299:5: FURB154 [*] Use of repeated consecutive `global`
+ airflow/settings.py:426:5: FURB154 [*] Use of repeated consecutive `global`

bokeh/bokeh (+1 -0 violations, +0 -0 fixes)

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

+ tests/unit/bokeh/document/test_callbacks__document.py:168:13: FURB154 [*] Use of repeated consecutive `nonlocal`

zulip/zulip (+6 -0 violations, +0 -0 fixes)

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

+ zerver/data_import/slack.py:532:9: FURB154 [*] Use of repeated consecutive `nonlocal`
+ zerver/data_import/slack.py:604:9: FURB154 [*] Use of repeated consecutive `nonlocal`
+ zerver/lib/cache.py:63:5: FURB154 [*] Use of repeated consecutive `global`
+ zerver/lib/markdown/__init__.py:2727:5: FURB154 [*] Use of repeated consecutive `global`
+ zerver/lib/templates.py:106:5: FURB154 [*] Use of repeated consecutive `global`
+ zerver/tests/test_auth_backends.py:7644:13: FURB154 [*] Use of repeated consecutive `nonlocal`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
FURB154 15 15 0 0 0

@charliermarsh
Copy link
Member

Since globals and nonlocals are rare, could we try instead running this rule on every global and nonlocal statement, and then, for each statement, mapping back to the containing Suite and looking for global statements that follow it repeatedly?

In other words... instead of iterating over every body / statement, can we instead run this for each global? You can look at traversal::next_sibling or traversal::suite (and their usages) for an example.

@alex-700
Copy link
Contributor Author

Since globals and nonlocals are rare, could we try instead running this rule on every global and nonlocal statement, and then, for each statement, mapping back to the containing Suite and looking for global statements that follow it repeatedly?

In other words... instead of iterating over every body / statement, can we instead run this for each global? You can look at traversal::next_sibling or traversal::suite (and their usages) for an example.

@charliermarsh done.
Just a note: now the algorithm is quadratic on len(suite) for each suite containing global or nonlocal. Instead of linear (and should be auto vectorized) for each suite.

@charliermarsh charliermarsh self-assigned this Jun 4, 2024
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels Jun 4, 2024
@charliermarsh
Copy link
Member

Thanks @alex-700. Blocked on me, will review shortly.

@charliermarsh charliermarsh changed the title [refurb] implement repeated_global (FURB154) lint [refurb] Implement repeated-global (FURB154) Jun 8, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) June 8, 2024 20:30
@charliermarsh charliermarsh merged commit ccc418c into astral-sh:main Jun 8, 2024
19 checks passed
Copy link

codspeed-hq bot commented Jun 8, 2024

CodSpeed Performance Report

Merging #11187 will degrade performances by 9.38%

Comparing alex-700:latyshev/furb154 (d66b3f1) with alex-700:latyshev/furb154 (333b7f4)

Summary

⚡ 9 improvements
❌ 8 regressions
✅ 13 untouched benchmarks

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

Benchmarks breakdown

Benchmark alex-700:latyshev/furb154 alex-700:latyshev/furb154 Change
lexer[large/dataset.py] 1.4 ms 1.1 ms +28.25%
lexer[numpy/ctypeslib.py] 285.3 µs 225.8 µs +26.32%
lexer[numpy/globals.py] 36.1 µs 29.4 µs +23.08%
lexer[pydantic/types.py] 639.6 µs 502.8 µs +27.22%
lexer[unicode/pypinyin.py] 98.2 µs 76.6 µs +28.21%
linter/all-rules[large/dataset.py] 15.5 ms 16.1 ms -4.27%
linter/all-rules[unicode/pypinyin.py] 2 ms 2.1 ms -6.2%
linter/default-rules[large/dataset.py] 3.5 ms 3.7 ms -5.65%
linter/default-rules[numpy/ctypeslib.py] 879.6 µs 945.5 µs -6.97%
linter/default-rules[pydantic/types.py] 1.7 ms 1.9 ms -9.38%
linter/default-rules[unicode/pypinyin.py] 341.6 µs 359.8 µs -5.07%
linter/all-with-preview-rules[numpy/globals.py] 777 µs 812.5 µs -4.37%
linter/all-with-preview-rules[unicode/pypinyin.py] 2.2 ms 2.4 ms -6.51%
parser[large/dataset.py] 5.6 ms 5.3 ms +5.85%
parser[numpy/ctypeslib.py] 1.1 ms 1 ms +7.76%
parser[numpy/globals.py] 122.3 µs 107 µs +14.27%
parser[unicode/pypinyin.py] 372 µs 341.6 µs +8.89%

@charliermarsh
Copy link
Member

I'm confused by the CodSpeed results but my guess is it's just confused by the branch being dated? We didn't touch the parser or lexer here of course.

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.

2 participants