GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-96397: Document that keywords in calls need not be identifiers by jeff5 · Pull Request #96393 · python/cpython

Conversation

@jeff5

@jeff5 jeff5 commented Aug 29, 2022 •
edited by merwok
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This is to clarify that the keys of keyword arguments (necessarily given as
a mapping) are acceptable in a call even if they would not be valid
as the identifiers of formal parameters. It responds to the discussion at:

https://discuss.python.org/t/supporting-or-not-invalid-identifiers-in-kwargs/17147/31

Respectfully note the continuing desire of some participants there there to debate this further.

Also, I may not have done full justice to @ChrisBarker-NOAA's contribution. In particular, I think it is worth documenting, as a further changeset here or on another PR, the apparent acceptability of non-identifier names in the __dict__ of objects, supported by (get|set)attr, which he pointed out.

  • Issue: Clarify status of non-identifier unpacked keyword arguments #96397

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jeff5
Docs: keywords in calls need not be identifiers
4540b8c
We clarify that the keys of keyword arguments (necessarily given as
a mapping) are acceptable in a call even if they would not be valid
as the identifiers of formal parameters.
@bedevere-bot bedevere-bot added docs Documentation in the Doc dir skip news awaiting review labels Aug 29, 2022
@AlexWaygood AlexWaygood added the skip issue label Aug 29, 2022
@AlexWaygood AlexWaygood assigned gvanrossum Aug 29, 2022
@merwok

merwok commented Aug 29, 2022

Copy link
Copy Markdown
Member

Let’s create an issue so that it can be referenced properly!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum reviewed Aug 29, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

WIth two small wording nits I am in favor of this PR. Given the controversy around it I will request a SC member as a second reviewer.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/reference/expressions.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/reference/expressions.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum
gvanrossum requested a review from Yhg1s August 29, 2022 18:19
pfmoore
pfmoore approved these changes Aug 29, 2022
View reviewed changes

@pfmoore pfmoore left a comment

Copy link
Copy Markdown
Member

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.

Guido's suggestions are the only comments I would have made.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Aug 29, 2022
@gvanrossum

gvanrossum commented Aug 29, 2022

Copy link
Copy Markdown
Member

Let’s create an issue so that it can be referenced properly!

Agreed. @merwok could you create the issue?

merwok reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood AlexWaygood removed the skip issue label Aug 29, 2022
@nascheme

nascheme commented Aug 29, 2022

Copy link
Copy Markdown
Member

I agree this is behaviour of CPython that we don't intend to change. I.e. it is not a bug. However, is it a feature of the Python language that these non-identifier keywords are allowed or is it an implementation detail of CPython? The reality seems to be that other Python implementations like pypy and Graal have to do the same thing, in order to be compatible. It seems the trend is to avoid having undefined behaviour in languages. So, maybe it should be defined at the language level.

gpshead and Fidget-Spinner reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@merwok merwok changed the title Document that keywords in calls need not be identifiers gh-96397: Document that keywords in calls need not be identifiers Aug 29, 2022
@merwok

merwok commented Aug 29, 2022

Copy link
Copy Markdown
Member

Issue created: #96397

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@merwok merwok linked an issue Aug 29, 2022 that may be closed by this pull request
Clarify status of non-identifier unpacked keyword arguments #96397
Closed
@ChrisBarker-NOAA

ChrisBarker-NOAA commented Aug 29, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@nascheme: There was a bunch of discussion about that in discourse, I don't think any consensus was reached.[*]

Once an issue has been created, I guess that's where we could discuss it.

OOPS -- I see it was created now -- I'll go there.

I'd also like to document the same thing with __dict__, setattr(), getattr() (anywhere else?) -- but I just looked through the docs, and I can't see where it would fit -- it's seems it belongs in the Language Reference, but I can't find where to put it -- maybe that can be discussed in the issue as well.

[*} My $0.02: using non-valid identifiers as attribute names via __dict__, **kwargs, getattr(), setattr() is being done in live production code right now, at least in cPython (https://pypi.org/project/netCDF4/ and I'm sure others). I have no idea what other implementations are doing, but I suspect it's the same, at least with PyPy. Given that, regardless of original intent -- I think it's better to define this as a language feature rather than an implementation detail.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jeff5 and others added 2 commits August 29, 2022 23:45
@jeff5 @gvanrossum
Idiom "whose" in Doc/reference/expressions.rst
2c0833d
Co-authored-by: Guido van Rossum <[email protected]>
@jeff5 @gvanrossum
Simplify to "instance of str" in Doc/reference/expressions.rst
d939eb9
Co-authored-by: Guido van Rossum <[email protected]>
gvanrossum
gvanrossum approved these changes Aug 29, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

You can do setattr() in the same PR or in a separate PR linked to the same issue. Either way this won't be merged until someone from the SC agrees.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jeff5 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@gvanrossum

gvanrossum commented Aug 29, 2022

Copy link
Copy Markdown
Member

I would not do __dict__ yet, since that is currently pretty much defined to return a dict, which doesn't easily let us require for string keys (except for the class dict, which is a readonly proxy guarded by the class setattr operation). I'd like this to remain strictly a docs change, not a code change.

jeff5 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChrisBarker-NOAA

ChrisBarker-NOAA commented Aug 30, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I would not do __dict__ yet, since that is currently pretty much defined to return a dict

Interesting -- so you can stuff pretty much whatever want into an object's dict. However, if you do put a non-string key into a dict, it will store it just fine, but not be accessible by getattr(), and only be accessible by directly accessing the dict.

I suppose it's possible that that could be a feature someone would (ab)use, but maybe the docs could advise against it, and/or call it unsupported?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@merwok

merwok commented Aug 30, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

IMO it’s best for the docs to define the spec precisely, say something about strings that are not identifiers, but not also mention details of non-strings in __dict__ — so document how things work, but not all the ways in which things may not work.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Gouvernathor

Gouvernathor commented Aug 30, 2022

Copy link
Copy Markdown
Contributor

Wouldn't the safest option in short term be to precisely describe how the implementation works, and to categorize all of it as implementation details ? That way, programmers won't assume something is documented when it's just a behavior happening in their console.
Then, anything can always be decided later to be part of the spec (like dict ordering was) or dropped from the implementation. Is there precedent on this though ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead

gpshead commented Aug 30, 2022

Copy link
Copy Markdown
Member

if you do put a non-string key into a dict, it will store it just fine, but not be accessible by getattr(), and only be accessible by directly accessing the dict.

I suppose it's possible that that could be a feature someone would (ab)use, but maybe the docs could advise against it, and/or call it unsupported?

Sometimes documenting something that can be abused even if just to advise against it just leads to more abuse. So I'd lean towards not mentioning that you might be able to get away with stuffing a .__dict__ full of other unexpected key types. (There likely exists code that iterates an object dict and assumes the keys are str that such a thing could trigger an exception out of).


Regardless, for this PR as is, documenting that ** arguments allow any string keys, that seems reasonable to mention. 👍🏾

There are a pile of existing APIs in the world that take arbitrary keyword arguments and depend on this as a convenient feature. Many intentional, some maybe not wholly intentional but users who realize it enjoy the convenience anyways.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jeff5 jeff5 mentioned this pull request Aug 30, 2022
Accepted keys for double-starred dictionaries #94728
Closed
@jeff5

jeff5 commented Aug 30, 2022

Copy link
Copy Markdown
Contributor Author

Wouldn't the safest option in short term be to precisely describe how the implementation works, and to categorize all of it as implementation details ?

@Gouvernathor This would be very much second best and not provide the consistency between implementations we hope for. The Language Reference aspires to document Python the language, and only occasionally (in grey boxes if we remember them) CPython the implementation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChrisBarker-NOAA

ChrisBarker-NOAA commented Aug 30, 2022

Copy link
Copy Markdown
Contributor

I would not do __dict__ yet, since that is currently pretty much defined to return a dict

Now that I think about it -- it seems it would not be too hard to have a dict that only accepts string keys -- but yes, that's a code change, so a new topic and not part of this PR.

For the Typing folks: are the dunders often typed? that may be a place to define what a __dict__ is, even if not enforced.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@merwok merwok added the DO-NOT-MERGE label Aug 30, 2022
@jeff5 jeff5 mentioned this pull request Aug 31, 2022
gh-96397: Document that attributes need not be identifiers #96454
Merged
@jeff5

jeff5 commented Sep 13, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

this won't be merged until someone from the SC agrees.

Am I the one to request that? I would include #96454, but in a way that allowed a separate decision.

Is there a proper place (like the PEP repo) that puts it on the agenda. E-mail to python-dev or SC secretary maybe?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@merwok

merwok commented Sep 13, 2022

Copy link
Copy Markdown
Member

Requests to the council are sent here: https://github.com/python/steering-council/issues/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Sep 16, 2022

Copy link
Copy Markdown
Member

Still waiting for the SC…

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jeff5

jeff5 commented Sep 16, 2022

Copy link
Copy Markdown
Contributor Author

Still waiting for the SC…

Which I think this is waiting on me to ask it?

I can draft this weekend (if I can slip by the King's forces currently encamped around PyConUK).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Sep 16, 2022

Copy link
Copy Markdown
Member

Still waiting for the SC…

Which I think this is waiting on me to ask it?

I can draft this weekend (if I can slip by the King's forces currently encamped around PyConUK).

Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jeff5 jeff5 mentioned this pull request Sep 18, 2022
Non-identifier keys in ** arguments and elsewhere python/steering-council#142
Closed
@jeff5

jeff5 commented Sep 21, 2022

Copy link
Copy Markdown
Contributor Author

In view of the SC decision, can this (and #96454) now be merged?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum reviewed Sep 21, 2022
View reviewed changes
Comment thread Doc/reference/expressions.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum reviewed Sep 22, 2022
View reviewed changes
Comment thread Doc/reference/expressions.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum
"must be a string" instead of a str instance
fd80c3a
gvanrossum
gvanrossum approved these changes Sep 22, 2022
View reviewed changes
@gvanrossum gvanrossum added needs backport to 3.10 needs backport to 3.11 only security fixes and removed DO-NOT-MERGE labels Sep 22, 2022
@gvanrossum
gvanrossum merged commit 9d432b4 into python:main Sep 22, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Sep 22, 2022
@miss-islington

miss-islington commented Sep 22, 2022

Copy link
Copy Markdown
Contributor

Thanks @jeff5 for the PR, and @gvanrossum for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Sep 22, 2022

Copy link
Copy Markdown

GH-97025 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Sep 22, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 22, 2022
@jeff5 @miss-islington
pythongh-96397: Document that keywords in calls need not be identifie…
f9cda36
…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]>
@bedevere-bot

bedevere-bot commented Sep 22, 2022

Copy link
Copy Markdown

GH-97026 is a backport of this pull request to the 3.10 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 label Sep 22, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 22, 2022
@jeff5 @miss-islington
pythongh-96397: Document that keywords in calls need not be identifie…
83ca7c5
…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]>
miss-islington added a commit that referenced this pull request Sep 22, 2022
@miss-islington @jeff5
gh-96397: Document that keywords in calls need not be identifiers (GH…
8a2afd2
…-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]>
miss-islington added a commit that referenced this pull request Sep 22, 2022
@miss-islington @jeff5
gh-96397: Document that keywords in calls need not be identifiers (GH…
9eb1e6c
…-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]>
@AlexWaygood AlexWaygood mentioned this pull request Oct 11, 2022
Document that CPython accepts "invalid" identifiers #79286
Closed
pablogsal pushed a commit that referenced this pull request Oct 22, 2022
@miss-islington @jeff5 @pablogsal
gh-96397: Document that keywords in calls need not be identifiers (GH…
50c0a22
…-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]>
@jeff5
jeff5 deleted the doc-accept-nonid-kwargs branch February 20, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gpshead gpshead gpshead left review comments
@pfmoore pfmoore pfmoore approved these changes
@gvanrossum gvanrossum gvanrossum approved these changes
@Yhg1s Yhg1s Awaiting requested review from Yhg1s

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@gvanrossum gvanrossum

Labels

docs Documentation in the Doc dir skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Clarify status of non-identifier unpacked keyword arguments

Uh oh!

There was an error while loading. Please reload this page.

11 participants

@jeff5 @merwok @gvanrossum @nascheme @ChrisBarker-NOAA @Gouvernathor @gpshead @miss-islington @bedevere-bot @pfmoore @AlexWaygood
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.