-
-
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
False-positive when using Union[Tuple[...]] #9791
Comments
You can go even simpler than that. from typing import Union
x: Union[str, int] = 1
print(x + x) |
Your simplification actually demonstrates a different issue: #2008, upon which we recently came to a consensus to change the behaviour. |
I guess the issues are different. Thanks for linking to the other issue 👍 I posted that example since there appears to be a typo in the original description that makes it seem the same. The code should probably be the following if it is referring to the issue that mypy doesn't remember "linking" between types: - print(element1[0] + element1[0])
+ print(element1[0] + element1[1]) |
@nmay231 You're right about the simplification and the typo. I edit my original post to fix typo even the problem still exists with the typo. |
I ran into this one which seems slightly different from #2008, and rather a more clearly bad behavior than the original post in this thread. Because x: List[Union[str, int]] = [1]
y: List[int] = [1]
z = x + y which also results in:
|
That one is intentional. |
Chiming in because I have a slightly different example which might be useful as an additional test: from typing import Union, List, Tuple
val1: Union[List[str], Tuple[str, ...]] = ['hi', 'bye']
val2: Union[List[str], Tuple[str, ...]] = ('hi', 'bye')
new1 = val1[:1] + val1[1:]
new2 = val2[:1] + val2[1:]
print(new1)
print(new2) The actual code I was using was using I'm not sure if this is #2008 or not, but the discussion there seems wide-ranging that they mention this bug there so I thought I'd add my comment here. I thought I'd also mention my current (non-ideal) workaround: I just cast to from typing import Union, List, Tuple, cast
val1: Union[List[str], Tuple[str, ...]] = ['hi', 'bye']
val2: Union[List[str], Tuple[str, ...]] = ('hi', 'bye')
val1 = cast(Tuple[str, ...], val1)
val2 = cast(Tuple[str, ...], val2)
new1 = val1[:1] + val1[1:]
new2 = val2[:1] + val2[1:]
print(new1)
print(new2) |
It's #2008 in that mypy would narrow the type of val1 if we had #2008 semantics, e.g. the following type checks:
But there is still an underlying issue which is that the code is sound even if the type is not narrowed. But mypy isn't going to fix that any time soon because it would involve tracking the provenance of the types of expressions. |
Thanks for the comment! I figured given the volume of discussion over on #2008 that this is going to happen when it happens, and I'm comfortable enough with my workaround to leave it in place for now. I considered simply creating duplicate conditional branches that separately |
Bug Report / To Reproduce
I have a incorrect
Unsupported operand types for + ("int" and "str")
when usingUnion[Tuple[...]]
.To Reproduce
Have a look at this example:
Run mypy.
Expected Behavior
No warning.
Actual Behavior
I have:
But it's wrong due to the fact that element1 is a Union (right) but it's the same variable on both side. So both type on the left and the right of the "+" should be the same.
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: