Skip to content

Corner case causes arg-type error: type is incompatible with itself #20608

@mwchase

Description

@mwchase

Bug Report

With a very specific combination of features, Mypy rejects a decorator application, claiming that a callable type is incompatible with apparently the same callable type.

To Reproduce

from collections.abc import Callable
from typing import Never

import attrs


# In the actual implementation, most classes are attrs classes, but this
# appears to be the only one needed to make the bug manifest.
@attrs.frozen
class ValueTerm[F]:
    pass


class ValueType[F]:
    pass


class Bundle[T]:
    data: T


class AlmostNever:
    """Replacing Never with this makes the bug disappear.

    At least, at this level of replication. Paring it down further causes weird
    behavior changes.
    """


def bundlemethod[T, U](method: Callable[[Bundle[T]], U]) -> Callable[[T], U]:
    raise NotImplementedError


@bundlemethod
def fail_value_type_inference[F](
    self_bundle: Bundle[ValueTerm[F]], /
) -> (
    tuple[Never, Bundle[ValueType[F]]] | tuple[int, Bundle[ValueType[F | str]]]
):
    raise NotImplementedError

The code that hit this was using Generator instead of tuple.

Actual Behavior

mypy_repro.py:34: error: Argument 1 to "bundlemethod" has incompatible type "Callable[[Bundle[ValueTerm[F]]], tuple[Never, Bundle[ValueType[F]]] | tuple[int, Bundle[ValueType[F | str]]]]"; expected "Callable[[Bundle[ValueTerm[F]]], tuple[Never, Bundle[ValueType[F]]] | tuple[int, Bundle[ValueType[F | str]]]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.20.0+dev.3cae656ce264f00ac5417b0559a8cba1453f5eb5 (compiled: no) also encountered with mypy 1.19.1 (compiled: yes)
  • Mypy command-line flags: --strict or nothing
  • Mypy configuration options from mypy.ini (and other config files): Was using unrelated plugin for deal, but commented out those lines, and still reproduced.
  • Python version used: Python 3.13.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions