Skip to content

Commit 41f9c2b

Browse files
committed
Do not use combined context if LHS has Any
1 parent e0ce3e3 commit 41f9c2b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6004,6 +6004,10 @@ def analyze_cond_branch(
60046004
def _combined_context(self, ty: Type | None) -> Type | None:
60056005
ctx_items = []
60066006
if ty is not None:
6007+
if has_any_type(ty):
6008+
# HACK: Any should be contagious, `dict[str, Any] or <x>` should still
6009+
# infer Any in x.
6010+
return ty
60076011
ctx_items.append(ty)
60086012
if self.type_context and self.type_context[-1] is not None:
60096013
ctx_items.append(self.type_context[-1])

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,3 +1530,13 @@ def check3(use: bool, val: str) -> "str | Literal[False]":
15301530
def check4(use: bool, val: str) -> "str | bool":
15311531
return use and identity(val)
15321532
[builtins fixtures/tuple.pyi]
1533+
1534+
[case testDictAnyOrLiteralInContext]
1535+
from typing import Union, Optional, Any
1536+
1537+
def f(x: dict[str, Union[str, None, int]]) -> None:
1538+
pass
1539+
1540+
def g(x: Optional[dict[str, Any]], s: Optional[str]) -> None:
1541+
f(x or {'x': s})
1542+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)