FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Unsupported operand types for + (List[A] and List[B]) when B extends A · Issue #3933 · python/mypy · GitHub

Repository navigation

Unsupported operand types for + (List[A] and List[B]) when B extends A #3933

Open
randolf-scholz/mypy
#4

Description

If I have:

Class A:

Class B(A):

Then If I try to add lists of these two objects you get the following mypy error:
error: Unsupported operand types for + (List[A] and List[B])

Any ideas for a work around on this one? Surely the inferred type should be List[Union[A, B]

Activity

  1. gvanrossum commented on Sep 7, 2017

    Member

    Please include full self-contained examples of code showing the error you are experiencing. It makes helping you so much easier

    I suppose you were writing something like

    x = [A(), A()]
    y = [B(), B()]
    z = x + y  # error here
    

    right? Your problem here is that the type of x is List[A], and the type of y is List[B]. Because the List type is invariant this is rejected. You can fix it by adding # type: List[A] to the assignment to y.

  2. JukkaL commented on Sep 7, 2017

    Collaborator

    Actually x + y would be safe if the inferred type of the result was List[A]. There currently is no way to represent this in the type system, though. We could probably write a type system plugin to support this if this seems to be a common enough issue.

  3. henryJack commented on Sep 7, 2017

    Author

    Yes, sorry. I'll try to be more explicit:

    If I declare the following classes:

    Class A:
    
    Class B(A):
    
    Class C(A):
    
    Class D(B):
    

    Then I prepare some pure lists:

    w = [A(), A()]
    x = [B(), B()]
    y = [C(), C()]
    z = [D(), D()]
    

    Now, if I add any combination of different lists I will get the mypy error error: Unsupported operand types for + (List[X] and List[Y]) :

    new_list = w + x  # should be type: List[A]
    new_list = w + y  # should be type: List[A]
    new_list = w + z  # should be type: List[A]
    new_list = x + y  # should be type: List[A]
    new_list = x + z  # should be type: List[B]
    new_list = y + z  # should be type: List[A]
    

    i.e., The highest common Parent class should become the type of the new list

  4. henryJack commented on Sep 7, 2017

    Author

    Then of course,

    new_list = x + z + w  # should be type: List[A]
    

    Because the highest common parent class is now A

  5. gvanrossum commented on Sep 7, 2017

    Member

    @henryJack Without writing a mypy plugin your only type-safe way to get this to work is to add explicit declarations so that all lists have type List[A].

  6. mokulus commented on Aug 7, 2024

    This is still a problem:

    mix: list[int | str] = [1, "foo"]
    strings: list[str] = ["bar"]
    
    # mix = mix + strings  # doesn't work
    mix = [*mix, *strings]  # works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL