python / python/mypy

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

Open
#21,879 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-descriptors topic-reachability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue and trace mypy's reachability analysis for the isinstance assertion, focusing on its treatment of FunctionType and MethodType. Add a regression test for the property-annotated case and verify that warn_unreachable produces no errors or warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.