Skip to content

[1.20 regression] unreachable regression with generic Callable #21182

Closed

Description

@dpinol

Bug Report

Since mypy 1.20.0, a comparison between 2 generic callables may cause "Statement is unreachable"

To Reproduce

from typing import Callable

def identity[T](x: T) -> T:
    return x

def msg[T,S](cmp_property: Callable[[T], S]) -> None:
    if cmp_property == identity:
        return

Expected Behavior

Mypy should not report any error.

Actual Behavior

scpytools/kk.py: note: In function "msg":
scpytools/kk.py:10: error: Statement is unreachable  [unreachable]
            return
            ^~~~~~

Your Environment

  • Mypy version used: mypy 1.20.0 (compiled: yes)
  • Mypy command-line flags: mypy --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): No config files
  • Python version used: 3.14.2

Activity

  1. changed the title [-]unreachable regression with generic Callable[/-] [+][1.20 regression] unreachable regression with generic Callable[/+] on Apr 7, 2026
  2. hauntsaninja commented on Apr 9, 2026

    @hauntsaninja
    Collaborator

    Thanks, changes in 1.20 surface a pre-existing deficiency in is_overlapping_types, will look more...

  3. hauntsaninja commented on Apr 13, 2026

    @hauntsaninja
    Collaborator

    One idea for a fix is this:

    --- a/mypy/meet.py
    +++ b/mypy/meet.py
    @@ -548,6 +551,14 @@ def is_overlapping_types(
                 is_proper_subtype=False,
                 ignore_pos_arg_names=not overlap_for_overloads,
                 allow_partial_overlap=True,
    +        ) or is_callable_compatible(
    +            right,
    +            left,
    +            is_compat=_is_overlapping_types,
    +            is_proper_subtype=False,
    +            ignore_pos_arg_names=not overlap_for_overloads,
    +            check_args_covariantly=True,
    +            allow_partial_overlap=True,
             )
    

    This is nice because it obviously solves any other symmetry issues, but maybe it is a bandaid over some deeper issue

  4. hauntsaninja commented on Apr 13, 2026

    @hauntsaninja
    Collaborator

    Okay, it looks like this is known and we do something similar elsewhere, see e.g. https://github.com/python/mypy/pull/5476/changes#diff-f96a2d6138bc6cdf2a07c4d37f6071cc25c1631afc107e277a28d5b59fc0ef04R3755 so I will open the PR

  5. added a commit that references this issue on Apr 13, 2026
    0ed9283
  6. added a commit that references this issue on Apr 13, 2026
    8f2e6ad
  7. added a commit that references this issue on Apr 21, 2026
    0dcbfaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions