Skip to content

Commit 93eff83

Browse files
authored
Use explicit Never for type inference (#21497)
Fixes #20391 The problem became more prominent with type variables with defaults, so better be consistent.
1 parent d9f17fb commit 93eff83

5 files changed

Lines changed: 16 additions & 7 deletions

File tree

‎mypy/applytype.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_target_type(
3939
skip_unsatisfied: bool,
4040
) -> Type | None:
4141
p_type = get_proper_type(type)
42-
if isinstance(p_type, UninhabitedType) and tvar.has_default():
42+
if isinstance(p_type, UninhabitedType) and p_type.ambiguous and tvar.has_default():
4343
return tvar.default
4444
if isinstance(tvar, ParamSpecType):
4545
return type

‎mypy/checker.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4552,7 +4552,7 @@ def check_lvalue(
45524552
self.check_lvalue(sub_expr)[0] or
45534553
# This type will be used as a context for further inference of rvalue,
45544554
# we put Uninhabited if there is no information available from lvalue.
4555-
UninhabitedType()
4555+
UninhabitedType(ambiguous=True)
45564556
for sub_expr in lvalue.items
45574557
]
45584558
lvalue_type = TupleType(types, self.named_type("builtins.tuple"))

‎mypy/checkexpr.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,7 +2085,7 @@ def infer_function_type_arguments_using_context(
20852085
# Only substitute non-Uninhabited and non-erased types.
20862086
new_args: list[Type | None] = []
20872087
for arg in args:
2088-
if has_uninhabited_component(arg) or has_erased_component(arg):
2088+
if has_ambiguous_uninhabited_component(arg) or has_erased_component(arg):
20892089
new_args.append(None)
20902090
else:
20912091
new_args.append(arg)
@@ -6719,8 +6719,8 @@ def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
67196719
return True
67206720

67216721

6722-
def has_ambiguous_uninhabited_component(t: Type) -> bool:
6723-
return t.accept(HasAmbiguousUninhabitedComponentsQuery())
6722+
def has_ambiguous_uninhabited_component(t: Type | None) -> bool:
6723+
return t is not None and t.accept(HasAmbiguousUninhabitedComponentsQuery())
67246724

67256725

67266726
class HasAmbiguousUninhabitedComponentsQuery(types.BoolTypeQuery):

‎mypy/types.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,9 +1389,9 @@ class UninhabitedType(ProperType):
13891389

13901390
ambiguous: bool # Is this a result of inference for a variable without constraints?
13911391

1392-
def __init__(self, line: int = -1, column: int = -1) -> None:
1392+
def __init__(self, line: int = -1, column: int = -1, *, ambiguous: bool = False) -> None:
13931393
super().__init__(line, column)
1394-
self.ambiguous = False
1394+
self.ambiguous = ambiguous
13951395

13961396
def can_be_true_default(self) -> bool:
13971397
return False

‎test-data/unit/check-typevar-defaults.test‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ def func_error_alias2(
150150
reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
151151
[builtins fixtures/dict.pyi]
152152

153+
[case testTypeVarDefaultsExplicitNever]
154+
from typing import Generic, Never, TypeVar
155+
156+
D = TypeVar("D", default=int)
157+
class WithDefault(Generic[D]): pass
158+
159+
never_with_default: WithDefault[Never] = WithDefault()
160+
explicit_with_default: WithDefault[Never] = WithDefault[Never]()
161+
153162
[case testTypeVarDefaultsFunctions]
154163
from typing import TypeVar, ParamSpec, List, Union, Callable, Tuple
155164
from typing_extensions import TypeVarTuple, Unpack

0 commit comments

Comments
 (0)