Skip to content

[mypyc] Fix infinite recursion on method call on union of subclasses - #22325

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1171-union-of-subclasses
Oct 30, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1171-union-of-subclasses

Conversation

@JukkaL

@JukkaL JukkaL commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Fixes mypyc/mypyc#1171.

Calling a method on a value whose type is a union of a class and its subclasses crashed the compiler with a RecursionError:

def f(objs: list[A | B]) -> None:  # B is a subclass of A
    for obj in objs:
        obj.method()

When generating the call for union item A, coercing the union value to A was a no-op, since the union is a subtype of A, so the value still had the union type and the union call was generated again. Now we use an unchecked cast in this case.

Fixes mypyc/mypyc#1171.

Calling a method on a value whose type is a union of a class and its
subclasses crashed the compiler with a `RecursionError`:

```python
def f(objs: list[A | B]) -> None:  # B is a subclass of A
    for obj in objs:
        obj.method()
```

When generating the call for union item `A`, coercing the union value to
`A` was a no-op, since the union is a subtype of `A`, so the value still
had the union type and the union call was generated again. Now we use
an unchecked cast in this case.
@JukkaL
JukkaL merged commit 40da371 into master Oct 30, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1171-union-of-subclasses branch October 30, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mypyc crashes with union type containing base class and its descendants in list iteration

2 participants