This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients RunOrVeith, eric.smith
Date 2019-12-17.19:52:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1576612374.26.0.251059522206.issue39078@roundup.psfhosted.org>
In-reply-to
Content
The problem is that __init__ has to have a sentinel to know whether or not to call the default value. If the default were just "dict", should it call it, or is the passed in value really dict, in which case it doesn't get called?

dataclass()
class Test:
    a: int
    b: Dict[Any, Any] = field(default_factory=dict)

The generated __init__ looks like:

def __init__(self, a, b=_HAS_DEFAULT_FACTORY):
   self.a = a
   self.b = dict() if b is _HAS_DEFAULT_FACTORY else b

If it were:

def __init__(self, a, b=dict):
   self.a = a

Then what would the assignment to self.b look like? What if you instantiated an object as Test(0, dict)? You wouldn't want dict to get called. You need to differentiate between Test(0, dict) and Test(0). The former does not call b, but the latter does call b.
History
Date User Action Args
2019-12-17 19:52:54eric.smithsetrecipients: + eric.smith, RunOrVeith
2019-12-17 19:52:54eric.smithsetmessageid: <1576612374.26.0.251059522206.issue39078@roundup.psfhosted.org>
2019-12-17 19:52:54eric.smithlinkissue39078 messages
2019-12-17 19:52:54eric.smithcreate