-
-
Notifications
You must be signed in to change notification settings - Fork 374
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 support for keyword only arguments #106
Comments
This functionality would be nice for the "big bag of configuration" use case. I don't really want to be able to pass my 19 different configuration items positionally. It would be totally unreadable. Currently I have to make sure to carefully order my attribute definitions to avoid running into limitations about what can have a default and what cannot. If everything were keyword-only, this would go away. This use-case would be better served by an API like the latter suggested above (so the setting doesn't have to be repeated 19 times). The existence of both of the proposed APIs wouldn't hurt this use case, though. |
I can already use keywords arguments to build |
Kwonly attributes would have to have defaults, no? How does that work with with the class decorator approach?
If kwonly attributes must have defaults, this wouldn't solve anything for you. You can set defaults today on everything and not care about ordering attributes.
Not sure what you mean here, could you clarify? You'd like to disable positional arguments on your I thought you might be disagreeing with having to carefully position arguments to |
I want to be able to write this:
This is not currently possible because:
As a work-around, I can re-arrange the attributes on my class every time I add or remove a default - but I'd much rather just say something like |
@Tinche No, keyword only is different to having a default.
is different to keyword only with default:
is different to keyword only
My original question was what should the API be? |
Huh, guess I learned something today. None of the examples in the PEP 3102 show a kw-only argument without a default, but the text mentions it (I just skimmed the text :). Anyway, continuing the discussion. Currently the definition order of attributes is the same as the order of the attributes in the generated Keeping with this design decision, if your attributes were a mix of normal and kwonly attributes, you'd still need to define your kwonly attributes after the normal ones.
The idea all attributes could be marked as kwonly is also being floated around:
The attribute-level kwonly needs support from
I think this is where we are with the proposals, currently. |
First of all: as long as I breathe, there won’t be any strings part of any API. :) Secondly, I guess we could implement kw-only so mixing is possible by moving default values into Precondition is that it must not have any performance implications for common use cases. Thirdly, I’m totally not implementing it. :) |
It's backward compatible, and supports not using the enum when you don't feel like it (i.e. the REPL or using timeit).
Is this a response to the issue of having to order the attributes so attributes with defaults are last? I think a separate issue should be created for commenting on this, since it's almost orthogonal to kwonly arguments. (We could implement kwonly args without touching the rule, or the rule could be changed without implementing kwonly args at all, and kwonly args would be useful even if we change the rule.) |
I feel like kwonly is a red herring. I don’t see any reason to add any complexity just so people can’t pass argument by order. OTOH being able to mix default with non-defaults is an actual use case that might be mitigated by a kwonly design. But focusing on the former seems backward to me. |
I've used kw-only arguments pretty regularly since their addition. My main reasons for doing so:
You don't have to agree, of course, but since nobody else responded after you said you don't see any reason, I thought I'd list the ones I find compelling in the projects/teams I've been working on. There's also the official python reason for including them in the language, which I also appreciate from time to time for functions, and actually isn't about preventing people from passing arguments by order - it's about allowing people to specify arguments by keyword after using varargs:
While I appreciate that use case from time to time for functions... I don't know that I've ever actually wanted to do keywords after varargs in a class init, but |
FWIW there’s two levels to it:
|
fwiw, a trick for py2 (who uses that anyway nowadays?) is to add a |
while one might think the extra "marker arg" is ugly, it actually gives much better info in |
I think I could live with such an hack on Python 2. |
I'm in favor of the class-wide The per-attribute constraint seems like it might add more complexity than it's worth, but that may be simply because it's not a use case I have at the moment… perhaps as a compatibility thing where you have an existing class and you want all new attributes to be kwarg-only? But I'd love to see the class-level constraint. |
I'd love to see this, too. And, while the granularity of the per- |
One more use case for keyword-only arguments is subclassing: For example I have two classess: @attr.s
class A:
required_a = attr.ib(None)
optional = attr.ib(None)
@attr.s
class B(A):
required_b = attr.ib(None) With kw-only I can mark the optional attribute of the A class as kw-only and pass required attributes as positional arguments: |
closes python-attrs#38 (Rebases python-attrs#281) Co-authored-by: Alex Ford <fordas@uw.edu>
* Added support for keyword-only attributes. Closes #106, and closes #38 (Rebases #281) Co-authored-by: Alex Ford <fordas@uw.edu> * Add `attr.s`-level `kw_only` flag. Add `kw_only` flag to `attr.s` decorator, indicating that all class attributes should be keyword-only in __init__. Minor updates to internal interface of `Attribute` to support evolution of attributes to `kw_only` in class factory. Expand examples with `attr.s` level kw_only. * Add `kw_only` to type stubs. * Update changelog for rebased PR. Hear ye, hear ye. A duplicate PR is born. * Tidy docs from review. * Tidy code from review. * Add explicit tests of PY2 kw_only SyntaxError behavior. * Add `PythonToOldError`, raise for kw_only on PY2. * `Attribute._evolve` to `Attribute._assoc`.
There was a suggestion in #38 that there be an option to make arguments keyword only. The suggested API was to have something like:
Would support for this be accepted? What about
which may be nicer when subclassing?
The text was updated successfully, but these errors were encountered: