python / python/mypy

New semantic analyzer: some assignments aren't recognized as types if incomplete

Đang mở
#6,985 0 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 priority-1-normal semantic-analyzer
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ả

In the following test case the forward reference to In in the class body doesn't work:

[case testNewAnalyzerNamedTupleCallNested]
from typing import NamedTuple
o: C.Out
i: C.In
reveal_type(o)  # E: Revealed type is 'Tuple[Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In], __main__.C.Other, fallback=__main__.C.Out]'
reveal_type(o.x)  # E: Revealed type is 'Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In]'
reveal_type(o.y)  # E: Revealed type is '__main__.C.Other'
reveal_type(o.x.t)  # E: Revealed type is '__main__.C.Other'
reveal_type(i.t)  # E: Revealed type is '__main__.C.Other'

class C:
    Out = NamedTuple('Out', [('x', In), ('y', Other)])   # 'In' undefined  <<--- should be ok?
    In = NamedTuple('In', [('s', str), ('t', Other)])
    class Other: pass

The root cause is that if the rvalue of an assignment is incomplete, we add a PlaceholderNode that doesn't have becomes_typeinfo set. This may also affect other things such as assignment-based TypedDicts, type aliases, type variables and NewTypes (haven't checked).

Not sure what would be the best way to fix this. One idea would be to treat every placeholder as a possible type, at least if it's generated from an incomplete assignment. A second idea would be to recognize possible types by looking at an incomplete rvalue, but this doesn't seem always possible.

This is follow-up to #6983.

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 bằng cách chạy trường hợp kiểm thử được cung cấp cho testNewAnalyzerNamedTupleCallNested và kiểm tra cách phép gán chưa hoàn chỉnh tạo ra một PlaceholderNode. Kiểm tra xem hành vi tương tự có ảnh hưởng đến các TypedDict dựa trên phép gán, bí danh kiểu, biến kiểu hoặc NewTypes như được nêu trong issue hay không. Công việc được xem là hoàn tất khi tham chiếu tiến tới được phân giải và các kỳ vọng reveal_type được liệt kê đều đạt mà không gây hồi quy cho các trường hợp 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ó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
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.