python / python/mypy

Truthiness of None Does not Narrow Optional Types

Đang mở
#7,249 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.

false-positive feature needs discussion priority-2-low topic-type-narrowing
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ả

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?
    I believe this is a bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

from typing import Optional

def the_method(maybe_int: Optional[int]):
    can_divide = bool(maybe_int)
    if can_divide:
        100 / maybe_int
  • What is the actual behavior/output?
error: Unsupported operand types for / ("int" and "None")
  • What is the behavior/output you expect?
    I would think that the conditional branch would recognize that only int values would ever make it there

  • What are the versions of mypy and Python you are using?
    mypy 0.720 with Python 3.7.3

  • Do you see the same issue after installing mypy from Git master?
    Yes still exists on master

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

Bắt đầu với bản tái hiện Python tối thiểu trong issue và lần theo điểm vào của việc thu hẹp kiểu dựa trên truthiness của mypy. Báo cáo không nêu tên tệp nguồn hay bài kiểm thử nào; được xem là hoàn thành khi nhánh điều kiện thu hẹp Optional[int] đủ để 100 / maybe_int vượt qua kiểm tra kiểu, kèm theo độ bao phủ hồi quy.

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
45/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.