bpo-34213: frozen dataclass with "object" attr bug by VadimPushtaev · Pull Request #8452 · python/cpython (original) (raw)

Looks like is misunderstood the __builtins__ problem.

From what I got, __builtins__ may be a reference to the builtins module or to its __dict__. All imported modules have __builtins__ equal to __dict__ while a program that is directly run has __builtins__ equal to the builtins module.

For example, if I run make test that __builtins__ is a dictionary and you can't do __builtins__.__dict__, but it's completely fine as long as I run the test individually.

That is more or less described in the documentation — https://docs.python.org/3/library/builtins.html

Next, the default for any code in exec depends on whether you provide the globals argument or not:

This behavior is described here — https://docs.python.org/3/library/functions.html#exec


Having all the in mind, I concluded that we should use builtins instead of __builtins__ wherever possible. However, the generated code can't do that since builtins can still be shadowed by an argument name. So the solution is to use __builtins__ but make sure it's a reference to the module, not to its __dict__.