Skip to content

list.__add__ isn't permissive enough #8292

Closed
@Akuli

Description

@Akuli

Works at runtime, doesn't type check:

class Foo:
    def asd(self) -> int:
        return 1

class Bar:
    def asd(self) -> int:
        return 2

combined = [Foo()] + [Bar()]
for item in combined:
    print(item.asd())

I would expect [Foo()] + [Bar()] to produce list[Foo | Bar].

Converting to tuples works as expected:

combined = tuple([Foo()]) + tuple([Bar()])

But invariance problems don't apply here, because we're constructing a new list that doesn't refer to the old lists in any way.

There has previously been discussion about how mypy doesn't understand [Foo(), Bar()], but this is slightly different: I'm adding lists, not mixing types within different elements of a single list literal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions