-
-
Notifications
You must be signed in to change notification settings - Fork 364
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
Add decorator option for converters #404
Conversation
This should probably be done via hypothesis strategies though?
I see it’s WIP, just a bunch of random notes to save you some time:
|
That's why I put it up here sooner than later, thanks. I made converter_self = (
isinstance(a.converter, Converter) and a.converter.takes_self
) would roughly become: converter_self = isinstance(a.converter, Converter) |
@hynek, I figured it would make sense to clarify the logic before writing up docs etc. I didn't quite understand how your suggestion would make the diff much simpler. Do you have time to offer a bit more explanation? I looked at making it so that internally Then again, perhaps I have just misrepresented my needs. I want both the decorator syntax for converter and also the |
I'm not sure if storing a |
So um I’m confused by your use case, it looks like you really just want a default? What do you need So could you elaborate why you think you need P.S. The rst in the news fragment is broken. :) |
One attribute is As to just making every |
https://travis-ci.org/python-attrs/attrs/jobs/401284405#L510
I don't get this locally and it's not telling me what line etc... but apparently I guessed correctly. |
040f2b9 adds a test for my desired behavior of |
|
I honestly don't understand the use-case at all here. Access to partially-initialized |
Sorry, I thought I'd have this tons-of-free-time summer this year and yet reality disliked my plans. Anyway, I haven't been following along. Is there an example of that this is meant to achieve? I don't see added docs in the PR or an example in this ticket, so it's not obvious to me what the problem being solved is. That said, I share the above concerns about anything passing a half-baked |
@glyph, the short explanation is that the converter is actually configurable via a 'previous' attribute. The library was hardcoded with float but I wanted to use decimal so I introduced the option to specify the factory to be what the application developer wanted. I would have sworn it was used for other stuff in the class but it appears to just be a converter for a few values. This doesn't seem great, more like we should let the developer apply it themselves or else we just need it elsewhere. :| I have to admit I was also surprised when I first saw that the Sorry for not providing docs yet, the reasoning in my head was that I'd write them once we decided what the functionality would be. I see how having them now could help clarify. |
I think what Kyle wants it this: @attr.s
class C:
type = attr.ib(default=float)
value = attr.ib(default=0.0). # NB: you can’t even have mandatory attribs with this approach
@value.converter
def _(self, val):
return self.type(val) And I’m gonna side with @glyph here: passing around half-initialized This is really complicated. 🤔 |
@altendky So to be as specific as possible, the question is: why don't you do this instead? @attr.s
class C:
type = attr.ib()
value = attr.ib()
@classmethod
def new(cls, type, value):
return cls(type, type(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. However, since this is my first review for an attrs PR, someone else should take a look at it, too. :)
@hynek, annoyed? No... :] But I do have to get my head back into attrs a bit. I worked a bit on the And I just realized I hadn't set this back to draft... Getting that now. |
Why? Is something still missing or are you just waiting for a reply from @hynek ? |
@sscherfke, I try to keep a todo list in the first post of my PRs. As I identify things to do I add them, then check them off when done. I just took a few days to work on getting a new pyqt5-tools out this morning. Assuming it doesn' t need immediate fixup, maybe I'll get back to this. If you have any suggestions about the open issues in my 'Draft for:' list, I'm happy for some help. |
Taking off the milestone to take off the pressure. Take all the time you need, thank you staying with us! |
Any merge update ? |
Anyone is welcome to branch off my work and try to get it fully ready for merge. I don't know what more has come up in the last few years, but I did leave a list of needed work, or at least things to consider, before I thought it would be ready. Sadly, I don't make it out to community development much these days so I don't expect to be working on this. Nor do I get to work with attrs, which also is sad. |
Yeah I'm keeping it open, because I've started a few times to implement the three-argument converters myself and using is for inspiration, but if you care about performance, it's a bit trickier than it seems on the first sight and something always came up. :|
💔 – thank you for all your contributions! |
At least I'm disappearing on PR #404... |
Issue #240
Draft for:
.pyi
).tests/typing_example.py
.docs/api.rst
by hand.@attr.s()
have to be added by hand too.versionadded
,versionchanged
, ordeprecated
directives. Find the appropriate next version in our__init__.py
file..rst
files is written using semantic newlines.changelog.d
.tests/typing_example.py:126: error: Unsupported converter, only named functions and types are currently supported
setattr
featureConsider eliminating theConverter
classAttribute
attr.setters.converter()
needingattr._make.Converter
(butattr._make
importsattr.setters
so...)