Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Property annotated as `FunctionType` triggers false unreachable error on `isinstance` check

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

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
50/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu với reproducer trong issue và lần theo quá trình phân tích khả năng tiếp cận của mypy đối với assertion isinstance, tập trung vào cách nó xử lý FunctionType và MethodType. Thêm một regression test cho trường hợp được chú thích bằng property và xác minh rằng warn_unreachable không tạo ra lỗi hoặc cảnh báo nào.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug topic-descriptors topic-reachability

Bug Report

Mypy incorrectly reports an unreachable error when assert isinstance(..., FunctionType) on a @property annotated as returning FunctionType, even though the constructor enforces the invariant.
This is a false positive: no MethodType is involved, and the assertion is trivially true.

Also, the option that enables this error is warn_unreachable. Is this supposed to be a warning instead of an error?

As an aside, while asserting that a defined function (def func(): ...) is an instance of FunctionType is True, if a parameter of a function/method defines func: FunctionType, mypy will raise a arg-type error at the call site (error: Argument 1 to "FuncWrap" has incompatible type "Callable[[], int]"; expected "FunctionType" [arg-type]). This also seems like a false positive, as raw function definitions are implicitly of type FunctionType. Not what this bug is about, but something I ran into designing my wrapper.

To Reproduce

from types import FunctionType

class FuncWrap:
    def __init__(self, func: object) -> None:
        if not isinstance(func, FunctionType):
            raise TypeError()
        self._func = func

    @property
    def __func__(self) -> FunctionType:
        return self._func

def good() -> int:
    return 1

wrapped = FuncWrap(good)

assert isinstance(wrapped.__func__, FunctionType)

If you replace the assert with assert wrapped.__func__ is good, mypy reports a new comparison-overlap error: error: Non-overlapping identity check (left operand type: "MethodType", right operand type: "FunctionType") [comparison-overlap].

gist
playground

Expected Behavior

No errors/warnings.

Actual Behavior

main.py:18: error: Subclass of "MethodType" and "FunctionType" cannot exist: "MethodType" is final  [unreachable]
main.py:18: error: Subclass of "MethodType" and "FunctionType" cannot exist: "FunctionType" is final  [unreachable]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 2.3.1, 1.20.x, 1.10.x
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files):
    [tool.mypy]
      cache_dir = "$MYPY_CONFIG_FILE_DIR/.mypy_cache"
      check_untyped_defs = true
      color_output = true
      disallow_any_generics = true
      disallow_incomplete_defs = true
      disallow_untyped_decorators = true
      disallow_untyped_defs = true
      exclude = []
      explicit_package_bases = false
      files = [ "src/", "tests/" ]
      implicit_optional = false
      implicit_reexport = false
      incremental = true
      local_partial_types = true
      mypy_path = "$MYPY_CONFIG_FILE_DIR/src"
      namespace_packages = false
      native_parser = true
      num_workers = 2
      plugins = []
      python_version = "3.11"
      show_error_codes = true
      show_error_context = true
      strict = true
      strict_equality = true
      strict_optional = true
      warn_no_return = true
      warn_redundant_casts = true
      warn_return_any = true
      warn_unreachable = true
      warn_unused_configs = true
      warn_unused_ignores = true
    
  • Python version used: 3.[11-14]
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 12 giờ
Pull request đã merge (30 ngày)
58

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.