Skip to content
Merged
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: 2 additions & 0 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from mypy.types import (
ELLIPSIS_TYPE_NAMES,
NOT_IMPLEMENTED_TYPE_NAMES,
SENTINEL_TYPE_NAMES,
AnyType,
CallableType,
ExtraAttrs,
Expand Down Expand Up @@ -1056,6 +1057,7 @@ def is_singleton_identity_type(typ: ProperType) -> bool:
(typ.type.is_enum and len(typ.type.enum_members) == 1)
or (typ.type.fullname in ELLIPSIS_TYPE_NAMES)
or (typ.type.fullname in NOT_IMPLEMENTED_TYPE_NAMES)
or (typ.type.fullname in SENTINEL_TYPE_NAMES)
)
if isinstance(typ, LiteralType):
return typ.is_enum_literal() or typ.is_sentinel_literal() or isinstance(typ.value, bool)
Expand Down
18 changes: 14 additions & 4 deletions test-data/unit/check-sentinels.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ MISSING = Sentinel("MISSING")
SPECIAL = Sentinel("SPECIAL")

def func(x: int | MISSING | SPECIAL) -> None:
# We could reasonably do narrowing here, but currently we're pretty conservative
# about narrowing on ==.
if x == MISSING:
assert_type(x, int | MISSING | SPECIAL)
assert_type(x, MISSING)
else:
assert_type(x, int | SPECIAL)
[builtins fixtures/ops.pyi]

[case testSentinelNarrowingInSequence]
from typing_extensions import assert_type, Sentinel

MISSING = Sentinel("MISSING")

def func(var: str | MISSING | None) -> None:
if var in (MISSING, None):
assert_type(var, MISSING | None)
else:
assert_type(x, int | MISSING | SPECIAL)
assert_type(var, str)
[builtins fixtures/tuple.pyi]

[case testSentinelSameReprDistinctTypes]
Expand Down
1 change: 0 additions & 1 deletion test-data/unit/lib-stub/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Never: _SpecialForm

class Sentinel:
def __init__(self, name: str, repr: str | None = None) -> None: ...
def __eq__(self, other: object) -> bool: ...
sentinel = Sentinel

TypeVarTuple: _SpecialForm
Expand Down
Loading