Sentinel and enum type merge issues with generic

Đang mở
#22,334 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@edgarrmondragon đang làm issue này rồi.

Từ ngày 2/11/2026.

  • #22351 của @edgarrmondragon — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
49/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug
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)
Ngôn ngữ chính
Python
Star
20.7k
Fork
3.4k
Merge trung bình
1 ngày 15 giờ
Pull request đã merge (30 ngày)
119

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.