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]
foo: list[str | int] = l1 + l2either; it's not that you can't return it, so much that the result isn't assignable tolist[str | int]. Possibly related to e.g. github.com/python/mypy/issues/3351.