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:
- It only works when the descriptor is assigned through the "Pythonic syntax" (
my_field: T = descriptor), not my_field: T = field(default=descriptor).
- The descriptor's
__get__ is called twice with instance=None, but not called for instance attribute gets.
- 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.
- What is the merit of this, vs. a property or a descriptor assigned as a
ClassVar?
- 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.
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:
my_field: T = descriptor), notmy_field: T = field(default=descriptor).__get__is called twice withinstance=None, but not called for instance attribute gets.__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.
ClassVar?__get__is called only during class initialization withinstance=None, then wouldn't the following be equivalent?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.