-
Notifications
You must be signed in to change notification settings - Fork 52
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
differences / compatibility with attrs project #60
Comments
First off, I found and read #19, which is a good read for anyone wondering whether Here is my first attempt at an overview of the differences, starting with function arguments:
Notes / Observations:
If anyone is aware of deeper functional differences, I'd love to hear them. Thanks! edit1: added notes on |
I think this is a useful exercise, thanks. I agree that it would be a shame to inadvertently miss something that's in |
As far as conversion functions and validators, I'd like to not support these. I'm hoping that static type checking gets us most of the way there. |
|
Thanks, that conversation cleared it up for me. I updated my post above with the new info.
I don't think that static type checking has much impact on the need for converters. Take something like this for instance: @attr.s
class C:
x: int = attr.ib(default=0, converter=int)
y: int = attr.ib(default=0, converter=int)
c = C('1', 1.1) This pattern is very common. A hypothetical mypy plugin for Without converters the best we can do this: @dataclass
class C:
x: int = 0
y: int = 0
c = C(int('1'), int(1.1)) Static type checking doesn't really have much to offer here in terms of ease of use: the best it can do is nag us to cast everything to As for validators, static type checking gets us part of the way there, but certainly not most of the way there. Here are some example validations:
All of these require runtime validation except the last. That said, validation can be performed in Is there an argument against adding |
I think the fact that static type checkers prohibit something like: class C:
x: int = ...
y: int = ...
c = C('1', 1.1) is rather good, not bad. What are the use cases for converters (apart form being temporary workarounds themselves)? As for validators, they can be added to As for |
@chadrik: where do you propose this documentation should go? Or is this just an exercise for the design phase, which I think has ended. It's not appropriate for this to go in the stdlib documentation. |
I think that attrs users are most definitely going to want this information
once this project makes it into the stdlib. How about adding it to the wiki
for now? I’ll gladly keep it up to date. I also want to use it to lobby for
certain changes to attrs to increase compatibility (e.g. order vs cmp
behavior).
…On Fri, Dec 1, 2017 at 10:44 AM Eric V. Smith ***@***.***> wrote:
metadata has been added.
@chadrik <https://github.com/chadrik>: where do you propose this
documentation should go? Or is this just an exercise for the design phase,
which I think has ended. It's not appropriate for this to go in the stdlib
documentation.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#60 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAD3E6j_nnE9uTo_770gRS0E_P-o0CTgks5s8B7rgaJpZM4QO1z0>
.
|
Either the Wiki (which I have no access to) or maybe under And note that you can use |
Also, note that As for |
I think that the "return a new class" approach is fundamentally incompatible with metaclasses and especially PEP 487. Since there is no way to add slots to an existing class, I'm considering a different API for slot classes in attrs too. Or, you know, Python could grow a better |
Actually we should design a new slots interface. The original was designed
before we had class decorators.
…On Dec 2, 2017 12:17 PM, "Tin Tvrtković" ***@***.***> wrote:
I think that the "return a new class" approach is fundamentally
incompatible with metaclasses and especially PEP 487. Since there is no way
to add slots to an existing class, I'm considering a different API for slot
classes in attrs too. Or, you know, Python could grow a better __slots__
interface itself, but I'm not holding my breath.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#60 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACwrMvziqyw8W_mLHtcHaBa-2yW1IKRLks5s8bA6gaJpZM4QO1z0>
.
|
Yes please! |
That won't be easy though -- it means that the instance layout has to be made changeable after the class object has been created (which happens when the metaclass creates it -- before the class decorator runs). Mayby there are some folks on python-ideas interested in brainstorming on how to do this. |
One last effort on this topic:
What if say, over half of the uses of |
I think such situations are relatively rare (like legacy API or similar). And IIUC this use case is covered by a combination of @dataclass
class C:
a: str
b: str = field(init=False)
_b: InitVar[bytes]
def __post_init__(self, _b) -> None:
self.b = convert_from_legacy_api(_b)
aa: str = 'a test'
bb: bytes = b'b test'
c = C(aa, bb) # OK And this will work well with static type checkers. |
Indeed :-) Fixed! |
I think there's nothing else to add here. Closing this issue. |
I honestly don't see how the dummy InitVar + extra var + post_init is a Pythonic replacement to the simple and clean converter. And it's also, as far as I can tell, not a solution for frozen dataclasses. Take this very simple and common dataclass
How do you insure names is not mutable itself? Normally, a simpler |
It’s unpythonic to expect “deep” frozen-ness. A frozen object disallows attribute assignment but doesn’t care about modifying attribute values. |
It would be helpful to have a list of functional differences between
dataclasses
andattrs
, broken down by@dataclass
vs@attr.s
andfield
vsattr.ib
.This would be useful and illuminating for a few reasons:
It would make it easier to vet the logic behind, and need for, each of the proposed differences.
@hynek and @Tinche have invested years of thought into the current design: deviating from it without fully understanding the history and reasoning behind each decision might lead to this project needlessly repeating mistakes. I'm glad to see that the
attrs
devs have already been brought into several issues. My hope is we can get a bird's eye view so that nothing slips through the cracks.If the differences aren't too great (and ideally they will not be, see above) I'd like to see a
dataclass
compatibility mode forattrs
(e.g.from attrs import dataclass, field
).I'm glad that this badly-needed feature is being worked on, but sadly I'm stuck in python 2 for at least another 2 years, so it's important to me, and surely many
attrs
-users, to have an easy path to adoption once this becomes part of stdlib.The text was updated successfully, but these errors were encountered: