Document that dict.get takes no keyword arguments by adamtheturtle · Pull Request #128207 · python/cpython · GitHub
Skip to content

Document that dict.get takes no keyword arguments - #128207

Closed
adamtheturtle wants to merge 1 commit into
python:mainfrom
adamtheturtle:patch-1
Closed

adamtheturtle wants to merge 1 commit into
python:mainfrom
adamtheturtle:patch-1

Conversation

@adamtheturtle

@adamtheturtle adamtheturtle commented Dec 23, 2024

Copy link
Copy Markdown
Contributor

Running

{}.get("a", default="b")

gives:

    {}.get("a", default="b")
    ~~~~~~^^^^^^^^^^^^^^^^^^
TypeError: dict.get() takes no keyword arguments

Relatedly, this means that dict does not implement collections.abc.Mapping which does allow keyword arguments:


📚 Documentation preview 📚: https://cpython-previews--128207.org.readthedocs.build/

Running

```python
{}.get("a", default="b")
```

gives:

```
    {}.get("a", default="b")
    ~~~~~~^^^^^^^^^^^^^^^^^^
TypeError: dict.get() takes no keyword arguments
```
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news awaiting review labels Dec 23, 2024
@ZeroIntensity

Copy link
Copy Markdown
Member

Relatedly, this means that dict does not implement collections.abc.Mapping which does allow keyword arguments:

Interesting. I wonder if that's worth fixing (but I also wonder if this has been brought up before).

@ZeroIntensity

Copy link
Copy Markdown
Member

but I also wonder if this has been brought up before

Oh, I didn't see that there is! #124675...where I participated in the discussion 😅

@adamtheturtle

Copy link
Copy Markdown
Contributor Author

Interesting. I wonder if that's worth fixing (but I also wonder if this has been brought up before).

I would like for it to be fixed, and I have commented on a related issue (#124675).

As for "brought up before" - I'm not sure here, but I found this comment in typeshed:

    # Positional-only in dict, but not in MutableMapping
    @overload  # type: ignore[override]
    def get(self, key: _KT, /) -> _VT | None: ...
    @overload

https://github.com/python/typeshed/blob/17408ee5381f866c100f5003119a7a1bf050b839/stdlib/builtins.pyi#L1125C1-L1128C14

and discussion around that at python/typeshed#6694.

@adamtheturtle

Copy link
Copy Markdown
Contributor Author

Oh, I didn't see that there is! https://github.com/python/cpython/issues/124675...where I participated in the discussion 😅

FWIW when you asked I pasted my issue text into ChatGPT and asked "I want to write up this bug for CPython. Has any duplicate issue been brought up before?". It found me that issue!

@joooeey

joooeey commented Jan 7, 2025

Copy link
Copy Markdown

Instead of the change proposed here, a note like the following would better reflect the status quo:

CPython implementation detail: All arguments to this method are positional-only.

You can pass key= and default= as keyword arguments in PyPy (see online interpreter).

@ZeroIntensity

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants