Skip to content

Fix crash on partial type used as context #19216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
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: 3 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3427,7 +3427,9 @@ def check_compatibility_all_supers(self, lvalue: RefExpr, rvalue: Expression) ->
# store the rvalue type on the variable.
actual_lvalue_type = None
if lvalue_node.is_inferred and not lvalue_node.explicit_self_type:
rvalue_type = self.expr_checker.accept(rvalue, lvalue_node.type)
# Don't use partial types as context, similar to regular code path.
ctx = lvalue_node.type if not isinstance(lvalue_node.type, PartialType) else None
rvalue_type = self.expr_checker.accept(rvalue, ctx)
actual_lvalue_type = lvalue_node.type
lvalue_node.type = rvalue_type
lvalue_type, _ = self.node_type_from_base(lvalue_node.name, lvalue_node.info, lvalue)
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -4128,3 +4128,24 @@ T = TypeVar("T")
def f(x: Optional[T]) -> T: ...
reveal_type(f(a)) # N: Revealed type is "Any"
reveal_type(f(oa)) # N: Revealed type is "Any"

[case testNoCrashOnPartialTypeAsContext]
from typing import overload, TypeVar, Optional, Protocol

T = TypeVar("T")
class DbManager(Protocol):
@overload
def get(self, key: str) -> Optional[T]:
pass

@overload
def get(self, key: str, default: T) -> T:
pass

class Foo:
def __init__(self, db: DbManager, bar: bool) -> None:
if bar:
self.qux = db.get("qux")
else:
self.qux = {} # E: Need type annotation for "qux" (hint: "qux: dict[<type>, <type>] = ...")
[builtins fixtures/dict.pyi]