Skip to content

Include ambiguous into UninhabitedType identity #19648

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions mypy/test/testsolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def test_multiple_variables(self) -> None:
)

def test_no_constraints_for_var(self) -> None:
self.assert_solve([self.fx.t], [], [self.fx.uninhabited])
self.assert_solve([self.fx.t, self.fx.s], [], [self.fx.uninhabited, self.fx.uninhabited])
self.assert_solve([self.fx.t], [], [self.fx.a_uninhabited])
self.assert_solve(
[self.fx.t, self.fx.s], [], [self.fx.a_uninhabited, self.fx.a_uninhabited]
)
self.assert_solve(
[self.fx.t, self.fx.s],
[self.supc(self.fx.s, self.fx.a)],
[self.fx.uninhabited, self.fx.a],
[self.fx.a_uninhabited, self.fx.a],
)

def test_simple_constraints_with_dynamic_type(self) -> None:
Expand Down Expand Up @@ -116,7 +118,7 @@ def test_poly_no_constraints(self) -> None:
self.assert_solve(
[self.fx.t, self.fx.u],
[],
[self.fx.uninhabited, self.fx.uninhabited],
[self.fx.a_uninhabited, self.fx.a_uninhabited],
allow_polymorphic=True,
)

Expand Down Expand Up @@ -152,7 +154,7 @@ def test_poly_free_pair_with_bounds_uninhabited(self) -> None:
self.assert_solve(
[self.fx.ub, self.fx.uc],
[self.subc(self.fx.ub, self.fx.uc)],
[self.fx.uninhabited, self.fx.uninhabited],
[self.fx.a_uninhabited, self.fx.a_uninhabited],
[],
allow_polymorphic=True,
)
Expand Down
2 changes: 2 additions & 0 deletions mypy/test/typefixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def make_type_var(
self.anyt = AnyType(TypeOfAny.special_form)
self.nonet = NoneType()
self.uninhabited = UninhabitedType()
self.a_uninhabited = UninhabitedType()
self.a_uninhabited.ambiguous = True

# Abstract class TypeInfos

Expand Down
4 changes: 2 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,10 +1236,10 @@ def accept(self, visitor: TypeVisitor[T]) -> T:
return visitor.visit_uninhabited_type(self)

def __hash__(self) -> int:
return hash(UninhabitedType)
return hash((UninhabitedType, self.ambiguous))

def __eq__(self, other: object) -> bool:
return isinstance(other, UninhabitedType)
return isinstance(other, UninhabitedType) and other.ambiguous == self.ambiguous

def serialize(self) -> JsonDict:
return {".class": "UninhabitedType"}
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-generic-subtyping.test
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,20 @@ s, s = Nums() # E: Incompatible types in assignment (expression has type "int",
[builtins fixtures/for.pyi]
[out]

[case testUninhabitedCacheChecksAmbiguous]
# https://github.com/python/mypy/issues/19641
from typing import Mapping, Never, TypeVar

M = TypeVar("M", bound=Mapping[str,object])

def get(arg: M, /) -> M:
return arg

get({})

def upcast(d: dict[Never, Never]) -> Mapping[str, object]:
return d # E: Incompatible return value type (got "dict[Never, Never]", expected "Mapping[str, object]")
[builtins fixtures/dict.pyi]

-- Variance
-- --------
Expand Down
10 changes: 9 additions & 1 deletion test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3781,10 +3781,18 @@ from typing import Any, Dict, NoReturn, NoReturn, Union

def foo() -> Union[Dict[str, Any], Dict[int, Any]]:
return {}
[builtins fixtures/dict.pyi]

[case testExistingEmptyCollectionDoesNotUpcast]
from typing import Any, Dict, NoReturn, NoReturn, Union

empty: Dict[NoReturn, NoReturn]

def foo() -> Dict[str, Any]:
return empty # E: Incompatible return value type (got "dict[Never, Never]", expected "dict[str, Any]")

def bar() -> Union[Dict[str, Any], Dict[int, Any]]:
return empty
return empty # E: Incompatible return value type (got "dict[Never, Never]", expected "Union[dict[str, Any], dict[int, Any]]")
[builtins fixtures/dict.pyi]

[case testUpperBoundInferenceFallbackNotOverused]
Expand Down
Loading