python / python/mypy

False positive comparison-overlap for nested function and nested class

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

bug
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ả

Bug Report

Nested functions inside methods incorrectly resolve attributes of their first argument relative to the surrounding class.

To Reproduce

class foo:
    a: str = "foo"
    
class bar:
    class a:
        pass
    
    def test(self) -> None:
        def inner1(f: foo, s: str) -> bool:
            return f.a == s
        
        def inner2(s: str, f: foo) -> bool:
            return f.a == s

https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=4c27f653074cd1c3a48bb16f49ca2168

Expected Behavior

As far as I can tell this should be valid code, even though this rarely happens in practice due to CamelCase vs snake_case naming conventions.

Actual Behavior

inner1 fails to type-check, because the a attribute is incorrectly assumed to be the a class inside bar

main.py:10: error: Non-overlapping equality check (left operand type: "Type[a]", right operand type: "str")  [comparison-overlap]

Interestingly, inner2 is fine.

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11

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

Tái hiện ví dụ từ issue trong mypy với Python 3.11 và --strict, sau đó theo dõi cách các hàm lồng nhau phân giải việc truy cập thuộc tính trong phép so sánh. Bản sửa được hoàn tất khi inner1 không còn báo lỗi comparison-overlap, trong khi inner2 vẫn hợp lệ và hồi quy được bao phủ bởi một bài kiểm thử.

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
devtools
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
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.