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

Add advice for fixing RUF008 when mutability is not desired #8853

Merged

Conversation

samueljsb
Copy link
Contributor

@samueljsb samueljsb commented Nov 27, 2023

Summary

A common mistake in Python code is creating global mutable state by assigning class attributes that are mutable (e.g. a list or dictionary). RUF012 helpfully catches this mistake and can be used to prevent inadvertent global state form being created.

The description of that rule helpfully provides advice for adjusting code to stop beigng reported as an error if the attribute should be mutable, but does not say how the error can be avoided when mutability is not the goal.

This change adds that advice, so that both possibilities are accounted for.

Test Plan

Docs built and inspected visually:

Screenshot 2023-11-27 at 14 30 02

Copy link
Contributor

github-actions bot commented Nov 27, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@samueljsb samueljsb force-pushed the mutable-class-default-immutable-advice branch from 3d1b1f2 to b319d4a Compare November 27, 2023 14:48
A common mistake in Python code is creating global mutable state by
assigning class attributes that are mutable (e.g. a list or dictionary).
`RUF012` helpfully catches this mistake and can be used to prevent
inadvertent global state form being created.

The description of that rule helpfully provides advice for adjusting
code to stop beigng reported as an error *if the attribute should be
mutable*, but does not say how the error can be avoided when mutability
is not the goal.

This change adds that advice, so that both possibilities are accounted
for.
@samueljsb samueljsb force-pushed the mutable-class-default-immutable-advice branch from b319d4a to 80221e4 Compare November 27, 2023 15:00
@zanieb zanieb added the documentation Improvements or additions to documentation label Nov 27, 2023
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.

Thanks!

@zanieb zanieb merged commit 1f14d9a into astral-sh:main Nov 27, 2023
17 checks passed
@samueljsb samueljsb deleted the mutable-class-default-immutable-advice branch November 27, 2023 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants