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] Implicit class variable in dataclass (RUF045) #14349

Merged
merged 11 commits into from
Feb 15, 2025

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Resolves #12877.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Nov 15, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+8 -0 violations, +0 -0 fixes in 4 projects; 51 projects unchanged)

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

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

+ airflow/executors/base_executor.py:89:5: RUF045 Assignment without annotation found in dataclass body
+ dev/breeze/src/airflow_breeze/params/doc_build_params.py:36:5: RUF045 Assignment without annotation found in dataclass body

latchbio/latch (+3 -0 violations, +0 -0 fixes)

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

+ src/latch_cli/constants.py:31:5: RUF045 Assignment without annotation found in dataclass body
+ src/latch_cli/constants.py:32:5: RUF045 Assignment without annotation found in dataclass body
+ src/latch_cli/constants.py:39:5: RUF045 Assignment without annotation found in dataclass body

python/typeshed (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select E,F,FA,I,PYI,RUF,UP,W

+ stubs/fpdf2/fpdf/fonts.pyi:33:5: RUF045 Assignment without annotation found in dataclass body

scikit-build/scikit-build-core (+2 -0 violations, +0 -0 fixes)

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

+ src/scikit_build_core/file_api/model/codemodel.py:129:5: RUF045 Assignment without annotation found in dataclass body
+ src/scikit_build_core/file_api/model/directory.py:23:5: RUF045 Assignment without annotation found in dataclass body

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF045 8 8 0 0 0

@InSyncWithFoo
Copy link
Contributor Author

InSyncWithFoo commented Nov 15, 2024

False positives found by ecosystem checks:

stubs/fpdf2/fpdf/fonts.pyi (@python/typeshed):

@dataclass
class FontFace:
    family: str | None
    emphasis: TextEmphasis | None
    size_pt: int | None
    color: DeviceGray | DeviceRGB | None
    fill_color: DeviceGray | DeviceRGB | None

    ...

    replace = dataclasses.replace

src/_pytest/fixtures.py (@pytest-dev/pytest):

@dataclasses.dataclass(frozen=True)
class FuncFixtureInfo:
    ...

    __slots__ = ("argnames", "initialnames", "names_closure", "name2fixturedefs")

Special names can be exempted, but I'm not sure what to do about the first.

@dhruvmanila dhruvmanila added rule Implementing or modifying a lint rule preview Related to preview mode features labels Nov 18, 2024
@MichaReiser
Copy link
Member

I'm not saying we shouldn't do it, but requiring type annotations has been controversial in other instances; for example, there's a long thread for RUF012 that does something similar. @AlexWaygood I'd be interested on your take on this before I start reviewing the rule itself.

@dylwil3 dylwil3 removed the needs-decision Awaiting a decision from a maintainer label Feb 15, 2025
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Thank you!

Looking at the ecosystem results, it seems fairly common for users to define a private class variable in their dataclasses. So I've restricted the scope of the rule a little to exempt that.

@dylwil3 dylwil3 merged commit 3c69b68 into astral-sh:main Feb 15, 2025
21 checks passed
@InSyncWithFoo InSyncWithFoo deleted the RUF045 branch February 16, 2025 07:06
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 un-annotated attributes in dataclass definition
5 participants