Skip to content

Runs forever at 100% CPU for generic protocol with bounded TypeVar argument #20828

@opk12

Description

@opk12

Bug Report

This makes mypy run forever at 100% CPU.

Note that numpy must be installed, else mypy returns immediately (I guess it thinks np.float64 = Any).

I cannot reproduce if I remove any of the methods or replace np.float64 with float or str.

To Reproduce

from __future__ import annotations


from typing import Protocol, TypeVar


import numpy as np


I = TypeVar("I")


float_bound = TypeVar('float_bound', bound=float)
float_bound_2 = TypeVar('float_bound_2', bound=float)


class Container(Protocol[I]):

    def __abs__(self: Container[float_bound]) -> Container[float_bound]:
        ...

    def __rmul__(self: Container[float_bound], other: np.float64 | Container[float_bound_2], /) -> Container[float_bound | float_bound_2]:
        ...

    def __rtruediv__(self: Container[float_bound], other: np.float64 | Container[float_bound_2], /) -> Container[float_bound | float_bound_2]:
        ...

    def __sub__(self: Container[float_bound], other, /) -> Container[float_bound]:
        ...

    def __truediv__(self: Container[float_bound], other: np.float64 | Container[float_bound_2], /) -> Container[float_bound | float_bound_2]:
        ...

Actual Behavior

mypy prints no output, it runs forever at 100% CPU (PC fans constantly spinning).

Your Environment

Debian 13, with python3-numpy 1:2.2.4+ds-1

  • Mypy version used: both 1.15 from the Debian 13 repo and 1.19.1 from the Debian sid repo. I'm told this happens since mypy 1.9 or older, but I do not have the VM to confirm personally.
  • Mypy command-line flags: --ignore-missing-imports --check-untyped-defs --no-error-summary --no-implicit-reexport --pretty --strict-equality
  • Mypy configuration options from mypy.ini (and other config files): these files do not exist
  • Python version used: 3.13.5. I'm told this happened on older Python versions, but I do not have the VM to confirm personally.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions