Add Sentinel support (PEP-661)#3575
Conversation
|
Hi @hrolfurgylfa! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
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 I can give this a more detailed review once I've unblocked you on #3562 |
|
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). |
|
No rush at all. The typeshed update just got merged, so once you rebase it should be there |
4778b67 to
a492972
Compare
47853ea to
b1c8fbf
Compare
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.
b1c8fbf to
1d894a2
Compare
|
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 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. |
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
```
1d894a2 to
f766c28
Compare
rchen152
left a comment
There was a problem hiding this comment.
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.
| 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"), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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__)'
> MISSINGAlso, 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.
There was a problem hiding this comment.
Thanks for the explanation! If the presence/absence of the __name__ attribute is the main difference, then I'm in favor of merging them.
| pub fn new(name: Identifier, module: Module, kind: SentinelKind) -> Self { | ||
| Self(ArcId::new(SentinelInner { | ||
| kind, | ||
| // TODO: properly take parent from caller of new() |
There was a problem hiding this comment.
You can grab the nesting context from the Scopes stack when constructing Binding::Sentinel - see comment in stmt.rs.
| ```python | ||
| from typing_extensions import Sentinel | ||
|
|
||
| # Sentinels must be assigned to a matching variable. |
There was a problem hiding this comment.
This doc example looks incorrect; the implementation (correctly) allows the name to not match the variable name.
There was a problem hiding this comment.
Good catch, forgot to change this when I removed the error on different variable name, fixed
… sentinel" This reverts commit b66942c.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
@rchen152 has imported this pull request. If you are a Meta employee, you can view this in D107930625. |
rchen152
left a comment
There was a problem hiding this comment.
Looks good, thank you! I'll get a second review and get this merged.
|
Thanks 😄 |
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.rsandtest/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:
It fails because the
Finaldoesn'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.