python / python/mypy

Possibly-undefined: allow assertions on the lengths of sequences

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

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

feature topic-possibly-undefined
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ả

The new (disabled-by-default) possibly-undefined error code is great, and I'm using it in several of my projects. There are many hits if you enable it for typechecking mypy's own code, however. An example is this function, where mypy (correctly) points out that arg may be undefined on line 526 (arg_types might be an empty list).

https://github.com/python/mypy/blob/456dcbdd6a2f9b36249e0e7d31e521f5678a8235/mypy/suggestions.py#L509-L527

It would be nice if we could use assertions based on the truthiness of a sequence to make this error go away. E.g., it would be nice if we could silence the error here with this diff:

--- a/mypy/suggestions.py
+++ b/mypy/suggestions.py
@@ -512,6 +512,7 @@ class SuggestionEngine:
         arg_names: list[list[str | None]],
         arg_types: list[list[Type]],
     ) -> str:
+        assert arg_types
         args: list[str] = []

Mypy doesn't currently understand this idiom, however. @ilinum, how hard might this kind of thing be to implement?

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 ví dụ trong mypy/suggestions.py quanh các dòng 509-527 và tái hiện cảnh báo possibly-undefined khi bật tùy chọn. Theo dõi đường đi kiểm tra kiểu cho ví dụ assert arg_types; hoàn tất khi assertion này xác lập chính xác rằng sequence không rỗng và loại bỏ cảnh báo.

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
Tính năng
Độ 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.