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
Special handling of descriptor field like in dataclasses #1232
Labels
Comments
I was wondering when they added it, because I didn't have it on my radar at all, but it looks like 3.10 – if the docs are to be trusted (3.9 doesn't mention descriptors at all). Does anyone have a clue how much work it would be to integrate this? |
I checked the source code of `dataclasses` and the only mention of descriptors is in this line:
https://github.com/python/cpython/blob/718dbd4cd33a204e12fb747a09b73c4c4ac1b55f/Lib/dataclasses.py#L349
Maybe defining the method `__set_name__` is enough to instantiate the descriptor and make it work.
…On 3 February 2024 08:02:38 GMT+01:00, Hynek Schlawack ***@***.***> wrote:
I was wondering when they added it, because I didn't have it on my radar at all, but it looks like 3.10 – if the docs are to be trusted (3.9 doesn't mention descriptors at all).
Does anyone have a clue how much work it would be to integrate this?
--
Reply to this email directly or view it on GitHub:
#1232 (comment)
You are receiving this because you authored the thread.
Message ID: ***@***.***>
|
I'm in favor of fixing this on philosophical grounds (I'm bothered by dataclasses doing a thing better than us) ;) |
Yes, it's very irritating. |
Maybe it's not too hard to implement, because annotating the descriptor as `ClassVar` is enough to make it work:
```python
# attrs: THIS WORKS
from typing import ClassVar
@define(slots=False)
class InventoryItem3:
quantity_on_hand: ClassVar[IntConversionDescriptor] = IntConversionDescriptor(default=100)
i3 = InventoryItem3()
print(i3.quantity_on_hand) # 100
i3.quantity_on_hand = 2.5
print(i3.quantity_on_hand) # 2
```
…On 4 February 2024 18:04:49 GMT+01:00, Hynek Schlawack ***@***.***> wrote:
Yes, it's very irritating.
--
Reply to this email directly or view it on GitHub:
#1232 (comment)
You are receiving this because you authored the thread.
Message ID: ***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dataclass
dataclasses handle descriptor fields differently from other fields, so the descriptor type is not lost when assigning:https://docs.python.org/3/library/dataclasses.html#descriptor-typed-fields
This does not work in
attrs
, it's a pity because it could be used as an alternative toconverters
called with the instance (#1108).Previous discussion about this was not very conclusive (#881).
The text was updated successfully, but these errors were encountered: