[possibly-undefined] Track conditional assignment

Đang mở
#14,310 1 bình luận 2 reaction 1 người được giao Xem trên GitHub

@ilinum đang làm issue này rồi.

Từ ngày 17/12/2022.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

feature topic-possibly-undefined

Feature

A common false-positive pattern I've seen for possibly-undefined is separating the conditional assignment and usage. It would be awesome if mypy could track the condition and thus recognize that the usage happens with the same condition.

Not sure if it's possible to implement. Even pyright hasn't done it (yet).

/CC: @ilinum

def some_func() -> bool:
    return True

var = some_func()
other_conditional: bool = True

if var:
    description = "Hallo"

... # some other code, which doesn't modify `var`

if var:  # maybe a bit easier
    print(description)  # Name "description" may be undefined

if var and other_conditional:
    print(description)  # Name "description" may be undefined

This will likely only work for local variables as we can't easily be sure instance attributes aren't changed by the code block in between the definition and usage.

Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

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.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

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.