erroneous type mismatch with `Union` and `Literal` (false positive)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug Report
Mypy does not sufficiently simplify Unions and Literals before some type checks (the reproduction case uses assert_type(), but assignments and calls will also trigger the issue). The result is false positive errors.
To Reproduce
from __future__ import annotations
from enum import (
Enum,
auto,
)
from typing import (
Literal,
Union,
)
from typing_extensions import (
assert_type,
reveal_type,
)
class Color(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()
RedKwds = Literal['RED', 'R']
GreenKwds = Literal['GREEN', 'G']
BlueKwds = Literal['BLUE', 'B']
ColorKwds = Union[RedKwds, GreenKwds, BlueKwds]
RedSpec = Union[Literal[Color.RED], RedKwds]
GreenSpec = Union[Literal[Color.GREEN], GreenKwds]
BlueSpec = Union[Literal[Color.BLUE], BlueKwds]
ColorSpec = Union[Color, ColorKwds] # ⎱ these are entirely
RGBSpec = Union[RedSpec, GreenSpec, BlueSpec] # ⎰ equivalent types
rgb_spec: RGBSpec
assert_type(rgb_spec, ColorSpec) # fails; should be silent # L34
# Expression is of type...
# Union[
# Union[Literal[Color.RED], Literal['RED', 'R']],
# Union[Literal[Color.GREEN], Literal['GREEN', 'G']],
# Union[Literal[Color.BLUE], Literal['BLUE', 'B']]
# ]
# ... not ...
# Union[
# Color,
# Union[
# Literal['RED', 'R'],
# Literal['GREEN', 'G'],
# Literal['BLUE', 'B']
# ]
# ]
reveal_type(rgb_spec) # L50
# Union[
# Literal[Color.RED], Literal['RED'], Literal['R'],
# Literal[Color.GREEN], Literal['GREEN'], Literal['G'],
# Literal[Color.BLUE], Literal['BLUE'], Literal['B']
# ]
# "shaking" the variable (where "shaking" is clearly a no-op) causes
# things to work as expected:
rgb_spec_shaken = (
rgb_spec
if isinstance(rgb_spec, Color) else
rgb_spec
)
assert_type(rgb_spec_shaken, ColorSpec) # passes!
reveal_type(rgb_spec_shaken) # L66
# Union[
# Color,
# Literal['RED'], Literal['R'],
# Literal['GREEN'], Literal['G'],
# Literal['BLUE'], Literal['B']
# ]
Expected Behavior
The types are provably equivalent; no error should be raised.
Actual Behavior
Mypy does not sufficiently simplify the types, resulting in an erroneous "mismatch".
Your Environment
- Mypy version used: 1.9.0, master 2024-04-22
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.8, 3.12
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.
Hướng nghiên cứu
Bắt đầu bằng cách chạy bản tái hiện của issue trên mypy-play với các trường hợp assert_type() và reveal_type() được nêu, đặc biệt là các dòng 34 và 50, sau đó kiểm tra các đường dẫn đơn giản hóa và tương thích kiểu liên quan đến những phép kiểm tra đó. Hoàn tất có nghĩa là các kiểu Union và Literal tương đương không còn tạo ra lỗi không khớp, đồng thời kiểu được reveal hiển thị được đơn giản hóa phù hợp.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- tooling
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100