Skip to content

Commit cb3c6ec

Browse files
ilevkivskyiJukkaL
authored andcommitted
Fix crash on partial type used as context (#19216)
Fixes #19213
1 parent c39f5e7 commit cb3c6ec

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
@@ -3426,7 +3426,9 @@ def check_compatibility_all_supers(self, lvalue: RefExpr, rvalue: Expression) ->
34263426
# store the rvalue type on the variable.
34273427
actual_lvalue_type = None
34283428
if lvalue_node.is_inferred and not lvalue_node.explicit_self_type:
3429-
rvalue_type = self.expr_checker.accept(rvalue, lvalue_node.type)
3429+
# Don't use partial types as context, similar to regular code path.
3430+
ctx = lvalue_node.type if not isinstance(lvalue_node.type, PartialType) else None
3431+
rvalue_type = self.expr_checker.accept(rvalue, ctx)
34303432
actual_lvalue_type = lvalue_node.type
34313433
lvalue_node.type = rvalue_type
34323434
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
@@ -3979,3 +3979,24 @@ def check(mapping: Mapping[str, _T]) -> None:
39793979
reveal_type(ok1) # N: Revealed type is "Union[_T`-1, builtins.str]"
39803980
ok2: Union[_T, str] = mapping.get("", "")
39813981
[builtins fixtures/tuple.pyi]
3982+
3983+
[case testNoCrashOnPartialTypeAsContext]
3984+
from typing import overload, TypeVar, Optional, Protocol
3985+
3986+
T = TypeVar("T")
3987+
class DbManager(Protocol):
3988+
@overload
3989+
def get(self, key: str) -> Optional[T]:
3990+
pass
3991+
3992+
@overload
3993+
def get(self, key: str, default: T) -> T:
3994+
pass
3995+
3996+
class Foo:
3997+
def __init__(self, db: DbManager, bar: bool) -> None:
3998+
if bar:
3999+
self.qux = db.get("qux")
4000+
else:
4001+
self.qux = {} # E: Need type annotation for "qux" (hint: "qux: Dict[<type>, <type>] = ...")
4002+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)