Skip to content

Commit a16521f

Browse files
authored
Infer constraints eagerly if actual is Any (#19190)
Fixes #8829 This case is more common in 1.16 due to some changes in binder, so it would be good to fix it. My fix may be a bit naive, but if `mypy_primer` looks good, I think it should be OK.
1 parent 68233f6 commit a16521f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

mypy/constraints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def _infer_constraints(
416416
infer_constraints_if_possible(t_item, actual, direction)
417417
for t_item in template.items
418418
],
419-
eager=False,
419+
eager=isinstance(actual, AnyType),
420420
)
421421
if result:
422422
return result

test-data/unit/check-inference.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4071,3 +4071,13 @@ def check_or_nested(maybe: bool) -> None:
40714071
reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]"
40724072
reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int]"
40734073
reveal_type(baz) # N: Revealed type is "builtins.list[builtins.int]"
4074+
4075+
[case testInferOptionalAgainstAny]
4076+
from typing import Any, Optional, TypeVar
4077+
4078+
a: Any
4079+
oa: Optional[Any]
4080+
T = TypeVar("T")
4081+
def f(x: Optional[T]) -> T: ...
4082+
reveal_type(f(a)) # N: Revealed type is "Any"
4083+
reveal_type(f(oa)) # N: Revealed type is "Any"

0 commit comments

Comments
 (0)