Skip to content

Commit 29d8f06

Browse files
authored
Fix crash on partial type used as context (#19216)
Fixes #19213
1 parent 1f339c0 commit 29d8f06

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

mypy/checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3427,7 +3427,9 @@ def check_compatibility_all_supers(self, lvalue: RefExpr, rvalue: Expression) ->
34273427
# store the rvalue type on the variable.
34283428
actual_lvalue_type = None
34293429
if lvalue_node.is_inferred and not lvalue_node.explicit_self_type:
3430-
rvalue_type = self.expr_checker.accept(rvalue, lvalue_node.type)
3430+
# Don't use partial types as context, similar to regular code path.
3431+
ctx = lvalue_node.type if not isinstance(lvalue_node.type, PartialType) else None
3432+
rvalue_type = self.expr_checker.accept(rvalue, ctx)
34313433
actual_lvalue_type = lvalue_node.type
34323434
lvalue_node.type = rvalue_type
34333435
lvalue_type, _ = self.node_type_from_base(lvalue_node.name, lvalue_node.info, lvalue)

test-data/unit/check-inference.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,3 +4128,24 @@ T = TypeVar("T")
41284128
def f(x: Optional[T]) -> T: ...
41294129
reveal_type(f(a)) # N: Revealed type is "Any"
41304130
reveal_type(f(oa)) # N: Revealed type is "Any"
4131+
4132+
[case testNoCrashOnPartialTypeAsContext]
4133+
from typing import overload, TypeVar, Optional, Protocol
4134+
4135+
T = TypeVar("T")
4136+
class DbManager(Protocol):
4137+
@overload
4138+
def get(self, key: str) -> Optional[T]:
4139+
pass
4140+
4141+
@overload
4142+
def get(self, key: str, default: T) -> T:
4143+
pass
4144+
4145+
class Foo:
4146+
def __init__(self, db: DbManager, bar: bool) -> None:
4147+
if bar:
4148+
self.qux = db.get("qux")
4149+
else:
4150+
self.qux = {} # E: Need type annotation for "qux" (hint: "qux: dict[<type>, <type>] = ...")
4151+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)