Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List concat with different types causes error #720

Closed
noisecapella opened this issue Jul 8, 2015 · 9 comments
Closed

List concat with different types causes error #720

noisecapella opened this issue Jul 8, 2015 · 9 comments

Comments

@noisecapella
Copy link

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?

@gvanrossum
Copy link
Member

gvanrossum commented Jul 8, 2015 via email

@noisecapella
Copy link
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.

@graingert
Copy link
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.

@graingert
Copy link
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.

@fivepapertigers
Copy link

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.

brickman1444 added a commit to code-312/greater-chicago-food-despository that referenced this issue Apr 26, 2021
I'm silencing a mypy error on appending lists of different types:
error: Unsupported operand types for + ("List[str]" and "List[int]")
It looks like the fixes to correct the overzealous type error wouldn't be worth implementing: python/mypy#720
brickman1444 added a commit to code-312/greater-chicago-food-despository that referenced this issue Apr 29, 2021
* Start changing the wic participation data into using multiple dataframes

* Clean up some code that isn't needed

* -Write four CSV files instead of one confusing one.
-Pass the participation data back in so it can be merged in with the census data

* Cover the new WIC code in tests

* Fix flake8 errors

* Fix missing returns

* Set Up Test for Merging WIC Participation Data into Census Data

Changed the WIC participation dataframes to actually have the fips column as the index. This changed the json format of a lot of the fixtures.

Add new test to test merging. This test currently fails because the code isn't implemented yet

* Get merging tests passing

* Change the data type of the wic participation numbers to ints

* Get the main path working

* Fix flake8 and mypy errors

I'm silencing a mypy error on appending lists of different types:
error: Unsupported operand types for + ("List[str]" and "List[int]")
It looks like the fixes to correct the overzealous type error wouldn't be worth implementing: python/mypy#720

* add main test (#45)

* added test_run

* set fail fast to false, allows determination of os/version specific errors

* replaced .loc[] with reindex(), due to key error

* Fix Error in Test test_run()

ValueError: invalid literal for int() with base 10: '1,044'

* Fix flake8 error

Co-authored-by: michaelpkuhn <mickuhn95@gmail.com>
@JackLeo
Copy link

JackLeo commented 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
Copy link

Randl commented Jun 16, 2022

I agree that this should be re-opened

@miyukiko-su
Copy link

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

@JelleZijlstra
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants