python / python/cpython

types.MappingProxyType states that proxy[key] will throw a KeyError, but that is not always true

Open
#117,956 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Documentation

In the documentation, it states proxy[key] will:

Return the item of the underlying mapping with key key. Raises a KeyError if key is not in the underlying mapping.

https://docs.python.org/3/library/types.html#types.MappingProxyType

However, it is possible to get an AttributeError instead, and in fact, unit tests for the dataclasses module require this to be true (metadata in dataclasses.field is wrapped in a mapping proxy):

https://github.com/python/cpython/blob/5f4c7cf3f49629bcf890c305e0a90ffd708110a2/Lib/test/test_dataclasses/__init__.py#L2065-L2066

The unit tests above are from Python3.12.

Looks like the CPython source code only forwards the getitem call to the inner mapping:

https://github.com/python/cpython/blob/5f4c7cf3f49629bcf890c305e0a90ffd708110a2/Objects/descrobject.c#L1042-L1046

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MappingProxyType documentation at the linked Python docs page, then compare its wording with the referenced dataclasses tests and Objects/descrobject.c implementation. Done means the documentation accurately describes the exception behavior demonstrated by the tests and implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.