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! |
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.