-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Comments
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 |
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. |
_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. |
I wish this weren't closed.
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 |
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
* 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 <[email protected]>
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 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. |
I agree that this should be re-opened |
Any updates? It's still very relevant... |
The original example typechecks for me now, likely thanks to python/typeshed#8293. If you have a different problem, please open a new issue. |
When I run mypy on this code:
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?
The text was updated successfully, but these errors were encountered: