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

ruff-mutable-default (RUF008) not not catching mutable default passed to attrs.field. #16495

Open
ashb opened this issue Mar 4, 2025 · 6 comments
Labels
help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@ashb
Copy link
Contributor

ashb commented Mar 4, 2025

Simple repro case

❯ cat tst.py
import attrs

@attrs.define
class B :
    mutable_default: list[int] = []
    mutable_default2: list[int] = attrs.field(default=[])

❯ uvx [email protected] check --extend-select RUF008,B006 --preview -n --isolated  tst.py
All checks passed!

Originally posted by @ashb in #14327 (comment)

Output:

tst.py:5:34: RUF008 Do not use mutable default values for dataclass attributes
  |
3 | @attrs.define
4 | class B :
5 |     mutable_default: list[int] = []
  |                                  ^^ RUF008
6 |     mutable_default2: list[int] = attrs.field(default=[])
  |

I was hoping it would catch the same error on mutable_default2

@MichaReiser
Copy link
Member

Can you tell me more about what diagnostics you'd expect?

E.g. this raises RUF012 for the mutable_default field https://play.ruff.rs/98850596-5ef1-4a1e-a3e5-fa07ed757a81

@MichaReiser MichaReiser added the needs-info More information is needed from the issue author label Mar 4, 2025
@AlexWaygood

This comment has been minimized.

@MichaReiser

This comment has been minimized.

@MichaReiser
Copy link
Member

Shouldn't it be @attrs.define instead of @attrs.deinfe?

@ashb
Copy link
Contributor Author

ashb commented Mar 4, 2025

🤦🏻 Okay, so that fixes the first one, but not the second. I guess it's up for debate if the second should be caught or not.

Updated top post

@ashb ashb changed the title ruff-mutable-default (RUF008) not working for attrs classes ruff-mutable-default (RUF008) not not catching mutable default passed to attrs.field. Mar 4, 2025
@MichaReiser MichaReiser added rule Implementing or modifying a lint rule help wanted Contributions especially welcome no-test Disable CI tests for a pull request and removed needs-info More information is needed from the issue author labels Mar 4, 2025
@MichaReiser
Copy link
Member

MichaReiser commented Mar 4, 2025

Thanks for updating the summary. Adding support for attrs.field (in preview) does make sense to me, considering that #14327 (comment) introduced attrs support.

@MichaReiser MichaReiser removed the no-test Disable CI tests for a pull request label Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants