-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
slotted dataclasses not calling descriptor field __set__
#132946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
cc @ericvsmith |
My wild guess: maybe this happens because we recreate |
Only the finest issues for my two favorite python features 🙃 |
From https://docs.python.org/2/reference/datamodel.html?highlight=__slots__#__slots__: slots are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by slots; otherwise, the class attribute would overwrite the descriptor assignment. It might be possible to detect this and raise an error, if someone wants to look into it. |
I found one more problem related to descriptors in class DescriptorFieldType:
def __get__(self, instance, owner):
print(instance, owner)
if instance is None:
return 1
return 2
@dataclass
class Regular:
one: DescriptorFieldType = DescriptorFieldType()
assert Regular.one == 1
assert Regular().one == 2 # fails, is actually `1` I propose to discus this in a separate issue. |
I tried to consider if
Since this isn't a dataclasses bug, feel free to close. (The alternative being raise an error as @ericvsmith suggests) |
Ah, wait I think raising an error is the right way to go: # This declaration doesn't error
@dataclass(slots=True)
class SlottedDataclass:
name: Descriptor = Descriptor()
# This declaration errors
class Slotted:
__slots__ = ("name",)
name = Descriptor() |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
(Sorry to be finding these funny little dudes)
Will error with:
(errors on
Unslotted
but notSlotted
)CPython versions tested on:
3.11
Operating systems tested on:
No response
Linked PRs
@dataclass(slots=True)
#133002The text was updated successfully, but these errors were encountered: