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
Assigning a variable to the result of an untyped method removes any existing type constraints #3724
$ python3.6 -m mypy --strict-optional test_optional_assign.py
test_optional_assign.py:11: error: Revealed type is 'builtins.int'
test_optional_assign.py:13: error: Revealed type is 'Union[builtins.int, builtins.None]'
test_optional_assign.py:14: error: Unsupported operand types for + ("Optional[int]" and "int")
I would have expected the revealed type at like 13 to be Any.
I would have expected the revealed type at like 13 to be Any.
I think you mean "to be int". If yes then this is a binder problem. It may be related to #3526 and recently merged #3361. But unlike the former, this issues is probably easy to solve (I would guess just a small binder bug).
OK, following our short discussion on gitter I think that Any is fine here. As I mentioned earlier in #3526, I am fine with the idea of rebinding to Any (especially inside unions).
@msullivan I thought your PR #5629 could fix this as well, but it looks like the original example in this issue is still failing. Can we also special case this? Or this would require a more general fix?
I think mypy's current behavior is correct here. The (implicitly) declared type of x is int | None. Assigning a value of type Any to x should be permitted (for reasons of gradual typing), but this assignment shouldn't change the type of x. Its type should not be narrowed to Any in this case.
No mypy's behavior is not correct. Changing any precise type to Any should never create a new error. The example below works, and so the original one should too.
Activity
ilevkivskyi commentedon Jul 17, 2017
I think you mean "to be
int". If yes then this is a binder problem. It may be related to #3526 and recently merged #3361. But unlike the former, this issues is probably easy to solve (I would guess just a small binder bug).rowillia commentedon Jul 17, 2017
@ilevkivskyi I actually did expect the type to be
Any. The other case where this is biting us is below:In this case
razreally will setxto some integer but it's impossible to get this code to typecheck.ilevkivskyi commentedon Jul 17, 2017
OK, following our short discussion on gitter I think that
Anyis fine here. As I mentioned earlier in #3526, I am fine with the idea of rebinding toAny(especially inside unions).ilevkivskyi commentedon Dec 7, 2018
@msullivan I thought your PR #5629 could fix this as well, but it looks like the original example in this issue is still failing. Can we also special case this? Or this would require a more general fix?
erictraut commentedon Aug 10, 2023
I think mypy's current behavior is correct here. The (implicitly) declared type of
xisint | None. Assigning a value of typeAnytoxshould be permitted (for reasons of gradual typing), but this assignment shouldn't change the type ofx. Its type should not be narrowed toAnyin this case.ilevkivskyi commentedon Aug 28, 2023
No mypy's behavior is not correct. Changing any precise type to
Anyshould never create a new error. The example below works, and so the original one should too.Use union types instead of join in binder (python#18538)
Use union types instead of join in binder (python#18538)