Skip to content

List concat with different types causes error #720

Closed

Description

@noisecapella

When I run mypy on this code:

from typing import List, Any

def f(a: List[int], b: List[str]):
    return a+b

f([1,2,3], ["3","4","5"])

I get this error:
/home/georgeatmit/type1.py: note: In function "f":
/home/georgeatmit/type1.py:3: error: Unsupported operand types for + ("list" and List[str])

Is combining lists of different types supported at the moment?

Activity

  1. gvanrossum commented on Jul 8, 2015

    @gvanrossum
    Member
  2. noisecapella commented on Jul 9, 2015

    @noisecapella
    Author

    I agree that this looks like a programming mistake, I was wondering about the general rule of combining collections with different types in Python's type hinting syntax, and a bit confused with the error message, why List[int] shows up as "list" but List[str] is represented correctly. I'll do some investigating and file another issue if I find anything.

  3. graingert commented on Jul 25, 2017

    @graingert
    Contributor
    from typing import List, Any
    
    def f(a: List[int], b: List[str]) -> List[Union[str, int]]:
        return a+b
    
    f([1,2,3], ["3","4","5"])

    should be totally valid.

  4. graingert commented on Jul 25, 2017

    @graingert
    Contributor
    _T = TypeVar('_T')
    _U = TypeVar('_U')
    
    def list_add(a: Iterable[_T], b: Iterable[_U]) -> Iterable[Union[_T, _U]]:
        items: Iterable[Iterable[Union[_T, _U]] = [a, b]
        return itertools.chain.from_iterable(items)
    
    f: Callable[[List[int], List[str]], List[Union[str, int]] = list_add

    should do this without cast.

  5. fivepapertigers commented on Jun 9, 2020

    @fivepapertigers

    I wish this weren't closed.

    List[A] + List[B] should be inferred as List[Union[A, B]]. Sure this could often simply be a mistake, but ideally that would be caught downstream when the resulting list would not match an expected type.

    a: List[int] = [1, 3]
    b: List[str] = ['foo', 'bar']
    c = a + b # this should work fine with the implied type of List[Union[int, str]]
    for item in c:
        print(item + 5) # this is where I would expect the mypy error

    In my use-case I want to make the trade-off the headaches of supporting a mixed collection to support a more intuitive interface for the users of my API. But now I have to explicitly type c or case a and b in order to support that - adding to the headache.

  6. JackLeo commented on Oct 11, 2021

    @JackLeo

    Ran into the same issue by trying to have the possibility of None in the list as I'm generating schema.

    Converting to tuple did work as tuples do support mixed types and undefined lenght so you can have Tuple[Optional[str], ...] but if anything this feels more of a hack as imutability does not matter in that particular case.

    It's great to assume error by default, but types should accept mixed cases if provided as the language does.

    This should not be closed, or should be linked to more appropriate bug report.

  7. Randl commented on Jun 16, 2022

    @Randl

    I agree that this should be re-opened

  8. miyukiko-su commented on Oct 5, 2023

    @miyukiko-su

    Any updates? It's still very relevant...
    Just faced the same situation, and I don't actually want to create workarounds every time...

  9. JelleZijlstra commented on Oct 5, 2023

    @JelleZijlstra
    Member

    The original example typechecks for me now, likely thanks to python/typeshed#8293. If you have a different problem, please open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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