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 math-constant (FURB152) #8727

Merged
merged 4 commits into from
Nov 17, 2023

Conversation

siiptuo
Copy link
Contributor

@siiptuo siiptuo commented Nov 16, 2023

Summary

Implements FURB152 that checks for literals that are similar to constants in math module, for example:

A = 3.141592 * r ** 2

Use instead:

A = math.pi * r ** 2

Related to #1348.

Test Plan

Tested with few test cases.

Comment on lines 59 to 62
Ok(Fix::unsafe_edits(
Edit::range_replacement(binding, literal.range()),
[edit],
))
Copy link
Member

Choose a reason for hiding this comment

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

Why is this an unsafe fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most likely it changes result of calculations. For example, math.pi is more precise than 3.14.

Copy link
Member

@zanieb zanieb Nov 16, 2023

Choose a reason for hiding this comment

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

I'm unsure if that means we need to mark it as unsafe, I could go either way though. @charliermarsh?

If we do leave it as unsafe, we should explain why in the rule docs.

Copy link
Member

Choose a reason for hiding this comment

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

Hah, this is an interesting case. I guess I'd lean towards unsafe since it could break tests, etc., if you had hard-coded expectations that differed slightly?

Copy link
Member

Choose a reason for hiding this comment

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

I guess but tests written to check floating point math already should include tolerances. Idk I'm struggling to think of a case where this would change runtime behavior in a meaningful way.

Copy link
Member

Choose a reason for hiding this comment

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

Since it's in preview, it may be fine to just use safe and see if we get feedback.

Copy link
Member

@zanieb zanieb left a comment

Choose a reason for hiding this comment

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

Sweet! Thanks. Just the one question.

@zanieb zanieb added the rule Implementing or modifying a lint rule label Nov 16, 2023
Copy link
Contributor

github-actions bot commented Nov 16, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+159 -147 violations, +0 -0 fixes in 41 projects)

DisnakeDev/disnake (+1 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

+ disnake/enums.py:163:9: PLW3201 Bad or misspelled dunder method name `__members__`
- disnake/enums.py:163:9: PLW3201 Bad or misspelled dunder method name `__members__`. (bad-dunder-name)

apache/airflow (+13 -2 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --select ALL --preview

+ airflow/auth/managers/fab/models/__init__.py:81:9: PLW3201 Bad or misspelled dunder method name `__neq__`
- airflow/auth/managers/fab/models/__init__.py:81:9: PLW3201 Bad or misspelled dunder method name `__neq__`. (bad-dunder-name)
+ airflow/providers/databricks/operators/databricks_repos.py:105:9: PLW3201 Bad or misspelled dunder method name `__detect_repo_provider__`
- airflow/providers/databricks/operators/databricks_repos.py:105:9: PLW3201 Bad or misspelled dunder method name `__detect_repo_provider__`. (bad-dunder-name)
+ tests/core/test_otel_logger.py:268:64: FURB152 [*] Replace `3.14` with `math.pi`
+ tests/core/test_otel_logger.py:274:34: FURB152 [*] Replace `3.14` with `math.pi`
+ tests/core/test_otel_logger.py:280:64: FURB152 [*] Replace `3.14` with `math.pi`
+ tests/core/test_otel_logger.py:286:34: FURB152 [*] Replace `3.14` with `math.pi`
+ tests/core/test_otel_logger.py:290:64: FURB152 [*] Replace `3.14` with `math.pi`
+ tests/core/test_otel_logger.py:298:34: FURB152 [*] Replace `3.14` with `math.pi`
... 6 additional changes omitted for rule FURB152
... 5 additional changes omitted for project

aws/aws-sam-cli (+1 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

+ samcli/commands/local/lib/debug_context.py:30:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`
- samcli/commands/local/lib/debug_context.py:30:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`. (bad-dunder-name)

bokeh/bokeh (+4 -3 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --select ALL --preview

+ src/bokeh/io/notebook.py:129:9: PLW3201 Bad or misspelled dunder method name `_repr_html_`
- src/bokeh/io/notebook.py:129:9: PLW3201 Bad or misspelled dunder method name `_repr_html_`. (bad-dunder-name)
+ src/bokeh/model/model.py:595:9: PLW3201 Bad or misspelled dunder method name `_repr_html_`
- src/bokeh/model/model.py:595:9: PLW3201 Bad or misspelled dunder method name `_repr_html_`. (bad-dunder-name)
+ tests/unit/bokeh/core/test_properties.py:224:23: FURB152 [*] Replace `3.14` with `math.pi`
+ tests/unit/bokeh/core/test_serialization.py:806:17: PLW3201 Bad or misspelled dunder method name `__array__`
- tests/unit/bokeh/core/test_serialization.py:806:17: PLW3201 Bad or misspelled dunder method name `__array__`. (bad-dunder-name)
... 1 additional changes omitted for rule PLW3201

freedomofpress/securedrop (+7 -7 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

+ securedrop/pretty_bad_protocol/_meta.py:227:9: PLW3201 Bad or misspelled dunder method name `__remove_path__`
- securedrop/pretty_bad_protocol/_meta.py:227:9: PLW3201 Bad or misspelled dunder method name `__remove_path__`. (bad-dunder-name)
+ securedrop/pretty_bad_protocol/_parsers.py:1007:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`
- securedrop/pretty_bad_protocol/_parsers.py:1007:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`. (bad-dunder-name)
+ securedrop/pretty_bad_protocol/_parsers.py:1115:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`
- securedrop/pretty_bad_protocol/_parsers.py:1115:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`. (bad-dunder-name)
+ securedrop/pretty_bad_protocol/_parsers.py:1313:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`
- securedrop/pretty_bad_protocol/_parsers.py:1313:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`. (bad-dunder-name)
+ securedrop/pretty_bad_protocol/_parsers.py:1417:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`
- securedrop/pretty_bad_protocol/_parsers.py:1417:9: PLW3201 Bad or misspelled dunder method name `__nonzero__`. (bad-dunder-name)
... 4 additional changes omitted for project

ibis-project/ibis (+63 -63 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

+ ibis/backends/base/__init__.py:234:9: PLW3201 Bad or misspelled dunder method name `_ipython_key_completions_`
- ibis/backends/base/__init__.py:234:9: PLW3201 Bad or misspelled dunder method name `_ipython_key_completions_`. (bad-dunder-name)
+ ibis/backends/base/__init__.py:769:9: PLW3201 Bad or misspelled dunder method name `__rich_repr__`
- ibis/backends/base/__init__.py:769:9: PLW3201 Bad or misspelled dunder method name `__rich_repr__`. (bad-dunder-name)
+ ibis/backends/base/sql/compiler/query_builder.py:241:9: PLW3201 Bad or misspelled dunder method name `__equals__`
- ibis/backends/base/sql/compiler/query_builder.py:241:9: PLW3201 Bad or misspelled dunder method name `__equals__`. (bad-dunder-name)
+ ibis/common/bases.py:105:9: PLW3201 Bad or misspelled dunder method name `__create__`
- ibis/common/bases.py:105:9: PLW3201 Bad or misspelled dunder method name `__create__`. (bad-dunder-name)
+ ibis/common/bases.py:123:9: PLW3201 Bad or misspelled dunder method name `__prohibit_inheritance__`
- ibis/common/bases.py:123:9: PLW3201 Bad or misspelled dunder method name `__prohibit_inheritance__`. (bad-dunder-name)
+ ibis/common/bases.py:156:9: PLW3201 Bad or misspelled dunder method name `__equals__`
- ibis/common/bases.py:156:9: PLW3201 Bad or misspelled dunder method name `__equals__`. (bad-dunder-name)
+ ibis/common/bases.py:159:9: PLW3201 Bad or misspelled dunder method name `__cached_equals__`
- ibis/common/bases.py:159:9: PLW3201 Bad or misspelled dunder method name `__cached_equals__`. (bad-dunder-name)
+ ibis/common/bases.py:219:9: PLW3201 Bad or misspelled dunder method name `__rich_repr__`
- ibis/common/bases.py:219:9: PLW3201 Bad or misspelled dunder method name `__rich_repr__`. (bad-dunder-name)
+ ibis/common/deferred.py:236:9: PLW3201 Bad or misspelled dunder method name `__create__`
- ibis/common/deferred.py:236:9: PLW3201 Bad or misspelled dunder method name `__create__`. (bad-dunder-name)
+ ibis/common/deferred.py:48:9: PLW3201 Bad or misspelled dunder method name `__coerce__`
- ibis/common/deferred.py:48:9: PLW3201 Bad or misspelled dunder method name `__coerce__`. (bad-dunder-name)
... 106 additional changes omitted for project

pandas-dev/pandas (+64 -64 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

+ pandas/core/arrays/arrow/array.py:593:9: PLW3201 Bad or misspelled dunder method name `__arrow_array__`
- pandas/core/arrays/arrow/array.py:593:9: PLW3201 Bad or misspelled dunder method name `__arrow_array__`. (bad-dunder-name)
+ pandas/core/arrays/arrow/array.py:597:9: PLW3201 Bad or misspelled dunder method name `__array__`
- pandas/core/arrays/arrow/array.py:597:9: PLW3201 Bad or misspelled dunder method name `__array__`. (bad-dunder-name)
+ pandas/core/arrays/arrow/extension_types.py:148:13: PLW3201 Bad or misspelled dunder method name `__arrow_ext_serialize__`
- pandas/core/arrays/arrow/extension_types.py:148:13: PLW3201 Bad or misspelled dunder method name `__arrow_ext_serialize__`. (bad-dunder-name)
+ pandas/core/arrays/arrow/extension_types.py:152:13: PLW3201 Bad or misspelled dunder method name `__arrow_ext_deserialize__`
- pandas/core/arrays/arrow/extension_types.py:152:13: PLW3201 Bad or misspelled dunder method name `__arrow_ext_deserialize__`. (bad-dunder-name)
+ pandas/core/arrays/arrow/extension_types.py:32:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_serialize__`
- pandas/core/arrays/arrow/extension_types.py:32:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_serialize__`. (bad-dunder-name)
+ pandas/core/arrays/arrow/extension_types.py:37:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_deserialize__`
- pandas/core/arrays/arrow/extension_types.py:37:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_deserialize__`. (bad-dunder-name)
+ pandas/core/arrays/arrow/extension_types.py:83:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_serialize__`
- pandas/core/arrays/arrow/extension_types.py:83:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_serialize__`. (bad-dunder-name)
+ pandas/core/arrays/arrow/extension_types.py:88:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_deserialize__`
- pandas/core/arrays/arrow/extension_types.py:88:9: PLW3201 Bad or misspelled dunder method name `__arrow_ext_deserialize__`. (bad-dunder-name)
+ pandas/core/arrays/base.py:2259:9: PLW3201 Bad or misspelled dunder method name `__array_ufunc__`
- pandas/core/arrays/base.py:2259:9: PLW3201 Bad or misspelled dunder method name `__array_ufunc__`. (bad-dunder-name)
+ pandas/core/arrays/boolean.py:102:9: PLW3201 Bad or misspelled dunder method name `__from_arrow__`
- pandas/core/arrays/boolean.py:102:9: PLW3201 Bad or misspelled dunder method name `__from_arrow__`. (bad-dunder-name)
... 108 additional changes omitted for project

sphinx-doc/sphinx (+6 -6 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

+ sphinx/ext/autodoc/mock.py:52:9: PLW3201 Bad or misspelled dunder method name `__mro_entries__`
- sphinx/ext/autodoc/mock.py:52:9: PLW3201 Bad or misspelled dunder method name `__mro_entries__`. (bad-dunder-name)
+ tests/test_ext_napoleon.py:49:9: PLW3201 Bad or misspelled dunder method name `__special_doc__`
- tests/test_ext_napoleon.py:49:9: PLW3201 Bad or misspelled dunder method name `__special_doc__`. (bad-dunder-name)
+ tests/test_ext_napoleon.py:53:9: PLW3201 Bad or misspelled dunder method name `__special_undoc__`
- tests/test_ext_napoleon.py:53:9: PLW3201 Bad or misspelled dunder method name `__special_undoc__`. (bad-dunder-name)
+ tests/test_ext_napoleon.py:57:9: PLW3201 Bad or misspelled dunder method name `__decorated_func__`
- tests/test_ext_napoleon.py:57:9: PLW3201 Bad or misspelled dunder method name `__decorated_func__`. (bad-dunder-name)
+ tests/test_ext_napoleon.py:70:9: PLW3201 Bad or misspelled dunder method name `__special_doc__`
- tests/test_ext_napoleon.py:70:9: PLW3201 Bad or misspelled dunder method name `__special_doc__`. (bad-dunder-name)
... 2 additional changes omitted for project

... Truncated remaining completed projected reports due to GitHub comment length restrictions

Changes by rule (2 rules affected)

code total + violation - violation + fix - fix
PLW3201 294 147 147 0 0
FURB152 12 12 0 0 0

@charliermarsh charliermarsh enabled auto-merge (squash) November 17, 2023 17:32
@charliermarsh charliermarsh changed the title Implement FURB152 [refurb] Implement math-constant (FURB152) Nov 17, 2023
@charliermarsh charliermarsh enabled auto-merge (squash) November 17, 2023 17:33
@charliermarsh charliermarsh merged commit 2faac1e into astral-sh:main Nov 17, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants