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
6 changes: 6 additions & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ def _infer_constraints(
# T :> U2", but they are not equivalent to the constraint solver,
# which never introduces new Union types (it uses join() instead).
if isinstance(template, TypeVarType):
if (
isinstance(actual, Instance)
and actual.last_known_value is not None
and actual.last_known_value.is_sentinel_literal()
):
actual = actual.last_known_value
return [Constraint(template, direction, actual)]

if (
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-sentinels.test
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ def func(x: int | MISSING = MISSING) -> None:
func(MISSING)
func(ALIAS) # E: Argument 1 to "func" has incompatible type "Sentinel"; expected "int | MISSING"
[builtins fixtures/tuple.pyi]

[case testDictGetPEP661SentinelDefault]
from typing_extensions import assert_type, sentinel

Unknown = sentinel("Unknown")

def func(d: dict[str, str]) -> None:
var = d.get("key", Unknown)
assert_type(var, str | Unknown)
[builtins fixtures/dict.pyi]
Loading