python / python/cpython

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

未關閉
#117,956 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

docs
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先查看連結的 Python 文件頁面中關於 MappingProxyType 的文件,然後將其措辭與所參照的 dataclasses 測試和 Objects/descrobject.c 實作進行比較。完成的標準是:文件準確描述測試和實作所呈現的例外行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
documentation
Issue 類型
文件
難度
1/5
預估耗時
1 小時以內
活躍度
停滯
描述清晰度
基本清楚
新手友好度
58/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。