Skip to content

[ruff] Implicit class variable in dataclass (RUF045) - #14349

Merged
dylwil3 merged 11 commits into
astral-sh:mainfrom
InSyncWithFoo:RUF045
Feb 15, 2025
Merged

dylwil3 merged 11 commits into
astral-sh:mainfrom
InSyncWithFoo:RUF045

Conversation

@InSyncWithFoo

Copy link
Copy Markdown
Contributor

Summary

Resolves #12877.

Test Plan

cargo nextest run and cargo insta test.

@github-actions

github-actions Bot commented Nov 15, 2024 •

Copy link
Copy Markdown
Contributor

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

InSyncWithFoo commented Nov 15, 2024 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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

@dylwil3 dylwil3 left a comment

Copy link
Copy Markdown
Collaborator

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.

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