Skip to content

Remove PEP-604 methods from Sentinel on Python <3.10 #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2025

Conversation

AlexWaygood
Copy link
Member

We don't generally try to "backport PEP 604" on Python <3.10; this is more consistent with our features

We don't generally try to "backport PEP 604" on Python <3.10; this is more consistent with our features
@JelleZijlstra
Copy link
Member

Could you add a note to the changelog?

@AlexWaygood AlexWaygood requested a review from JelleZijlstra May 25, 2025 15:34
@JelleZijlstra
Copy link
Member

Note that typing_extensions._SpecialForm also unconditionally has __or__.

$ uv run python3.9
Python 3.9.0rc1 (v3.9.0rc1:439c93d51f, Aug 11 2020, 16:45:30) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing_extensions import Self, LiteralString
>>> Self | LiteralString
typing.Union[typing_extensions.Self, typing_extensions.LiteralString]
>>> 

Probably not worth fixing at this point though.

@JelleZijlstra
Copy link
Member

It's suspicious that no tests failed, would you mind adding a simple 3.10+ test that __or__ works?

@AlexWaygood
Copy link
Member Author

Note that typing_extensions._SpecialForm also unconditionally has __or__.

Yes, the word "generally" in the changelog entry is doing some work here 😄 as you say, though, _SpecialForm.__(r)or__ is probably not doing much harm, and it might cause backwards-compat issues to remove it now 🤷

@AlexWaygood
Copy link
Member Author

It's suspicious that no tests failed, would you mind adding a simple 3.10+ test that __or__ works?

it actually is already tested on Python 3.10+, but the test is already skipped on Python <=3.9!

@skipIf(sys.version_info < (3, 10), reason='New unions not available in 3.9')
def test_sentinel_type_expression_union(self):
sentinel = Sentinel('sentinel')
def func1(a: int | sentinel = sentinel): pass
def func2(a: sentinel | int = sentinel): pass
self.assertEqual(func1.__annotations__['a'], Union[int, sentinel])
self.assertEqual(func2.__annotations__['a'], Union[sentinel, int])

@JelleZijlstra JelleZijlstra merged commit fadc1ed into main May 25, 2025
21 checks passed
@JelleZijlstra JelleZijlstra deleted the AlexWaygood-patch-1 branch May 25, 2025 15:42
@Viicos
Copy link
Contributor

Viicos commented May 25, 2025

Thanks @AlexWaygood, now that this is version guarded we could in fact return operator.or_(self, other), so that in typeshed we expose -> UnionType in >=3.10 instead of >=3.14.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented May 25, 2025

@Viicos I don't think that works, does it? operator.or_ just calls __or__, so we can't call operator.or_ from __or__ without infinite recursion:

~/dev/typing_extensions/src (main)⚡ % git diff          
diff --git a/src/typing_extensions.py b/src/typing_extensions.py
index 292641a..73caf8d 100644
--- a/src/typing_extensions.py
+++ b/src/typing_extensions.py
@@ -4246,10 +4246,10 @@ class Sentinel:
 
     if sys.version_info >= (3, 10):
         def __or__(self, other):
-            return typing.Union[self, other]
+            return operator.or_(self, other)
 
         def __ror__(self, other):
-            return typing.Union[other, self]
+            return operator.or_(self, other)
 
     def __getstate__(self):
         raise TypeError(f"Cannot pickle {type(self).__name__!r} object")
~/dev/typing_extensions/src (main)⚡ % python                   
Python 3.13.1 (main, Jan  3 2025, 12:04:03) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing_extensions import TypeVar, Sentinel
>>> Sentinel("T") | Sentinel("T")
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    Sentinel("T") | Sentinel("T")
    ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
  File "/Users/alexw/dev/typing_extensions/src/typing_extensions.py", line 4249, in __or__
    return operator.or_(self, other)
           ~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/alexw/dev/typing_extensions/src/typing_extensions.py", line 4249, in __or__
    return operator.or_(self, other)
           ~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/alexw/dev/typing_extensions/src/typing_extensions.py", line 4249, in __or__
    return operator.or_(self, other)
           ~~~~~~~~~~~~^^^^^^^^^^^^^
  [Previous line repeated 988 more times]
RecursionError: maximum recursion depth exceeded
>>> 

UnionType also doesn't support wrapping objects that aren't instances of type on Python <3.14

@Viicos
Copy link
Contributor

Viicos commented May 25, 2025

Good catch, I always fall into this recursion trap!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants