-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Description
This is not a bug that I encountered in the wild, but I happened to notice the possibility for this while reading the source.
from dataclasses import dataclass
@dataclass(frozen=True)
class X:
BUILTINS: str
X(5)
will give you:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 3, in __init__
AttributeError: 'int' object has no attribute 'object'
There are other field names that you can use to trick dataclass into bad behaviour, but all of the other ones are prefixed by an underscore.
So one "fix" is pretty simple: just rename BUILTINS to _BUILTINS in dataclasses.py to bring it in line with the others.
If a single underscore doesn't feel like sufficient "this is the user's fault", we could use __dataclass_* (like we do for __dataclass_self__ param to __init__)
For what it's worth, the only thing we need BUILTINS for is so we can access object inside __init__, so an alternative fix could be to use ().__class__.__base__ instead of BUILTINS.object (and skip the locals dance needed to define BUILTINS altogether)
If any of that sounds worth doing, I'd be happy to open a PR