Skip to content

Emit assigning-non-slot for @dataclass(slots=True) classes - #11773

Open
Thanatos9404 wants to merge 1 commit into
pylint-dev:mainfrom
Thanatos9404:dataclass-slots-assigning-non-slot
Open

Thanatos9404 wants to merge 1 commit into
pylint-dev:mainfrom
Thanatos9404:dataclass-slots-assigning-non-slot

Conversation

@Thanatos9404

Copy link
Copy Markdown

Type of Changes

Type
✓ 🐛 Bug fix

Description

@dataclass(slots=True) creates __slots__ from the class's fields at runtime, so the class body has no __slots__ and _check_in_slots returned early. Assigning an undeclared attribute, as in the issue's example, raises AttributeError at runtime but wasn't reported. The same class written with an explicit __slots__ already gets assigning-non-slot, so this reuses that message rather than no-member.

For a class decorated with @dataclass(slots=True) (or @dataclasses.dataclass(slots=True)), the slots are now its annotated fields, ClassVars excluded, plus the slots of its ancestors. The rest of the check is unchanged:

  • Ancestors: if an ancestor has no slots, the instances have a __dict__, so nothing is reported.
  • Exemptions: properties, class attributes, descriptors and __setattr__ are handled as before.
  • __class__ assignments: skipped for these classes, since there is no __slots__ layout to compare.

Not covered:

  • slots=<name> or **options passed to the decorator;
  • a plain __slots__ class that inherits from a slots dataclass, which is still unchecked, as before.

The new functional test assigning_non_slot_dataclass includes the issue's example. It covers methods and __post_init__, subclasses, and a dataclass on a __slots__ base. It also covers the cases that must stay silent: declared fields, slots=False, a plain @dataclass, and an ancestor without slots. Every line it reports raises AttributeError when reached. The test fails on main and passes here, and the changed lines are fully covered, branches included.

Closes #7790

``@dataclass(slots=True)`` creates ``__slots__`` from the fields at
runtime, so the class body has none and the check returned early,
although assigning an undeclared attribute raises AttributeError.
Take the slots of such a class from its fields and those of its
ancestors.

Closes pylint-dev#7790
@Pierre-Sassoulas Pierre-Sassoulas added False Negative 🦋 No message is emitted but something is wrong with the code dataclasses labels Nov 2, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on pandas:

Changed messages:

Details
  1. redefined-variable-type:
    Moved from 505:16-505:47 to 507:16-507:43.

This comment was generated for commit 84c463a

@codecov

codecov Bot commented Nov 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.54%. Comparing base (278fc1b) to head (84c463a).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #11773      +/-   ##
==========================================
+ Coverage   94.53%   94.54%   +0.01%     
==========================================
  Files         187      187              
  Lines       21174    21199      +25     
  Branches     4698     4709      +11     
==========================================
+ Hits        20016    20043      +27     
+ Misses        690      688       -2     
  Partials      468      468              
Files with missing lines Coverage Δ
pylint/checkers/classes/class_checker.py 92.37% <100.00%> (+0.20%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dataclasses False Negative 🦋 No message is emitted but something is wrong with the code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False negative no-member for @dataclass(slots=...)

2 participants