Skip to content

gh-145369: Warn against reusing dataclass field objects#145373

Closed
gourijain029-del wants to merge 1 commit intopython:mainfrom
gourijain029-del:gh-145369-dataclass-field-reuse-warning
Closed

gh-145369: Warn against reusing dataclass field objects#145373
gourijain029-del wants to merge 1 commit intopython:mainfrom
gourijain029-del:gh-145369-dataclass-field-reuse-warning

Conversation

@gourijain029-del
Copy link

@gourijain029-del gourijain029-del commented Feb 28, 2026

Adds a warning to the dataclasses documentation explaining that field objects should not be reused across multiple fields or classes.

The issue is that field() returns an object that gets modified in place by the @DataClass decorator. If you reuse the same field object, it becomes corrupted and causes the class to behave incorrectly.

The warning includes:

  • Clear explanation of why reusing is problematic
  • Example showing the incorrect pattern
  • Example showing the correct approach

Fixes #145369


📚 Documentation preview 📚: https://cpython-previews--145373.org.readthedocs.build/

…reused

Add warning to dataclasses documentation explaining that Field objects
returned by field() should not be reused across multiple fields or
classes, as they are modified in place by the decorator.
@picnixz
Copy link
Member

picnixz commented Feb 28, 2026

  • A warning is too loud.
  • This shouldn't be documented in the dataclass section IMO. It's the regular behavior.
  • Please read https://devguide.python.org/getting-started/generative-ai/ and avoid using language that may convene hestiation.
  • Please don't open a PR unless an issue has been triaged by a triager or a maintainer.

@picnixz picnixz closed this Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Document dataclass field should not be reused

2 participants