[Python] Why does mypy error on returning list[str, int] + list[int]?

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    From mypy's point of view l1 + l2 is OK. But returning l1 + l2 isn't OK. Why?

    I'm using Python 3.11 and mypy 1.16.

    def test() -> list[str | int]:
    l1: list[str | int]
    l2: list[int]
    l1 + l2 # OK
    return l1 + l2 # error: Unsupported operand types for + ("list[str | int]" and "list[int]") [operator]

    Continue reading...

Compartilhe esta Página