Skip to content

Dataclass descriptor behavior inconsistent #102646

Description

@ikonst

In #94424, the behavior of dataclasses for a descriptor that was assigned as a field default was defined and documented.

While I might be wrong here, a number of things lead me to believe it was an "accidental feature" being documented:

  1. It only works when the descriptor is assigned through the "Pythonic syntax" (my_field: T = descriptor), not my_field: T = field(default=descriptor).
  2. The descriptor's __get__ is called twice with instance=None, but not called for instance attribute gets.
  3. Inconsistently with that, __set__ is called for each instance attribute assignment.

We can probably address those with some amount of effort, but first I'd like to make sure this behavior is intentional and desired.

  1. What is the merit of this, vs. a property or a descriptor assigned as a ClassVar?
  2. Given that the __get__ is called only during class initialization with instance=None, then wouldn't the following be equivalent?
    -my_field: T = descriptor
    +my_field: T = field(default=descriptor.__get__())
    (Clearly the first line is shorter, but more vague - descriptors are not usually used for this purpose.)

Motivation: python/mypy#14869 brought up mypy's dataclasses plugin not properly understanding the finer semantics of assigning a descriptor, which led me to look at what exactly are the semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions