Skip to content

[1.18 regression] False negative with variable increment in a walrus inside a tuple #20606

@hamdanal

Description

@hamdanal

Bug Report

mypy sometimes fails to report errors in assignment expressions inside a tuple.

To Reproduce

def condition() -> bool: return False

def fn() -> tuple[int, int]:
    i: int | None = 0 if condition() else None
    return (i, (i := i + 1))

print(fn())

Expected Behavior

Something like

lib.py:5: error: Incompatible return value type (got "tuple[int | None, int]", expected "tuple[int, int]")  [return-value]
lib.py:5: error: Unsupported operand types for + ("None" and "int")  [operator]
lib.py:5: note: Left operand is of type "int | None"

Actual Behavior

Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 1.19.1 and master
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.14.2

The weird thing is that if I change assignment target to a different variable like (i, (j := i + 1)) mypy reports the two errors above.
Possibly related to mypyc/mypyc#1179

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-572PEP 572 (walrus operator)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions