python / python/mypy

Optional types not being narrowed after a logical-and followed by a test of one of the operands

Đang mở
#6,835 2 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 priority-2-low
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ả

(Sorry if the title is overly specific)

  • Are you reporting a bug, or opening a feature request?

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 get_org_id(org_name: str) -> str:
    return 'some_org_id'

def create(org_name: Optional[str], org_id: Optional[str]):
    if not org_name and not org_id:
        raise ValueError('No org specified')
    if not org_id:
        org_id = get_org_id(org_name)
  • What is the actual behavior/output?

The inferred type of org_name at the last line is Optional[str] (technically Union[builtins.str, None]).

  • What is the behavior/output you expect?

The type should be narrowed to str.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Python 3.7.3
mypy: 0.701

  • What are the mypy flags you are using? (For example --strict-optional)

No flags

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 giản trong issue và xác nhận kiểu được suy luận của org_name sau các điều kiện AND logic. Theo dõi hành vi thu hẹp kiểu đối với org_name và org_id, sau đó bổ sung độ bao phủ cho trường hợp này. Hoàn tất khi org_name được suy luận là str tại lần gọi get_org_id mà không làm hỏng việc thu hẹp kiểu tùy chọn liên quan.

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
Đặc tả rõ ràng
Mức phù hợp với người mới
35/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.