Skip to content
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
2 changes: 1 addition & 1 deletion mypy/applytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_target_type(
skip_unsatisfied: bool,
) -> Type | None:
p_type = get_proper_type(type)
if isinstance(p_type, UninhabitedType) and tvar.has_default():
if isinstance(p_type, UninhabitedType) and p_type.ambiguous and tvar.has_default():
return tvar.default
if isinstance(tvar, ParamSpecType):
return type
Expand Down
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4552,7 +4552,7 @@ def check_lvalue(
self.check_lvalue(sub_expr)[0] or
# This type will be used as a context for further inference of rvalue,
# we put Uninhabited if there is no information available from lvalue.
UninhabitedType()
UninhabitedType(ambiguous=True)
for sub_expr in lvalue.items
]
lvalue_type = TupleType(types, self.named_type("builtins.tuple"))
Expand Down
6 changes: 3 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ def infer_function_type_arguments_using_context(
# Only substitute non-Uninhabited and non-erased types.
new_args: list[Type | None] = []
for arg in args:
if has_uninhabited_component(arg) or has_erased_component(arg):
if has_ambiguous_uninhabited_component(arg) or has_erased_component(arg):
new_args.append(None)
else:
new_args.append(arg)
Expand Down Expand Up @@ -6719,8 +6719,8 @@ def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
return True


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


class HasAmbiguousUninhabitedComponentsQuery(types.BoolTypeQuery):
Expand Down
4 changes: 2 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,9 +1389,9 @@ class UninhabitedType(ProperType):

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

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

def can_be_true_default(self) -> bool:
return False
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-typevar-defaults.test
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ def func_error_alias2(
reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
[builtins fixtures/dict.pyi]

[case testTypeVarDefaultsExplicitNever]
from typing import Generic, Never, TypeVar

D = TypeVar("D", default=int)
class WithDefault(Generic[D]): pass

never_with_default: WithDefault[Never] = WithDefault()
explicit_with_default: WithDefault[Never] = WithDefault[Never]()

[case testTypeVarDefaultsFunctions]
from typing import TypeVar, ParamSpec, List, Union, Callable, Tuple
from typing_extensions import TypeVarTuple, Unpack
Expand Down
Loading