Skip to content

[ty] Add support for sentinels (PEP 661)#25082

Merged
sharkdp merged 7 commits into
astral-sh:mainfrom
JelleZijlstra:sentinels
May 13, 2026
Merged

[ty] Add support for sentinels (PEP 661)#25082
sharkdp merged 7 commits into
astral-sh:mainfrom
JelleZijlstra:sentinels

Conversation

@JelleZijlstra
Copy link
Copy Markdown
Collaborator

Summary

Add support for typing_extensions.Sentinel (PEP 661). Support for the other variants (builtins.sentinel, typing_extensions.sentinel) can be added later as stubs are updated.

See PEP 661 and python/typing#2277

Test Plan

New tests added.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label May 10, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 10, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 89.36%. The percentage of expected errors that received a diagnostic held steady at 85.49%. The number of fully passing files held steady at 88/134.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 10, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 10, 2026

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

Copy link
Copy Markdown
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you very much!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few other things that we could test (from reading your PEP, please feel free to leave some of these out if you think they don't make sense):

  • That we recognize sentinel objects as being truthy, i.e. that bool(MISSING) is of type Literal[True].
  • That accessing .__name__ and .__module__ on a sentinel does not lead to an error, and that the type of those attributes is reasonable / "not obviously wrong".
  • That sentinel can not be subclassed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, adding some tests.

That sentinel can not be subclassed

We currently have only typing_extensions.Sentinel, which we didn't make @final. builtins.sentinel will be, though testing that specifically in ty doesn't feel that useful, as it should just listen to the @final in the stub.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and __name__ does error right now because the attribute isn't there in typeshed for typing_extensions.Sentinel. We'll add it soon. This also seems not critical to test since the behavior is specified by the stub.

Mapping,
// typing_extensions
ExtensionsTypeVar, // must be distinct from typing.TypeVar, backports new features
Sentinel,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like typing_extensions.Sentinel is an alias for builtins.sentinel? Should we add explicit tests to make sure that we also understand the latter, i.e. if someone uses

MISSING = sentinel("MISSING")

def next_value(default: int | MISSING = MISSING):
    ...

Copy link
Copy Markdown
Member

@AlexWaygood AlexWaygood May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we won't recognise the builtin symbol as existing until we upgrade our vendored version of typeshed in a couple of days

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'm merging this for now, but it would be great to add a few more tests once that lands.

Copy link
Copy Markdown
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the updates.

@sharkdp sharkdp merged commit 679d7b2 into astral-sh:main May 13, 2026
58 checks passed
@JelleZijlstra JelleZijlstra deleted the sentinels branch May 13, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants