Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Open
Description
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
), notmy_field: T = field(default=descriptor)
. - The descriptor's
__get__
is called twice withinstance=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 withinstance=None
, then wouldn't the following be equivalent?(Clearly the first line is shorter, but more vague - descriptors are not usually used for this purpose.)-my_field: T = descriptor+my_field: T = field(default=descriptor.__get__())
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.