Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6004,6 +6004,10 @@ def analyze_cond_branch(
def _combined_context(self, ty: Type | None) -> Type | None:
ctx_items = []
if ty is not None:
if has_any_type(ty):
# HACK: Any should be contagious, `dict[str, Any] or <x>` should still
# infer Any in x.
return ty
ctx_items.append(ty)
if self.type_context and self.type_context[-1] is not None:
ctx_items.append(self.type_context[-1])
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-inference-context.test
Original file line number Diff line number Diff line change
Expand Up @@ -1530,3 +1530,13 @@ def check3(use: bool, val: str) -> "str | Literal[False]":
def check4(use: bool, val: str) -> "str | bool":
return use and identity(val)
[builtins fixtures/tuple.pyi]

[case testDictAnyOrLiteralInContext]
from typing import Union, Optional, Any

def f(x: dict[str, Union[str, None, int]]) -> None:
pass

def g(x: Optional[dict[str, Any]], s: Optional[str]) -> None:
f(x or {'x': s})
[builtins fixtures/dict.pyi]
Loading