-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
List concatenation problems with type context and different operand item types #5492
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
Comments
I think this is a consequence of how |
python/typeshed#2404 fixes this. |
(This comment is copied from #5778, which is a duplicate issue.) Code like this doesn't type check correctly (Python 2 example): from typing import List, Optional
result = range(10) + [None] # type: List[Optional[int]] It generates these errors:
Issues like these seem somewhat frequent. There are also similar examples in the mypy codebase. The errors have two causes:
A quick way to fix this particular example would be to change the signature of class list(Generic[_T]):
...
def __add__(self, x: List[_S]) -> List[Union[_T, _S]]: ...
... This signature might cause other problems, though. This wouldn't solve the problem in general. For example, if the annotation would say With some type system extensions we could fix this more generally. We'd need type variable lower bounds (we currently only have upper bounds), and to allow type variable bounds to refer to other type variables. I think that this would be a good signature: T = TypeVar('T')
S = TypeVar('S')
U = TypeVar('U', lower_bound=Union[T, S])
class list(Generic[T]):
def __add__(self, other: List[S]) -> List[U]: ... Implementing this could be difficult. Instead, we could special case |
Increasing priority to high since we have several independent sightings of this issue. |
python/typeshed#2404 caused regressions (see #5971 and #5874). I'm going to propose reverting it, since there seems to be no easy way to avoid the regressions in mypy. In summary, the PR fixed some issues but introduced others, which looks like a significant regression for existing mypy users. If we want to fix the original issue, the simplest way would likely be to special case |
The fix caused regressions for mypy that are difficult to fix. See python/mypy#5492 for context. This reverts commit 1a42a2c.
The fix caused regressions for mypy that are difficult to fix. See python/mypy#5492 for context. This reverts commit 1a42a2c.
The fix caused regressions for mypy that are difficult to fix. See python/mypy#5492 for context. This reverts commit 1a42a2c.
Just wanted to comment here as we run into this issue a lot in pandas. Here's a specific example: pandas-dev/pandas#29205 (comment) There doesn't appear to be a good way to say add lists and pass as an argument to a function all in one go. Instead we either have to break this apart into a few lines and use append/extend OR use unpacking like Understood limitations that caused this to be reverted in the first place; wanted to voice support for this issue in case someone has a good idea on how to make it work going forward! |
I was going to open an issue after getting an issue with list concatenation, but but I think it is a variation of this.
|
seems like mypy 0.981 might have (accidentally?) fix this issue. |
Well, not exactly an accident, given how long we spent discussing it in python/typeshed#8293 ;) But yup, this is fixed now! 🎉 |
Here is a reproduction snippet:
Error report looks like this:
The covariance of
Sequence
helps eliminate the first error. But I can never add two lists of objects of different classes without existing annotation, even the classes have same base.In my opinion,
cast
takes place when downcasting is necessary. So it should not be used in this way.The text was updated successfully, but these errors were encountered: