python / python/mypy

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

Open
#20,828 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug crash topic-protocols topic-recursive-types
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

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

No source file or test path is identified in the report. Start with the supplied Protocol/TypeVar reproduction, with numpy installed and the listed mypy flags, then trace the checking path that fails to terminate; done means mypy completes on this input without sustained 100% CPU usage and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.