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 yselivanov
Recipients drhagen, eric.smith, gvanrossum, ivan, lukasz.langa, yselivanov
Date 2018-09-23.18:31:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1537727502.57.0.956365154283.issue34776@psf.upfronthosting.co.za>
In-reply-to
Content
> See this for Yury's self-described "hack-ish fix we can use" until we do something better:

Actually, I think I found a better solution that doesn't require any changes to anything besides dataclasses.

Currently, dataclasses uses 'exec()' function to dynamically create methods like '__init__'.  The generated code for '__init__' needs to access MISSING and _HAS_DEFAULT_FACTORY constants from the dataclasses module.  To do that, we compile the code with 'exec()' with globals set to a dict with {MISSING, _HAS_DEFAULT_FACTORY} keys in it.  This does the trick, but '__init__.__globals__' ends up pointing to that custom dict, instead of pointing to the module's dict.

The other way around is to use a closure around __init__ to inject MISSING and _HAS_DEFAULT_FACTORY values *and* to compile the code in a proper __dict__ of the module the dataclass was defined in.  Please take a look at the PR.
History
Date User Action Args
2018-09-23 18:31:42yselivanovsetrecipients: + yselivanov, gvanrossum, ivan, eric.smith, lukasz.langa, drhagen
2018-09-23 18:31:42yselivanovsetmessageid: <1537727502.57.0.956365154283.issue34776@psf.upfronthosting.co.za>
2018-09-23 18:31:42yselivanovlinkissue34776 messages
2018-09-23 18:31:42yselivanovcreate