Skip to content

Add Sentinel support (PEP-661)#3575

Open
hrolfurgylfa wants to merge 11 commits into
facebook:mainfrom
hrolfurgylfa:PEP-661-Sentinel
Open

Add Sentinel support (PEP-661)#3575
hrolfurgylfa wants to merge 11 commits into
facebook:mainfrom
hrolfurgylfa:PEP-661-Sentinel

Conversation

@hrolfurgylfa

Copy link
Copy Markdown

Summary

This PR adds support for PEP 661 Sentinel types.

Fixes #3207

Test Plan

I've run this as an LSP for a bit on a bigger private repo and it seems to work, but the main testing was the tests added to test/narrow.rs and test/sentinel.rs.

Some assumptions/things I didn't fully understand and might deserve more attention:

  • I'm using NestingContext::toplevel() when creating a Sentinel struct, this is similar to TypeVar/TypeAlias/a few others, I left the same TODO in place as they had. I looked through some of the other types and couldn't find any other way it was being done, so I left it like this for now, but if anyone has any pointers to how it should be done better, please let me know and I can fix it.

  • For the cinderx support, I added the sentinel to it in the TODO section of variables that get set to Any. I haven't really diven into what cinderx is and how the support for it is implemented, but if it is important to get it working I can go ahead and look into that.

  • I'm using bind_legacy_type_var_or_typing_alias to bind the Sentinel type, I think this should be ok as the PEP is pretty strict in how it can be assigned. I've done some testing and only found one edge case so far that fails because of this, but I don't think Final's should be mixed with Sentinel objects like this anyway, here is the test case that fails:

from typing import Any, Final
from typing_extensions import Sentinel

MISSING: Final[Any]
MISSING = Sentinel("MISSING")

It fails because the Final doesn't notice the assignment, it still thinks the variable was never assigned to.

Status

This PR is mostly ready for review, but I'm keeping it in Draft form until #3562 is addressed, since until then I can't finalize Python 3.15 support.

@meta-cla

meta-cla Bot commented May 25, 2026

Copy link
Copy Markdown

Hi @hrolfurgylfa!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented May 25, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@yangdanny97

Copy link
Copy Markdown
Contributor

Thanks for the PR, wow! that was fast, the PEP just got accepted recently and the conformance tests haven't even been added yet

I see you added lots of tests already, but maybe worth looking at the proposed conformance test to see if there's anything missing
https://github.com/python/typing/pull/2277/changes

I can give this a more detailed review once I've unblocked you on #3562

@yangdanny97 yangdanny97 self-assigned this May 29, 2026
@yangdanny97 yangdanny97 requested a review from rchen152 May 29, 2026 02:58
@hrolfurgylfa

Copy link
Copy Markdown
Author

Thanks, I didn't know those conformance tests existed, I'll review those once I get back home after the weekend, don't have my laptop with me.

And yeah, a proper review should probably be held until I can add the Python 3.15 support, since there is a bit of extra code to differentiate sentinel lowercase builtin and Sentinel uppercase from typing extensions, might be adding another 30-50 lines, don't remember the exact number though.

I'm hoping to have both of those ready on Tuesday/Wednesday evening (UTC+0).

@yangdanny97

Copy link
Copy Markdown
Contributor

No rush at all. The typeshed update just got merged, so once you rebase it should be there

This seems to be what is being decided in the python typing spec, see
the PR description: python/typing#2277

Although some typecheckers, like pyright, still have the old behaviour
recommended in the PEP of giving an error when a different name is
assigned.
@hrolfurgylfa

Copy link
Copy Markdown
Author

This PR should now be ready for review.

It now passes the conformance tests, the only change was just no longer emit an error when a sentinel is assigned to a variable with a different name like MISSING = sentinel("<MISSING>"). This is something the PEP prohibited but in the open conformance PR to python/typing that prohibition was lifted, so type checkers are no longer supposed to emit an error there.

I'm still not perfectly sure on the query interface and tsp implementation for sentinel, I haven't really figured out how to test the tsp stuff properly and while I have looked at the output of the shape query with sentinels I'm not perfectly sure how it should look, as such I haven't added any automated tests for it.

@hrolfurgylfa hrolfurgylfa marked this pull request as ready for review June 4, 2026 20:24
This is necessary as some properties, like __name__, are only available
on the builtin sentinel, not the typing_extensions Sentienl type.

This can be confirmed by running the following:

```
uv run -p 3.15 --with 'typing_extensions' python -c 'from typing_extensions import Sentinel; MISSING = Sentinel("MISSING"); print(MISSING.__name__)'
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
>     from typing_extensions import Sentinel; MISSING = Sentinel("MISSING"); MISSING.__name__
>                                                                            ^^^^^^^^^^^^^^^^
> AttributeError: 'Sentinel' object has no attribute '__name__'. Did you mean '.__ne__' instead of '.__name__'?

uv run -p 3.15 --with 'typing_extensions' python -c 'MISSING = sentinel("MISSING"); print(MISSING.__name__)'
> MISSING
```

@rchen152 rchen152 left a comment

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.

Thank you for the PR! This is great. I left a few comments, mostly minor. The one larger question I have is whether we can drop the distinction between builtins.sentinel and typing_extensions.Sentinel, which seems like it would simplify the code a fair bit if we can get away with it.

Comment thread crates/pyrefly_types/src/stdlib.rs Outdated
type_var_tuple: lookup_concrete(standardised(3, 11), "TypeVarTuple"),
type_alias_type: lookup_concrete(standardised(3, 12), "TypeAliasType"),
// sentinel: lookup_concrete(typing_extensions, "Sentinel"),
sentinel_builtin: lookup_concrete(builtins, "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.

How different are builtins.sentinel and typing_extensions.Sentinel? If they're pretty similar, I'd suggest combining them into just one sentinel, which is looked up from builtins in 3.15+ and from typing_extensions otherwise, and modifying the rest of the PR to likewise stop tracking the distinction between the two.

In general, we treat the typing and typing_extensions versions of things as the same without worrying about the minor differences between their runtime definitions. And distinguishing between builtins.sentinel and typing_extensions.Sentinel seems to be adding a fair bit of complexity to this PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The main reason I differentiated between the two was the __name__ attribute that is only available on the builtins sentinel, not the typing extensions Sentinel. If we merge them it would mean that in Python 3.15+ pyrefly would say that accessing __name__ on typing_extensions Sentinel is allowed and returns a string, while raising a runtime error if run. If that is fine I can go ahead and remove this logic.

Here is how I confirmed this difference if you want to test it with the python 3.15 beta:

uv run -p 3.15 --with 'typing_extensions' python -c 'from typing_extensions import Sentinel; MISSING = Sentinel("MISSING"); print(MISSING.__name__)'
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
>     from typing_extensions import Sentinel; MISSING = Sentinel("MISSING"); MISSING.__name__
>                                                                            ^^^^^^^^^^^^^^^^
> AttributeError: 'Sentinel' object has no attribute '__name__'. Did you mean '.__ne__' instead of '.__name__'?

uv run -p 3.15 --with 'typing_extensions' python -c 'MISSING = sentinel("MISSING"); print(MISSING.__name__)'
> MISSING

Also, if this ends up being kept, I should probably add a comment explaining the reasoning better, and remove that commented out line, not sure how that one got past me.

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 for the explanation! If the presence/absence of the __name__ attribute is the main difference, then I'm in favor of merging them.

Comment thread crates/pyrefly_types/src/sentinel.rs Outdated
pub fn new(name: Identifier, module: Module, kind: SentinelKind) -> Self {
Self(ArcId::new(SentinelInner {
kind,
// TODO: properly take parent from caller of new()

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.

You can grab the nesting context from the Scopes stack when constructing Binding::Sentinel - see comment in stmt.rs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, fixed

Comment thread pyrefly/lib/binding/stmt.rs
Comment thread website/docs/error-kinds.mdx Outdated
```python
from typing_extensions import Sentinel

# Sentinels must be assigned to a matching variable.

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.

This doc example looks incorrect; the implementation (correctly) allows the name to not match the variable name.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, forgot to change this when I removed the error on different variable name, fixed

@github-actions github-actions Bot added size/xl and removed size/xl labels Jun 6, 2026
@github-actions github-actions Bot added size/xl and removed size/xl labels Jun 6, 2026
@github-actions github-actions Bot added size/xl and removed size/xl labels Jun 7, 2026
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@meta-codesync

meta-codesync Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

@rchen152 has imported this pull request. If you are a Meta employee, you can view this in D107930625.

@rchen152 rchen152 left a comment

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.

Looks good, thank you! I'll get a second review and get this merged.

@hrolfurgylfa

Copy link
Copy Markdown
Author

Thanks 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Sentinel types in pydantic models

3 participants