KEMBAR78
Fix inference with UninhabitedType by cdce8p · Pull Request #16994 · python/mypy · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Deal with edge case
  • Loading branch information
cdce8p committed Mar 7, 2024
commit cd4600f5bf48db080307447a6eed2bce83da8b7a
4 changes: 2 additions & 2 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def join_instances(self, t: Instance, s: Instance) -> ProperType:
# TODO: contravariant case should use meet but pass seen instances as
# an argument to keep track of recursive checks.
elif type_var.variance in (INVARIANT, CONTRAVARIANT):
if isinstance(ta_proper, UninhabitedType):
if isinstance(ta_proper, UninhabitedType) and not ta_proper.is_noreturn:
new_type = sa
elif isinstance(sa_proper, UninhabitedType):
elif isinstance(sa_proper, UninhabitedType) and not sa_proper.is_noreturn:
new_type = ta
elif not is_equivalent(ta, sa):
self.seen_instances.pop()
Expand Down
9 changes: 8 additions & 1 deletion test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3815,7 +3815,7 @@ reveal_type(Combine(m1, m2)) # N: Revealed type is "builtins.float"
[builtins fixtures/list.pyi]

[case testInferenceWithUninhabitedType]
from typing import Dict, Generic, TypeVar
from typing import Dict, Generic, List, Never, TypeVar

T = TypeVar("T")

Expand All @@ -3839,4 +3839,11 @@ def a3() -> None:
reveal_type(a) # N: Revealed type is "builtins.dict[Any, Any]"
reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, builtins.dict[Any, Any]]"
reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.dict[builtins.str, builtins.dict[Any, Any]]]"

def a4(x: List[str], y: List[Never]) -> None:
z1 = [x, y]
z2 = [y, x]
reveal_type(z1) # N: Revealed type is "builtins.list[builtins.object]"
reveal_type(z2) # N: Revealed type is "builtins.list[builtins.object]"
z1[1].append("asdf") # E: "object" has no attribute "append"
[builtins fixtures/dict.pyi]