python / python/mypy

Crash on diverging recursive generic protocols

Open
#17,326 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

EDIT: Although the original repro is not causing a crash anymore, the underlying issue is still there, see https://github.com/python/mypy/issues/17326#issuecomment-2151135134

Crash Report

Not sure how to title this, the following example produces a segfault, I simplified it from more complicated code. It seems to be multicausal, removing the Literal makes it disappear, so does removing the __or__ operator on Transform.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=2409c60fc1d5734569f6ab55344a85f0

from typing import Any, Literal, Protocol, TypeVar, overload

X = TypeVar("X")
Y = TypeVar("Y")
Z = TypeVar("Z")
X2 = TypeVar("X2")
Y2 = TypeVar("Y2")


class Transform(Protocol[X, Y]):
    def __or__(
        self, other: "Transform[X2, Y2]", /
    ) -> "Transform[tuple[X, X2], tuple[Y, Y2]]": ...


@overload  # 1 arg
def chain_encoders(
    e: Transform[X, Y], /, *, simplify: Literal[True] = ...
) -> Transform[X, Y]: ...
@overload  # ≥2 args
def chain_encoders(
    *es: *tuple[Transform[Any, Y], *tuple[Transform, ...], Transform[X, Any]],
    simplify: Literal[True] = ...,
) -> Transform[X, Y]: ...
def chain_encoders(*encoders: Transform, simplify: bool = True) -> Transform:
    r"""Chain encoders."""

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 by running the linked mypy-play reproduction with the current mypy version and compare it with the linked issue comment, since the original crash no longer reproduces. Isolate the effects of Literal and Transform.or while tracing the remaining failure. The issue names no repository files or tests, so a completed fix would need a new regression test for the confirmed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.