8000 Backport `types.CapsuleType` (#390) · python/typing_extensions@12a0f28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12a0f28

Browse files
authored
Backport types.CapsuleType (#390)
1 parent 08c066e commit 12a0f28

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
signifies the return type of the `__(a)exit__` method, matching
3333
`typing.ContextManager` and `typing.AsyncContextManager` on Python
3434
3.13+.
35+
- Backport `types.CapsuleType` from Python 3.13.
3536

3637
# Release 4.11.0 (April 5, 2024)
3738

doc/index.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,20 @@ Annotation metadata
882882
The documentation string passed to :class:`Doc`.
883883

884884

885+
Capsule objects
886+
~~~~~~~~~~~~~~~
887+
888+
.. class:: CapsuleType
889+
890+
The type of :py:ref:`capsule objects <capsules>`.
891+
See :py:class:`types.CapsuleType`, where it has existed since Python 3.13.
892+
893+
Note that this may not exist on all implementations of Python; it is only
894+
guaranteed to exist on CPython.
895+
896+
.. versionadded:: 4.12.0
897+
898+
885899
Pure aliases
886900
~~~~~~~~~~~~
887901

src/test_typing_extensions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6765,5 +6765,15 @@ def test_pickle(self):
67656765
self.assertEqual(doc_info, pickle.loads(pickled))
67666766

67676767

6768+
@skipUnless(
6769+
hasattr(typing_extensions, "CapsuleType"),
6770+
"CapsuleType is not available on all Python implementations"
6771+
)
6772+
class CapsuleTypeTests(BaseTestCase):
6773+
def test_capsule_type(self):
6774+
import _datetime
6775+
self.assertIsInstance(_datetime.datetime_CAPI, typing_extensions.CapsuleType)
6776+
6777+
67686778
if __name__ == '__main__':
67696779
main()

src/typing_extensions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3411,6 +3411,23 @@ def __eq__(self, other: object) -> bool:
34113411
return self.documentation == other.documentation
34123412

34133413

3414+
_CapsuleType = getattr(_types, "CapsuleType", None)
3415+
3416+
if _CapsuleType is None:
3417+
try:
3418+
import _socket
3419+
except ImportError:
3420+
pass
3421+
else:
3422+
_CAPI = getattr(_socket, "CAPI", None)
3423+
if _CAPI is not None:
3424+
_CapsuleType = type(_CAPI)
3425+
3426+
if _CapsuleType is not None:
3427+
CapsuleType = _CapsuleType
3428+
__all__.append("CapsuleType")
3429+
3430+
34143431
# Aliases for items that have always been in typing.
34153432
# Explicitly assign these (rather than using `from typing import *` at the top),
34163433
# so that we get a CI error if one of these is deleted from typing.py

0 commit comments

Comments
 (0)
0