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
15 changes: 15 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ def _verify_arg_default_value(
and type(runtime_arg.default) is not object
# And ellipsis
and runtime_arg.default is not ...
# And sentinels
and not is_sentinel(runtime_type)
and not is_subtype_helper(runtime_type, stub_type)
):
yield (
Expand Down Expand Up @@ -2090,6 +2092,19 @@ def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool:
return mypy.subtypes.is_subtype(left, right)


def is_sentinel(runtime: mypy.types.Type) -> bool:
"""Checks whether ``runtime`` is the type of a ``sentinel()`` marker object."""
runtime = mypy.types.get_proper_type(runtime)
return isinstance(runtime, mypy.types.Instance) and runtime.type.fullname in (
"builtins.sentinel",
"typing_extensions.sentinel",
# typing_extensions 4.14.0-4.15.x defined the class as `Sentinel`, with
# no lowercase alias; the rename (and `Sentinel = sentinel` alias)
# landed in 4.16.0.
"typing_extensions.Sentinel",
)


def get_mypy_node_for_name(module: str, type_name: str) -> mypy.nodes.SymbolNode | None:
stub = get_stub(module)
if stub is None:
Expand Down
57 changes: 57 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class frozenset(Generic[T]): ...

class function: pass

class sentinel:
__name__: str
__module__: str
def __new__(cls, name: str, /, *, repr: str | None = None) -> sentinel: ...

class int: ...
class float: ...
class bool(int): ...
Expand Down Expand Up @@ -3074,6 +3079,58 @@ def __init__(self, formatter_class=HelpFormatter): ...
error=None,
)

@collect_cases
def test_object_marker(self) -> Iterator[Case]:
yield Case(
stub="""
def f(x: int = ...) -> None: ...
""",
runtime="""
_MISSING = object()


def f(x=_MISSING):
pass
""",
error=None,
)

@collect_cases
def test_sentinel_object_like_marker(self) -> Iterator[Case]:
yield Case(
stub="""
def f(x: int = ...) -> None: ...
""",
runtime="""
from typing_extensions import sentinel

_MISSING = sentinel("_MISSING")


def f(x=_MISSING):
pass
""",
error=None,
)

@collect_cases
def test_legacy_sentinel_object_like_marker(self) -> Iterator[Case]:
yield Case(
stub="""
def f(x: int = ...) -> None: ...
""",
runtime="""
from typing_extensions import Sentinel

_MISSING = Sentinel("_MISSING")


def f(x=_MISSING):
pass
""",
error=None,
)


def remove_color_code(s: str) -> str:
return re.sub("\\x1b.*?m", "", s) # this works!
Expand Down
Loading