You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
List concat with different types causes error #720
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?
I think it's fine to get an error for this -- it's more likely to be a
programming mistake than intentional. Maybe you can use `cast()` to remove
the warning if this is really what you want?
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.
Reacted by Michael Lefkowitz, Giorgio Balestrieri, Markus Unterwaditzer, José Enrique Narváez Gago, Hannes, Domantas Jackūnas, Laurens Duijvesteijn, Bazyli Cyran, Jeffrey Terry, David Rajaratnam and 4 more
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]]foriteminc:
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.
Reacted by Dustin Martin, Giorgio Balestrieri, epervago, Till Ehrengruber, Kevin Mader, Markus Unterwaditzer, José Enrique Narváez Gago, Romain Mondon Cancel, Eldar Bogdanov, Hannes and 19 more
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.
Activity
gvanrossum commentedon Jul 8, 2015
noisecapella commentedon Jul 9, 2015
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"butList[str]is represented correctly. I'll do some investigating and file another issue if I find anything.graingert commentedon Jul 25, 2017
should be totally valid.
graingert commentedon Jul 25, 2017
should do this without cast.
fivepapertigers commentedon Jun 9, 2020
I wish this weren't closed.
List[A] + List[B]should be inferred asList[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.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
cor caseaandbin order to support that - adding to the headache.Fix flake8 and mypy errors
Make WIC Participation Data Available to Frontend (#44)
JackLeo commentedon Oct 11, 2021
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.
Randl commentedon Jun 16, 2022
I agree that this should be re-opened
miyukiko-su commentedon Oct 5, 2023
Any updates? It's still very relevant...
Just faced the same situation, and I don't actually want to create workarounds every time...
JelleZijlstra commentedon Oct 5, 2023
The original example typechecks for me now, likely thanks to python/typeshed#8293. If you have a different problem, please open a new issue.