Skip to content

Commit

Permalink
Remove typing_extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Apr 23, 2024
1 parent 0266507 commit 6d91311
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
4 changes: 4 additions & 0 deletions sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ def _is_annotated_form(obj: Any) -> TypeGuard[Annotated[Any, ...]]:

def _is_unpack_form(obj: Any) -> bool:
"""Check if the object is :class:`typing.Unpack` or equivalent."""
if sys.version_info >= (3, 11):
from typing import Unpack

return typing.get_origin(obj) is Unpack
origin = typing.get_origin(obj)
origin_module = getattr(origin, '__module__', None)
origin_name = getattr(origin, '__name__', None)
Expand Down
40 changes: 11 additions & 29 deletions tests/test_util/test_util_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,25 +325,19 @@ def test_restify_pep_585():
":py:class:`int`]")


@pytest.mark.skipif(sys.version_info[:2] <= (3, 11), reason='python 3.11+ is required.')
def test_restify_Unpack():
import typing
from typing import Unpack

from typing_extensions import Unpack as UnpackCompat

class X(typing.TypedDict):
class X(t.TypedDict):
x: int
y: int
label: str

# Unpack is considered as typing special form so we always have '~'
expect = rf':py:obj:`~{UnpackCompat.__module__}.Unpack`\ [:py:class:`X`]'
assert restify(UnpackCompat['X'], 'fully-qualified-except-typing') == expect
assert restify(UnpackCompat['X'], 'smart') == expect

if NativeUnpack := getattr(typing, 'Unpack', None):
expect = r':py:obj:`~typing.Unpack`\ [:py:class:`X`]'
assert restify(NativeUnpack['X'], 'fully-qualified-except-typing') == expect
assert restify(NativeUnpack['X'], 'smart') == expect
expect = r':py:obj:`~typing.Unpack`\ [:py:class:`X`]'
assert restify(Unpack['X'], 'fully-qualified-except-typing') == expect
assert restify(Unpack['X'], 'smart') == expect


@pytest.mark.skipif(sys.version_info[:2] <= (3, 9), reason='python 3.10+ is required.')
Expand Down Expand Up @@ -495,29 +489,17 @@ def test_stringify_Annotated():
assert stringify_annotation(Annotated[str, "foo", "bar"], "smart") == "str"


@pytest.mark.skipif(sys.version_info[:2] <= (3, 11), reason='python 3.11+ is required.')
def test_stringify_Unpack():
import typing

from typing_extensions import Unpack as UnpackCompat
from typing import Unpack

class X(typing.TypedDict):
class X(t.TypedDict):
x: int
y: int
label: str

# typing.Unpack is introduced in 3.11 but typing_extensions.Unpack
# is only using typing.Unpack since 3.12, so those objects are not
# synchronized with each other.
if hasattr(typing, 'Unpack') and typing.Unpack is UnpackCompat:
assert stringify_annotation(UnpackCompat['X']) == 'Unpack[X]'
assert stringify_annotation(UnpackCompat['X'], 'smart') == '~typing.Unpack[X]'
else:
assert stringify_annotation(UnpackCompat['X']) == 'typing_extensions.Unpack[X]'
assert stringify_annotation(UnpackCompat['X'], 'smart') == '~typing_extensions.Unpack[X]'

if NativeUnpack := getattr(typing, 'Unpack', None):
assert stringify_annotation(NativeUnpack['X']) == 'Unpack[X]'
assert stringify_annotation(NativeUnpack['X'], 'smart') == '~typing.Unpack[X]'
assert stringify_annotation(Unpack['X']) == 'Unpack[X]'
assert stringify_annotation(Unpack['X'], 'smart') == '~typing.Unpack[X]'


def test_stringify_type_hints_string():
Expand Down

0 comments on commit 6d91311

Please sign in to comment.