Sentinel and enum type merge issues with generic
維護者通常 1 天內回覆
評估
研究方向
Start by running the supplied examples through mypy and compare direct assignment with assigning the result to a temporary. Check both the sentinel and enum cases against the working int example. Done means the direct assignments are accepted and the revealed types match the expected results.
由索引模型根據 Issue 內容生成。
描述
from typing_extensions import sentinel
Undefined = sentinel("Undefined")
def none_if_undefined[_T](value: _T | Undefined) -> _T | None:
return None if value is Undefined else value
def func() -> None:
device_id: str | Undefined | None = Undefined
device_id = none_if_undefined(device_id) # <-- error
reveal_type(device_id)
test.py:10: error: Argument 1 to "none_if_undefined" has incompatible type "str | Undefined | None"; expected "str | Undefined" [arg-type]
test.py:11: note: Revealed type is "str | Undefined | None"
However, assigning the result from none_if_undefined to a temporary variable first works as expected.
def func() -> None:
device_id: str | Undefined | None = Undefined
x = none_if_undefined(device_id)
reveal_type(x)
device_id = x
reveal_type(device_id)
test.py:11: note: Revealed type is "str | None"
test.py:13: note: Revealed type is "str | None"
--
A similar issues exists for enums
from enum import Enum
from typing import Literal
class E(Enum):
SPECIAL = 1
def none_if_special[_T](value: _T | Literal[E.SPECIAL]) -> _T | None:
return None if value is E.SPECIAL else value
def func() -> None:
device_id: str | Literal[E.SPECIAL] | None = E.SPECIAL
device_id = none_if_special(device_id) # <-- error
reveal_type(device_id)
test.py:12: error: Argument 1 to "none_if_special" has incompatible type "str | Literal[E.SPECIAL] | None"; expected "str | Literal[E.SPECIAL]" [arg-type]
test.py:13: note: Revealed type is "str | Literal[test7.E.SPECIAL] | None"
--
For completeness the related example with just int - that works fine -- no errors.
def none_if_int[_T](value: _T | int) -> _T | None:
return None if isinstance(value, int) else value
def func() -> None:
device_id: str | int | None = 42
device_id = none_if_int(device_id)
reveal_type(device_id)
- 主要語言
- Python
- 星號
- 20.7k
- 分支
- 3.4k
- 平均合併
- 1 天 15 小時
- 30 天內合併 PR
- 114
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/mypy 的其他 Issue
-
Regression in 2.4.0: empty constraint tuple in PEP 695 type parameter (`class C[T: ()]`) is no longer reported可能重新可做 @CheerathAniketh 於 22 天前認領,目前沒有進行中的 PR。 未關閉topic-parser topic-pep-695
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
matches_gitignore: directory-only patterns in nested .gitignore not excluded under --exclude-gitignore可能重新可做 @pavithraramesh5500-ship-cse 於 14 天前認領,目前沒有進行中的 PR。 未關閉bug
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
`stubgen --inspect-mode` crashes with `AttributeError on PEP 604 union syntax (X | Y)` *(mypy 2.1.0)*可能已有人在做 @ayushdoesdev 於 90 天前認領。 未關閉bug
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
Inconsistent description of `strict_optional`可能已有人在做 @Joosboy 於 106 天前認領。 未關閉documentation
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
Cannot set `output` in `mypy.ini`可能重新可做 關聯的 PR 已關閉且未合併。 未關閉bug topic-configuration topic-error-reporting
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
aws/sagemaker-python-sdk#6554 ·
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 78/100
huggingface/diffusers#15170 ·
維護者通常 1 天內回覆
-
[BUG] LLM conflict check ignores MNEMOSYNE_LLM_MAX_TOKENS, so reasoning models return empty replies未關閉
難度 2/5 1-3 小時 新手友好度 62/100
mnemosyne-oss/mnemosyne#1325 ·
維護者通常 1 天內回覆
-
enhancement meshtastic-2.8
難度 2/5 1-3 小時 新手友好度 62/100
pskillen/meshflow-api#459 ·