-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Clarify status of non-identifier unpacked keyword arguments #96397
Comments
Docs referenced from the discussion: https://docs.python.org/3/reference/expressions.html#calls Secondary issue that could be wrapped in this (from Guido):
|
Note that the same issue shows up in a few other places, e.g. (There's also |
I think there's two potentially separate issues:
|
Using non-valid identifiers as attribute names via As Guido pointed out, this is also relevant to I had suggested text something like: """ Examples: dict and **kwargs may not have non-string keys, but may have non-valid strings as identifiers (e.g. "this-name"). getattr() and setattr() will accept only strings, but the strings to do not have to be valid identifiers. (e.g. setattr(obj, "this-name", value) But I have no idea where to put it in the docs. |
I agree we don't have to allow string subclasses. Right now CPython allows it, but the usage would appear niche. We can leave this unspecified if we want to. |
I found this too difficult to understand. What is a "python name" different from a "python identifier"? And what, every container of any kind? I opted for documenting the behaviour in the specific context of keys in a mapping in a call. I offer to do the same for the name arguments in
No indeed. And I wouldn't know where to find it, which recommended in my mind putting consistent specialisations of the rule in the docs for call, |
Yes, but can I suggest this might be important to standardise, one way or the other? In #94938 I broke CPython by violating the rules of hashable, but you can do some things with well-behaved sub-classes might be thought useful:
|
An "identifier" is what you can put in code: https://docs.python.org/3/reference/lexical_analysis.html#identifiers I think that's well defined. A "name" is the thing we can use in **kwargs dict, or pass to get/setattr() or use as a key in a dict. I have no idea how to clearly define it -- maybe: "A key in a namespace mapping" -- but I can't say that's clear either.
I used "container" to be generic -- in practice, it's usually a dict -- but that's certainly not part of the language definition -- maybe Mapping? But now that I think about it, it's an argument to setattr() -- not in a container at all :-( I used those generic terms because it seems that this pops up in multiple places in the language, and currently (and I hope forever), the rule is consistent -- so it seemed better to define the consistent rule in one place, rather than, or in addition to, adding a note to the half a dozen places where it's relevant (and maybe miss some). I'm getting way out my area of expertise here, but maybe the rules for valid identifiers only apply when parsing/evaluating code. Once the system is passed the parsing stage, they are simply strings, and any string will do. So maybe that could be the way to define this behavior? |
Agreed. And I might say "in code without the quotes" because in the context of Python source, the scanner can pick identifiers out as lexemes.
Also agreed. ;)
Ok, I think we're in agreement that while a single statement would be good, it is quite difficult to write and place. Being a bear of very little brain, I chose to start with the specific cases, even at risk of repetition. Maybe a common thread will emerge. It was only about 100 words for keywords. I don't think it's as much as half-a-dozen places. |
I'll offer a separate PR documenting the range of names acceptable in Note that our current definition of attribute is "something you can address with a dot" so ... would pseudo-attribute be a good name for something you can't address with a dot but can with I realised I had recently come across pseudo-attributes in a the |
@jeff5: Thanks, that sounds like a good plan -- though I'm not sure the I'm not sure I like "pseudo-attributes", but don't have a better suggestion :-( |
The main thing here is about unpacked keyword arguments; I think https://docs.python.org/3/reference/expressions.html#calls is the place to change. Docs for getattr/setattr (functions, not magic methods) could be updated in the same PR. |
We'll need to wait for the SC to pipe up before the |
@merwok I was given the choice and thought separate was better because of the possibility of separately approving the PRs. @ChrisBarker-NOAA wrote:
Actually in PyObject_GetAttr. Let me try and write it. I'll be able to tell if I'm doing it wrong or you can say so later. This is interesting to me because in the prototype Jython 3 the type of the |
Thanks for the clarification, but in any case, before it gets to a user-defined |
I chose to document this in the functions, rather than the dunders, but
I'm boldly suggesting in #96454 that they be considered attributes, even if dot notation cannot be used (for grammatical reasons). |
SC ruling: python/steering-council#142 (comment) The feature is fine, and there’s a distinction between attributes in the object model and names in the syntax that’s IMO clear and meaningful. |
…6393) This represents the official SC stance, see python/steering-council#142 (comment)
…rs (pythonGH-96393) This represents the official SC stance, see https://github.com/python/steering-council/issues/142GH-issuecomment-1252172695 (cherry picked from commit 9d432b4) Co-authored-by: Jeff Allen <[email protected]>
…rs (pythonGH-96393) This represents the official SC stance, see https://github.com/python/steering-council/issues/142GH-issuecomment-1252172695 (cherry picked from commit 9d432b4) Co-authored-by: Jeff Allen <[email protected]>
…-96393) This represents the official SC stance, see https://github.com/python/steering-council/issues/142GH-issuecomment-1252172695 (cherry picked from commit 9d432b4) Co-authored-by: Jeff Allen <[email protected]>
…-96393) This represents the official SC stance, see https://github.com/python/steering-council/issues/142GH-issuecomment-1252172695 (cherry picked from commit 9d432b4) Co-authored-by: Jeff Allen <[email protected]>
Co-authored-by: C.A.M. Gerlach <[email protected]>
…honGH-96454) Co-authored-by: C.A.M. Gerlach <[email protected]> (cherry picked from commit 9a11ed8) Co-authored-by: Jeff Allen <[email protected]>
…honGH-96454) Co-authored-by: C.A.M. Gerlach <[email protected]> (cherry picked from commit 9a11ed8) Co-authored-by: Jeff Allen <[email protected]>
Co-authored-by: C.A.M. Gerlach <[email protected]> (cherry picked from commit 9a11ed8) Co-authored-by: Jeff Allen <[email protected]>
Co-authored-by: C.A.M. Gerlach <[email protected]> (cherry picked from commit 9a11ed8) Co-authored-by: Jeff Allen <[email protected]>
…hon#96454) Co-authored-by: C.A.M. Gerlach <[email protected]>
Co-authored-by: C.A.M. Gerlach <[email protected]> (cherry picked from commit 9a11ed8) Co-authored-by: Jeff Allen <[email protected]>
…-96393) This represents the official SC stance, see https://github.com/python/steering-council/issues/142GH-issuecomment-1252172695 (cherry picked from commit 9d432b4) Co-authored-by: Jeff Allen <[email protected]>
In current CPython, unpacked keyword arguments must be strings but don’t have to be valid identifiers.
Discussion: https://discuss.python.org/t/supporting-or-not-invalid-identifiers-in-kwargs/17147
Messages from Guido (creator of the language) and core developers show that this behaviour is desired and will not be changed.
The issue is whether this is a CPython (stable) implementation detail, in which case docs could be clarified with the fix backported, or if it’s time to make it a guaranteed part of the language, binding for other implementations, starting from 3.11 or 3.12.
I have not found where the doc should be improved; arguments and parameters are discussed in glossary, library reference, etc. but I don’t know where is one complete, authoritative explanation of unpacked keyword params.
The text was updated successfully, but these errors were encountered: