python / python/mypy

Wrong errors of type unsupported operand when type checking is done in list

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

Chưa có ai nhận issue này.

bug
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

(A clear and concise description of what the bug is.)

To Reproduce

a: None | float = 0.0
b: None | float = 0.0

c = None if None in (a, b) else a - b
print(c)

(Also the alternative approach of using a regular if...else... block fails)

Playground link

Expected Behavior

No error found by mypy. I cannot think of a way that this code fails. But maybe I'm just very code-blind right now.

Actual Behavior

coordinate_magic/test.py:4: error: Unsupported left operand type for - ("None")  [operator]
coordinate_magic/test.py:4: error: Unsupported operand types for - ("None" and "float")  [operator]
coordinate_magic/test.py:4: error: Unsupported operand types for - ("float" and "None")  [operator]
coordinate_magic/test.py:4: note: Both left and right operands are unions
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): pyproject.toml
    [tool.mypy]
    # Config for 'mypy'
    # See https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file for more details
    python_version = "3.11"
    plugins = "numpy.typing.mypy_plugin"
    warn_return_any = true
    warn_unused_configs = true
    allow_redefinition = true        # Allows variables to be redefined with an arbitrary type, as long as the redefinition is in the same block and nesting level as the original definition.
    disallow_untyped_defs = true
    disallow_incomplete_defs = true  # Disallows defining functions with incomplete type annotations.
    check_untyped_defs = true        # Type-checks the interior of functions without type annotations.
    no_implicit_optional = true      # Changes the treatment of arguments with a default value of None by not implicitly making their type Optional.
    show_error_codes = true
    exclude = "gui/|doc/|coordinate.py"
    
    [[tool.mypy.overrides]]
    # mypy per-module options
    module = [
        "scipy",
        "scipy.*",
        "mpl_toolkits.mplot3d",
        "altair",       # Remove when issue https://github.com/altair-viz/altair/issues/2493 is solved
        "folium",       # Remove when issue https://github.com/python-visualization/folium/issues/1559 is solved
        "folium.plugins",
        "matplotlib",   # Remove when issue https://github.com/matplotlib/matplotlib/issues/20504 is solved
        "matplotlib.*",
        "traffic",
        "coloredlogs",  # Remove when issue https://github.com/xolox/python-coloredlogs/issues/93 is solved
        "pymap3d",      # Remove when discussion https://github.com/geospace-code/pymap3d/pull/76 is solved
    ]
    ignore_missing_imports = true
    
  • Python version used: 3.11

The problem lies in the if None in (a, b) part.
As soon as code is changed to

a: None | float = 0.0
b: None | float = 0.0

c = None if a is None or b is None else a - b
print(c)

mypy does not report an error.

EDIT: Added a playground link with all examples

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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.

Hướng nghiên cứu

Tái hiện đoạn mã từ coordinate_magic/test.py với mypy 1.8.0, sau đó so sánh dạng None in (a, b) với dạng is None. Kiểm tra đường dẫn kiểm tra kiểu cho narrowing dựa trên phép membership; hoàn tất khi phép trừ hợp lệ không còn tạo ra lỗi unsupported-operand và hồi quy được bao phủ bằng một test.

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
compilers
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
42/100

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.