Sentinel and enum type merge issues with generic
Maintainer thường phản hồi trong vòng 1 ngày
Đá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
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ả
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
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/mypy
-
Regression in 2.4.0: empty constraint tuple in PEP 695 type parameter (`class C[T: ()]`) is no longer reportedCó thể làm lại được @CheerathAniketh đã nhận 23 ngày trước và không có pull request nào đang mở. Đang mởtopic-parser topic-pep-695
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
python/mypy#22176 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
matches_gitignore: directory-only patterns in nested .gitignore not excluded under --exclude-gitignoreCó thể làm lại được @pavithraramesh5500-ship-cse đã nhận 14 ngày trước và không có pull request nào đang mở. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`stubgen --inspect-mode` crashes with `AttributeError on PEP 604 union syntax (X | Y)` *(mypy 2.1.0)*Có thể đã có người làm @ayushdoesdev đã nhận 91 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Inconsistent description of `strict_optional`Có thể đã có người làm @Joosboy đã nhận 107 ngày trước. Đang mởdocumentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
python/mypy#21658 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Cannot set `output` in `mypy.ini`Có thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởbug topic-configuration topic-error-reporting
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
python/mypy#21376 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
cost:cheap practicality:seen-in-the-wild severity:medium
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
fairagro/m4.2_advanced_middleware_api#620 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area:security effort:S priority:P2 provider:local-libvirt status:ready type:docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
randomparity/kdive#3495 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
microsoft/markitdown#2820 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày