-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Assigning a variable to the result of an untyped method removes any existing type constraints #3724
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
@ilevkivskyi I actually did expect the type to be from typing import Any, Optional
def foo() -> Optional[int]:
return 42
raz: Any
def bar() -> int:
x = foo()
if x is None:
x = raz()
return x In this case |
OK, following our short discussion on gitter I think that |
@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 |
No mypy's behavior is not correct. Changing any precise type to from typing import Any, Optional
def foo() -> Optional[int]:
return 42
def bar() -> int:
x = foo()
if isinstance(x, int):
reveal_type(x)
x = 15
reveal_type(x)
x = x + 42
return 0 |
This would be more consistent with what we already do for ternary expressions. Note the change in match test results from match logic not handling well the situation when initial type is a union. A possible workaround would be to force "collapsing" union of tuples back into a tuple with union, but it is not easy and was planning to do some cleanup in the match handling as well (in particular it uses joins instead of unions in a way that will be inconsistent with new binder behavior). I want to put the switch from join to union for match statement in a separate PR. Note I also simplify a bunch of special-casing around `Any` in the binder that existed mostly because `join(Any, X) == Any`. Fixes python#3724
I would have expected the revealed type at like 13 to be
Any
.The text was updated successfully, but these errors were encountered: