polymorphic overloads on `list.__add__` and `dict.__or__` lead to divergences in type checkers.
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
從 list.add 和 dict.or 的多載開始,然後檢視 PRs #14282 和 #14284,以了解提議的簡化方案。使用 mypy 和 pyright 重現 list 和 dict 範例,並使用 mypy-primer 驗證所選的 stub 變更能移除不一致的 wrapper 錯誤。
由索引模型根據 Issue 內容生成。
描述
The overloads on list.__add__ lead to divergent behavior between mypy and pyright when doing something as simple as a list concatenation
# Overloading looks unnecessary, but is needed to work around complex mypy problems
@overload
def __add__(self, value: list[_T], /) -> list[_T]: ...
@overload
def __add__(self, value: list[_S], /) -> list[_S | _T]: ...
Code sample in pyright playground, https://mypy-play.net/?mypy=latest&python=3.12&gist=abf6a8834020af17a16bd8cfb44b2f10
from typing import Any, overload
class ListA[T]: # emulates builtins list
@overload
def __add__(self, other: "ListA[T]", /) -> "ListA[T]": return ListA()
@overload
def __add__[S](self, other: "ListA[S]", /) -> "ListA[T | S]": return ListA()
class ListB[T]: # without overloads
def __add__[S](self, other: "ListB[S]", /) -> "ListB[T | S]": return ListB()
# mypy | pyright
reveal_type( list[str]() + list[str]() ) # list[str] | list[str] ✅️
reveal_type( list[str]() + list[int]() ) # list[str | int] | list[str | int] ✅️
reveal_type( list[str]() + list[Any]() ) # list[Any] | list[str] ❌️
reveal_type( ListA[str]() + ListA[str]() ) # ListA[str] | ListA[str] ✅️
reveal_type( ListA[str]() + ListA[int]() ) # ListA[str | int] | ListA[str | int] ✅️
reveal_type( ListA[str]() + ListA[Any]() ) # ListA[Any] | ListA[str] ❌️
reveal_type( ListB[str]() + ListB[str]() ) # ListB[str] | ListB[str] ✅️
reveal_type( ListB[str]() + ListB[int]() ) # ListB[str | int] | ListB[str | int] ✅️
reveal_type( ListB[str]() + ListB[Any]() ) # ListB[str | Any] | ListB[str | Any] ✅️
This ultimately causes some very annoying type errors when checking wrapper functions in pyright.
Code sample in pyright playground
from typing import Mapping, Any
# function with 2 optional arguments
def foo(arg: object, /, *, opt1: str = ..., opt2: int = ...) -> None: ...
# wrapper that forwards args via dict
def foo_wrapper(arg: object, foo_kwargs: Mapping[str, Any]) -> None:
# apply new defaults
foo_kwargs = {"opt1": "new_default"} | dict(foo_kwargs)
foo(arg, **foo_kwargs) # "str" cannot be assigned to parameter "opt2"
PR #14282 and #14284 show mypy-primer results of simplifying the overloads away from list.__add__ and dict.__or__.
- 主要語言
- Python
- 星號
- 5.1k
- 分支
- 2.1k
- 平均合併
- 2 天 5 小時
- 30 天內合併 PR
- 122
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typeshed 的其他 Issue
-
`networkx.set_edge_attributes` should support scalar value + name可能已有人在做 @Fire162 於 10 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 67/100
維護者通常 1 天內回覆
-
false alarm on decode call可能已有人在做 @jw9829 於 38 天前認領。 未關閉help wanted stubs: false positive
難度 2/5 1-3 小時 新手友好度 76/100
python/typeshed#16441 · 1 則留言 ·
維護者通常 1 天內回覆
-
Don't run pyright basic checks on projects with strict pyright settings in CI可能已有人在做 @Mohith1612 於 75 天前認領。 未關閉project: infrastructure
難度 2/5 1-3 小時 新手友好度 68/100
python/typeshed#16005 · 1 則留言 ·
維護者通常 1 天內回覆
-
stubs: improvement
難度 2/5 1-3 小時 新手友好度 72/100
python/typeshed#15548 · 5 則留言 ·
維護者通常 1 天內回覆
-
stubs: improvement
難度 2/5 1-3 小時 新手友好度 64/100
python/typeshed#15529 · 5 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
bug ready-for-dev
難度 1/5 1-3 小時 新手友好度 92/100
OpenHands/extensions#897 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
tensorflow/datasets#11253 ·
維護者通常 1 天內回覆
-
fix(core): a config section with all keys commented out crashes startup可能已有人在做 @Aryanbhargava18 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 75/100
kubeflow/mcp-server#512 ·
維護者通常 1 天內回覆
-
Stop trusting the compromised eMudhra CA certificates可能已有人在做 @anth-volk 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
PolicyEngine/uk-triple-lock#54 ·
維護者通常 1 天內回覆
-
DBFS append mode silently overwrites existing files可能已有人在做 @IlyaKhalafi 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 77/100
fsspec/filesystem_spec#2429 ·
維護者通常 1 天內回覆