Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dataclass descriptor behavior inconsistent #102646

Open
Labels
stdlibPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or error
@ikonst

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), notmy_field: T = field(default=descriptor).
  2. The descriptor's__get__ is called twice withinstance=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 aClassVar?
  2. Given that the__get__ is called only during class initialization withinstance=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 exactlyare the semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp