-
-
Notifications
You must be signed in to change notification settings - Fork 382
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
Find better name for attr.dataclass #408
Comments
|
That’s kinda my favorite too. It’s just sad that it doesn’t work with the attr prefix but that ship has somewhat sailed. I think I’ll have to re-visit the introduction of an |
Which opens the question: if the namespace becomes The world around While I realize that introducing new APIs brings some confusion, there’s always the cautionary tale of why Makefiles use tab characters. After using it a bit, I believe that if Python has a future, static typing is unequivocally part of it and I want I for one, am very annoyed whenever I use the |
So I’ve been playing and I’m absolutely in love with this being the future canonical API for >>> import attrs
>>> import typing
>>> @attrs.auto
... class SomeClass:
... a_number: int = 42
... list_of_numbers: typing.List[int] = attrs.Factory(list)
... a_number = attrs.attrib(converter=int)
>>> attrs.fields(SomeClass).a_number.type
<class 'int'> Which kind of means that the question is reversed – should there be another name for That’s the only thing that annoys me but I don’t think we can fix it: it’s impossible to both Which gives us: >>> from attrs import attrs, attrib, Factory
>>> @attrs
... class SomeClass(object):
... a_number = attrib(default=42)
... list_of_numbers = attrib(default=Factory(list))
>>> sc = SomeClass(1, [1, 2, 3])
>>> attrs.asdict(sc)
{'a_number': 1, 'list_of_numbers': [1, 2, 3]}
>>> C = attrs.make_class("C", ["a", "b"])
>>> C("foo", "bar")
C(a='foo', b='bar') Which I think I can live with? At least I don’t have a better idea. One thing that’s notable tho and that makes me feel quite good about |
I think it would be helpful to begin by enumerating the problems that this renaming is attempting to solve.
Am I missing anything? |
My own feeling on
But, it's worse as the new default thing that attrs does. My feeling is that if we want the API to be centered around type annotations, then |
Alternately, thinking from the perspective of someone approaching attrs for the first time, why does one use this decorator? Because one is defining a type. So perhaps something like this would make more sense:
Or, resurrecting one of my own personal bugaboos from long ago:
Or, for ultimate style points:
|
OK, last thought for the moment: please don't land this without having a typeshed / mypy update ready and waiting :) |
I'm in the serious business camp (can't help myself, sorry) and I don't have a problem with the above. If I port a file, I'll have clean that up. This doesn't sound like a big deal to me. |
Mmmm… |
|
Another suggestion if we're pushing type annotations. |
What would that mean? |
Oh I'm sorry. I meant the Unless you're really partial to |
I’m still a bit confused – are you suggesting to rename/alias the existing |
I am suggesting that. But I guess we don't have to. Though I do think we wanted to at least change the default to
Instead of
What I'm suggesting is:
Hmm. But now that I look at it
And I believe this wouldn't work with the current implementation. Anyway whatever we do. If we can try to only use |
Yeah, new default is gonna be true and |
Yeah. Definitely not |
You’ve just summed up my line of thinking perfectly. :) |
I think it’s fair to say, that while I still think the readability of it is superior compared to
Yes:
Absolutely! The question is whether or not we release 18.2 first. The good/interesting thing here is that we literally don’t have to write any code. It’s a purely design thing of introducing a new package and move things into it. That doesn’t mean it’s easy, but at least we don’t have to review complex code.
I’m a heavy mypy user myself so that’s not gonna happen. :) Fortunately, typeshed doesn’t apply to us anymore since the recent merge of our own stubs. 🎉 Naming
Giving it a thought I very much agree with this. It’s like calling a module/package “new”. Given enough time, you end up with Pont Neuf. Since I feel we do have a rough consensus that a new shell around the API would be beneficial, it’a time to paint the shed.
Please let me know if I missed something from above, it took me two days to write this. :) |
The only thing odd about
But in practice this probably won't matter. Since most people won't be creating classes inside The only other names I can come up with are:
But all are pretty bad. I think |
Random thought: |
My favorite decorator names so far are:
you could also consider verb names:
|
|
Some other things I like about |
Would (I have to admit, I'm fine with the jokey |
I’d have to lie if I said I didn’t think about it. 🤡
why is that? |
Yeah, I like it too but it seems too ambiguous for a general purpose API. OTOH @attrs.cls
class C:
x: int
y: List[int] = attrs.new(list) is kinda cool. I mean this new package is all about ergonomics. I’m still very conflicted on cls vs make. Both have upsides and downsides I guess I’ll do a vote on it after it stewed in my head for a while. |
I haven't really had time to give to this ticket a much as I wanted to. However lately I've started importing names directly into my modules more and more, both for brevity and performance. So whatever names we choose, I'd like them to be able to stand by themselves. What I mean is, I try not to do "import attr; attr.s()" but rather "from attr import s; s()". |
While I agree, it kinda complicates matters because it forces the name to be longer. |
One thing I came up with in the shower: @attrs.define
class Foo:
x = attrs.attrib() which also perfectly works with: @define
class Foo:
x = attrib() It reads perfectly (try to read both out) and also makes perfect sense. In hindsight I’m surprised nobody came up with it before. 🤔 Opinions? |
Hmm. "Define". Maybe we could abbreviate it, to like... In all seriousness this is the best proposal I've seen so far. I still don't instantly love it but it reads cleanly and makes sense. |
Both @hynek this sounds to me as the name to use. I would prefer to keep it as Greetings to your magic shower. |
It’s not always obvious, but glyph was trolling here. ;) You can’t have a variable called >>> def = 42
File "<stdin>", line 1
def = 42
^
SyntaxError: invalid syntax |
😜 |
Well, a month later, I'm still thinking Is anyone unhappy with |
I like the triplet of define, frozen, mutable!
…On Wed, Nov 28, 2018 at 10:23 AM Wilfredo Sánchez Vega < ***@***.***> wrote:
Well, a month later, I'm still thinking define is pretty damned good. I'd
probably bind it to mutable because I'm pedantic and want to be explicit
about that, but define is great (a built-in alias would be swell, but
I'll shut up). I think we had a consensus on frozen for immutable classes.
Is anyone unhappy with define and frozen as the two (canonical, at least)
names?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#408 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAD3E1xdNUR7klu_UZh3r0PVIzRSohTfks5uztSJgaJpZM4VO6LK>
.
|
Heh I think this topic is settled at this point. FWIW, you can always alias our API. There even seem to be at least two ways to make it work with mypy: python/mypy#5406 (comment) |
Finally, how we can avoid confusing @attrs.dataclass in our code? I don't want to confuse colleagues by constructions like this:
So, is there way to use |
Funny you'd ask! So as it's very much obvious, I've been procrastinating on these changes for a while, because I'm afraid to get it wrong. Now I've been thinking, that maybe I should make a step in between and add As for the dataclass method itself, I've noticed that for a former easter egg it's hilariously popular so I may be convinced to add it into the |
what about On the other side of this, what about making a namespace that actually is intentionally similar to PEP 557 |
Guys, my team is asking "Why we are using dataclass here?" ... While I want less boilerplate code with attrs. And I don't want to have dataclasses with methods. We use python dataclasses, when need kind of interface between functions. That's why |
To be fair, it was a joke…it was an easter egg (note the comment in |
Please check out #666 y'all. |
This is the kind of stuff that keeps me from doing any real work. @hynek, I realize that you've already packaged v20.0.1 with some of the proposed changes from #408 and #487, but I wanted to suggest a completely different solution. The new namespace Here it goes:
Firstly, it works. Capturing that audience could lead to fields that have built in validation.
|
I'm perfectly happy with the new API. I extracted my PEP compatibility proposal into #686 |
Is |
Yes, I think this chapter can be closed after more than 2 years.
Thanks everyone for playing! |
So
attr.dataclass
was meant more or less as a joke, but I just caught myself using it too but the name is bad, because it’s confusing. It implies some kind of symmetry or compatibility to DCs which it doesn’t.So here’s a nice bike shed, please help me finding a short, memorable, and meaningful color for it!
The text was updated successfully, but these errors were encountered: