python / python/mypy

Mypy fails to type check function signature after plugin pass

Đang mở
#18,166 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 topic-plugins
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

I wrote a plugin to replace types, but Mypy fails to typecheck after my changes. I start off trying to fix the type error in this signature with a plugin:

def almost_id(a: str) -> str:
    return int(a)

almost_id("6")

with my plugin:

def replace_str(self, ctx: FunctionSigContext) -> FunctionLike:
        api = ctx.api

        new_signature = ctx.default_signature.copy_modified(
            ret_type=api.named_type('builtins.int')
        )

        un_anal = new_signature.definition.unanalyzed_type.copy_modified(
            ret_type = api.named_type('builtins.int')
        )

        new_signature.definition.unanalyzed_type = un_anal

        typ = new_signature.definition.type.copy_modified(
            ret_type = api.named_type('builtins.int')
        )

        new_signature.definition.type = typ

        return new_signature

I have tried going through the attrs of the functionsigcontext, api and the expr_checker but can't seem to find some other functionality to either:

  • force mypy to proceed with type checking
  • modify the existing type analysis or map
  • delay type checking until the plugin mods have been completed, something like defer() in the semantic analyzer.

To Reproduce
gist: https://gist.github.com/dibrinsofor/bddcbd2eedbb233b131bb1b8d562369a

mypy tests/str_int.py

Expected Behavior

I expect mypy to pass

Actual Behavior

tests/str_int.py:2: error: Incompatible return value type (got "int", expected "str")  [return-value]

even though printing the new signature returns:

def (a: builtins.str) -> builtins.int

Your Environment

  • Mypy version used: 1.14.0+dev.8c7db13f71098a6facea66ab165b6bc3ef688bdf
  • Mypy command-line flags: --show-traceback
  • Python version used: Python 3.11.5

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

Chạy mypy tests/str_int.py bằng bản tái hiện trong gist và so sánh lỗi được báo cáo với chữ ký được in ra sau replace_str. Theo dõi cách kết quả của FunctionSigContext được sử dụng trong quá trình phân tích ngữ nghĩa và kiểm tra biểu thức, bằng các đường dẫn expr_checkerdefer() được đề cập trong báo cáo. Hoàn tất khi kiểu trả về do plugin sửa đổi được tôn trọng và bản tái hiện chạy thành công.

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