Skip to content

[ruff] Implement mutable-fromkeys-value (RUF024)#9597

Merged
charliermarsh merged 4 commits intoastral-sh:mainfrom
tjkuson:mutable-fromkeys-value
Jan 22, 2024
Merged

[ruff] Implement mutable-fromkeys-value (RUF024)#9597
charliermarsh merged 4 commits intoastral-sh:mainfrom
tjkuson:mutable-fromkeys-value

Conversation

@tjkuson
Copy link
Contributor

@tjkuson tjkuson commented Jan 21, 2024

Summary

Implement rule mutable-fromkeys-value (RUF023).

Autofixes

dict.fromkeys(foo, [])

to

{key: [] for key in foo}

The fix is marked as unsafe as it changes runtime behaviour. It also uses key as the comprehension variable, which may not always be desired.

Closes #4613.

Test Plan

cargo test

@tjkuson tjkuson force-pushed the mutable-fromkeys-value branch from 58bf244 to e9410b9 Compare January 21, 2024 15:15
Implement rule `mutable-fromkeys-value` (`RUF023`) with a fix marked as
unsafe.

Fix formatting in docs
@tjkuson tjkuson force-pushed the mutable-fromkeys-value branch from e9410b9 to 6cd8382 Compare January 21, 2024 15:16
@tjkuson tjkuson marked this pull request as ready for review January 21, 2024 15:20
@github-actions
Copy link
Contributor

github-actions bot commented Jan 21, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood
Copy link
Member

Could you maybe use RUF024 for this? I'm using RUF023 for #9564 :)

@tjkuson
Copy link
Contributor Author

tjkuson commented Jan 21, 2024

Oops, I will change it when I am back at my desk

@Skylion007
Copy link
Contributor

Can you also open an issue on this for flake8-bugbear? I'm sure they'd love to include this rule and it would make more sense with a reserved B error code. Other ruff rule suggestions have been added there and we usually migrate the rule alias.

@Skylion007
Copy link
Contributor

Ah nvm, there is an issue that has been open for a while: PyCQA/flake8-bugbear#387

@AlexWaygood AlexWaygood changed the title Implement rule mutable-fromkeys-value (RUF023) Implement rule mutable-fromkeys-value (RUF024) Jan 21, 2024
@tjkuson
Copy link
Contributor Author

tjkuson commented Jan 21, 2024

Thanks for renaming the PR @AlexWaygood, didn't realise I had missed that

@charliermarsh charliermarsh self-requested a review January 21, 2024 20:34
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.

Excellent work, thank you.

@charliermarsh charliermarsh enabled auto-merge (squash) January 22, 2024 00:10
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels Jan 22, 2024
@charliermarsh charliermarsh changed the title Implement rule mutable-fromkeys-value (RUF024) [ruff] Implement mutable-fromkeys-value (RUF024) Jan 22, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) January 22, 2024 00:10
@charliermarsh charliermarsh merged commit 1e4b421 into astral-sh:main Jan 22, 2024
@tjkuson tjkuson deleted the mutable-fromkeys-value branch January 22, 2024 09:35
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.

Warn about dict.fromkeys(keys, mutable)

4 participants