Message 326170 - Python tracker (original) (raw)
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.