Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#21,879 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
50/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
compilers

调研方向

从 issue 中的复现用例开始,跟踪 mypy 对 isinstance 断言的可达性分析,重点关注其对 FunctionType 和 MethodType 的处理。为使用 property 注解的情况添加回归测试,并验证 warn_unreachable 不会产生任何错误或警告。

由索引模型根据 Issue 内容生成。

描述

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]
主要语言
Python
星标
20.6k
派生
3.3k
平均合并
1 天 12 小时
30 天内合并 PR
58

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。