Skip to content

Commit 2b38971

Browse files
committed
Try slightly different logic; add more tests
1 parent 675b3b6 commit 2b38971

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4884,7 +4884,7 @@ def infer_context_dependent(
48844884
alt_typ = get_proper_type(
48854885
self.expr_checker.accept(expr, None, allow_none_return=allow_none_func_call)
48864886
)
4887-
if not msg.has_new_errors() and is_proper_subtype(alt_typ, typ):
4887+
if not msg.has_new_errors() and is_subtype(alt_typ, type_ctx):
48884888
self.store_types(type_map)
48894889
return alt_typ
48904890

test-data/unit/check-inference-context.test

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ def g(x: Optional[dict[str, Any]], s: Optional[str]) -> None:
15411541
f(x or {'x': s})
15421542
[builtins fixtures/dict.pyi]
15431543

1544-
[case testReturnFallbackInference]
1544+
[case testReturnFallbackInferenceTuple]
15451545
from typing import TypeVar, Union
15461546

15471547
T = TypeVar("T")
@@ -1554,3 +1554,31 @@ def bar2(x: list[int]) -> tuple[Union[str, int], ...]:
15541554
y = foo(x)
15551555
return y
15561556
[builtins fixtures/tuple.pyi]
1557+
1558+
[case testReturnFallbackInferenceUnion]
1559+
from typing import Generic, TypeVar, Union
1560+
1561+
T = TypeVar("T")
1562+
1563+
class Cls(Generic[T]):
1564+
pass
1565+
1566+
def inner(c: Cls[T]) -> Union[T, int]:
1567+
return 1
1568+
1569+
def outer(c: Cls[T]) -> Union[T, int]:
1570+
return inner(c)
1571+
1572+
[case testReturnFallbackInferenceAsync]
1573+
from typing import Generic, TypeVar, Optional
1574+
1575+
T = TypeVar("T")
1576+
1577+
class Cls(Generic[T]):
1578+
pass
1579+
1580+
async def inner(c: Cls[T]) -> Optional[T]:
1581+
return None
1582+
1583+
async def outer(c: Cls[T]) -> Optional[T]:
1584+
return await inner(c)

test-data/unit/pythoneval.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,3 +2188,19 @@ reveal_type([*map(str, x)])
21882188
[out]
21892189
_testUnpackIteratorBuiltins.py:4: note: Revealed type is "builtins.list[builtins.int]"
21902190
_testUnpackIteratorBuiltins.py:5: note: Revealed type is "builtins.list[builtins.str]"
2191+
2192+
[case testReturnFallbackInferenceDict]
2193+
# Requires full dict stubs.
2194+
from typing import Dict, Mapping, TypeVar, Union
2195+
2196+
K = TypeVar("K")
2197+
V = TypeVar("V")
2198+
K2 = TypeVar("K2")
2199+
V2 = TypeVar("V2")
2200+
2201+
def func(one: Dict[K, V], two: Mapping[K2, V2]) -> Dict[Union[K, K2], Union[V, V2]]:
2202+
...
2203+
2204+
def caller(arg1: Mapping[K, V], arg2: Mapping[K2, V2]) -> Dict[Union[K, K2], Union[V, V2]]:
2205+
_arg1 = arg1 if isinstance(arg1, dict) else dict(arg1)
2206+
return func(_arg1, arg2)

0 commit comments

Comments
 (0)