Skip to content

[1.20 regression] "Unsupported operand types for |" with recursive type definition #21141

@latentvoid

Description

@latentvoid

Bug Report

When merging dicts with a value type that is union containing a recursive type (e.g. A = dict[str, A | int]), mypy incorrectly detects an "Unsupported operand types" error. This worked in version 1.19.1.

To Reproduce

type A = dict[str, A | int]		# can use arbitrary types instead of `str` and `int`

d1: A
d2: A
d3: A = {**d1, **d2}		# works
d4: A = d1 | d2		# equivalent to line above but fails
d5 = d1 | d2
d6: A = d5		# works (similar behavior to what was reported in https://github.com/python/mypy/issues/18236)

type B = dict[str, B]

d7: B
d8: B
d9: B = d7 | d8		# no union in type def works

type C = dict[str, float | int]

d10: C
d11: C
d12: C = d10 | d11		# no recursive type def works

Playground URL: https://mypy-play.net/?mypy=master&python=3.12&gist=1c39fc367614e2a0af2bd4d604c25aeb

The mypy playground seems to be bugged currently. Selecting version "mypy 1.20.0" results in HTTP 500 and "mypy latest (1.20.0)" does not reproduce the bug even though it should. "mypy master branch" does reproduce the bug.

Expected Behavior

None of the examples above should produce an error.

Actual Behavior

Mypy produces error: Unsupported operand types for | ("dict[str, A | int]" and "dict[str, A | int]") [operator]

Your Environment

  • Mypy version used: 1.20.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.12, 3.12.13, 3.14.3

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