feat(python): Make UNSET a PEP 661 sentinel - #1086
Conversation
`UNSET` currently fails on most tests against [PEP 661](https://peps.python.org/pep-0661/) behavior
Will use for creating `UNSET` - https://docs.python.org/3.15/library/functions.html#sentinel - https://peps.python.org/pep-0661/
- Good news: all the tests I wrote are fixed
- Bad news: `ty` is now reporting "Found 3460 diagnostics"
- Need to tweak the codegen, which relied on `Any`
Fixes all 3460 `ty` diagnostics
Makes sense in `typing_extensions` as a helper, but here it just adds complexity
|
I've had this sitting ready since shortly after opening #1075 But didn't want to create too much noise - so here's a present now if anyone wants it 😄 |
Seems that identity isn't true for `3.14` (previosly tested on `3.12`) But the upstream test wasn't checking that in the first place https://github.com/python/typing_extensions/blob/83400e979b8e3b0b647f9a6a57f0275230e5f19f/src/test_typing_extensions.py#L9700
| # Was renamed in https://github.com/python/typing_extensions/releases/tag/4.16.0 | ||
| from typing_extensions import Sentinel as sentinel # noqa: N813 | ||
| else: # noqa: PLR5501 | ||
| if sys.version_info >= (3, 15): |
There was a problem hiding this comment.
Can we remove this in the future? Would be good to add a note saying when we can remove this?
So this part would be detected by Ruff via outdated-version-block (UP036)
Anything that uses sys.version_info works the same and will be flagged in-step with
But on this part:
when we can remove this?
Without dependencies?
October 2031
With "typing_extensions>=4.16"?
Any time 😄
There was a problem hiding this comment.
I went with a module doc instead, since I wrote one in (https://github.com/narwhals-dev/narwhals/blob/deffd1d1e11dd593675119c1445dc7d5291426c3/src/narwhals/_typing_compat.py)
There was a problem hiding this comment.
I'm generally a fan of updating version requirements when it helps us clean up stuff. We don't need to support the oldest Python versions so feel free to update version requirements.
There was a problem hiding this comment.
If we were to update requirements, adding this would be my preference as it is a single file dependency and very common:
dependencies = [
"typing-extensions>=4.16 ; python_full_version < '3.15'",
]Depending on the latest python version can simplify maintainence, but it will hurt adoption of Mosaic.
It would mean that no downstream packages can offer backwards compatibility, if they have vgplot as a required dependency

Description
As defined on
main,UNSEThas a few issues - most of which are not that visible.This PR introduces a conditional-backport for
sentinel(python3.15).Mosaic gets both the runtime and typing benefits - while requiring no new dependencies .
When available 1,
typing_extensions.sentinelis prefered - before defining a minimal 2 vendored version 3The new tests are a combination of:
typing_extensionstest suite (1, 2)sentinel(python3.15) docsWhat's fixed?
(4dcdb43)
UNSETcan be used directly in annotations and is understood by type checkers:UNSETmaintains identity when copied or pickled:UNSETnow has a visible docstring:Related
sentinel(python3.15)typing_extensions.sentinelFootnotes
typing_extensions>=4.16.0is installed ↩I've documented the original source and the differences are the exclusion of deprecation paths,
which serve no benefit to mosaic. ↩
We've used this technique a few times in
narwhals, but not (yet) forsentinel↩