Skip to content

Commit

Permalink
Properly format collections.abc.Callable
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Apr 23, 2024
1 parent 1ff9adf commit d8227d4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
3 changes: 2 additions & 1 deletion sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ def restify(cls: Any, mode: _RestifyMode = 'fully-qualified-except-typing') -> s
return text

# Callable has special formatting
if cls_module_is_typing and _get_typing_internal_name(cls) == 'Callable':
if (cls.__module__ in {'collections.abc', 'typing'}
and _get_typing_internal_name(cls) == 'Callable'):
args = ', '.join(restify(a, mode) for a in __args__[:-1])
returns = restify(__args__[-1], mode)
return fr'{text}\ [[{args}], {returns}]'
Expand Down
86 changes: 57 additions & 29 deletions tests/test_util/test_util_typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests util.typing functions."""

import sys
import typing as t
from collections import abc
from contextvars import Context, ContextVar, Token
from enum import Enum
from numbers import Integral
Expand Down Expand Up @@ -29,10 +31,7 @@
)
from typing import (
Any,
Callable,
Dict,
Generator,
Iterator,
List,
NewType,
Optional,
Expand Down Expand Up @@ -173,20 +172,29 @@ def test_restify_type_hints_containers():
assert restify(MyList[Tuple[int, int]]) == (":py:class:`tests.test_util.test_util_typing.MyList`\\ "
"[:py:class:`~typing.Tuple`\\ "
"[:py:class:`int`, :py:class:`int`]]")
assert restify(Generator[None, None, None]) == (":py:class:`~typing.Generator`\\ "
"[:py:obj:`None`, :py:obj:`None`, "
":py:obj:`None`]")
assert restify(Iterator[None]) == (":py:class:`~typing.Iterator`\\ "
"[:py:obj:`None`]")
assert restify(t.Generator[None, None, None]) == (":py:class:`~typing.Generator`\\ "
"[:py:obj:`None`, :py:obj:`None`, "
":py:obj:`None`]")
assert restify(abc.Generator[None, None, None]) == (":py:class:`collections.abc.Generator`\\ "
"[:py:obj:`None`, :py:obj:`None`, "
":py:obj:`None`]")
assert restify(t.Iterator[None]) == (":py:class:`~typing.Iterator`\\ "
"[:py:obj:`None`]")
assert restify(abc.Iterator[None]) == (":py:class:`collections.abc.Iterator`\\ "
"[:py:obj:`None`]")


def test_restify_type_hints_Callable():
assert restify(Callable) == ":py:class:`~typing.Callable`"

assert restify(Callable[[str], int]) == (":py:class:`~typing.Callable`\\ "
"[[:py:class:`str`], :py:class:`int`]")
assert restify(Callable[..., int]) == (":py:class:`~typing.Callable`\\ "
"[[...], :py:class:`int`]")
assert restify(t.Callable) == ":py:class:`~typing.Callable`"
assert restify(t.Callable[[str], int]) == (":py:class:`~typing.Callable`\\ "
"[[:py:class:`str`], :py:class:`int`]")
assert restify(t.Callable[..., int]) == (":py:class:`~typing.Callable`\\ "
"[[...], :py:class:`int`]")
assert restify(abc.Callable) == ":py:class:`collections.abc.Callable`"
assert restify(abc.Callable[[str], int]) == (":py:class:`collections.abc.Callable`\\ "
"[[:py:class:`str`], :py:class:`int`]")
assert restify(abc.Callable[..., int]) == (":py:class:`collections.abc.Callable`\\ "
"[[...], :py:class:`int`]")


def test_restify_type_hints_Union():
Expand Down Expand Up @@ -409,13 +417,21 @@ def test_stringify_type_hints_containers():
assert stringify_annotation(MyList[Tuple[int, int]], "fully-qualified") == "tests.test_util.test_util_typing.MyList[typing.Tuple[int, int]]"
assert stringify_annotation(MyList[Tuple[int, int]], "smart") == "~tests.test_util.test_util_typing.MyList[~typing.Tuple[int, int]]"

assert stringify_annotation(Generator[None, None, None], 'fully-qualified-except-typing') == "Generator[None, None, None]"
assert stringify_annotation(Generator[None, None, None], "fully-qualified") == "typing.Generator[None, None, None]"
assert stringify_annotation(Generator[None, None, None], "smart") == "~typing.Generator[None, None, None]"
assert stringify_annotation(t.Generator[None, None, None], 'fully-qualified-except-typing') == "Generator[None, None, None]"
assert stringify_annotation(t.Generator[None, None, None], "fully-qualified") == "typing.Generator[None, None, None]"
assert stringify_annotation(t.Generator[None, None, None], "smart") == "~typing.Generator[None, None, None]"

assert stringify_annotation(abc.Generator[None, None, None], 'fully-qualified-except-typing') == "collections.abc.Generator[None, None, None]"
assert stringify_annotation(abc.Generator[None, None, None], "fully-qualified") == "collections.abc.Generator[None, None, None]"
assert stringify_annotation(abc.Generator[None, None, None], "smart") == "~collections.abc.Generator[None, None, None]"

assert stringify_annotation(t.Iterator[None], 'fully-qualified-except-typing') == "Iterator[None]"
assert stringify_annotation(t.Iterator[None], "fully-qualified") == "typing.Iterator[None]"
assert stringify_annotation(t.Iterator[None], "smart") == "~typing.Iterator[None]"

assert stringify_annotation(Iterator[None], 'fully-qualified-except-typing') == "Iterator[None]"
assert stringify_annotation(Iterator[None], "fully-qualified") == "typing.Iterator[None]"
assert stringify_annotation(Iterator[None], "smart") == "~typing.Iterator[None]"
assert stringify_annotation(abc.Iterator[None], 'fully-qualified-except-typing') == "collections.abc.Iterator[None]"
assert stringify_annotation(abc.Iterator[None], "fully-qualified") == "collections.abc.Iterator[None]"
assert stringify_annotation(abc.Iterator[None], "smart") == "~collections.abc.Iterator[None]"


def test_stringify_type_hints_pep_585():
Expand Down Expand Up @@ -489,17 +505,29 @@ def test_stringify_type_hints_string():


def test_stringify_type_hints_Callable():
assert stringify_annotation(Callable, 'fully-qualified-except-typing') == "Callable"
assert stringify_annotation(Callable, "fully-qualified") == "typing.Callable"
assert stringify_annotation(Callable, "smart") == "~typing.Callable"
assert stringify_annotation(t.Callable, 'fully-qualified-except-typing') == "Callable"
assert stringify_annotation(t.Callable, "fully-qualified") == "typing.Callable"
assert stringify_annotation(t.Callable, "smart") == "~typing.Callable"

assert stringify_annotation(t.Callable[[str], int], 'fully-qualified-except-typing') == "Callable[[str], int]"
assert stringify_annotation(t.Callable[[str], int], "fully-qualified") == "typing.Callable[[str], int]"
assert stringify_annotation(t.Callable[[str], int], "smart") == "~typing.Callable[[str], int]"

assert stringify_annotation(t.Callable[..., int], 'fully-qualified-except-typing') == "Callable[[...], int]"
assert stringify_annotation(t.Callable[..., int], "fully-qualified") == "typing.Callable[[...], int]"
assert stringify_annotation(t.Callable[..., int], "smart") == "~typing.Callable[[...], int]"

assert stringify_annotation(abc.Callable, 'fully-qualified-except-typing') == "collections.abc.Callable"
assert stringify_annotation(abc.Callable, "fully-qualified") == "collections.abc.Callable"
assert stringify_annotation(abc.Callable, "smart") == "~collections.abc.Callable"

assert stringify_annotation(Callable[[str], int], 'fully-qualified-except-typing') == "Callable[[str], int]"
assert stringify_annotation(Callable[[str], int], "fully-qualified") == "typing.Callable[[str], int]"
assert stringify_annotation(Callable[[str], int], "smart") == "~typing.Callable[[str], int]"
assert stringify_annotation(abc.Callable[[str], int], 'fully-qualified-except-typing') == "collections.abc.Callable[[str], int]"
assert stringify_annotation(abc.Callable[[str], int], "fully-qualified") == "collections.abc.Callable[[str], int]"
assert stringify_annotation(abc.Callable[[str], int], "smart") == "~collections.abc.Callable[[str], int]"

assert stringify_annotation(Callable[..., int], 'fully-qualified-except-typing') == "Callable[[...], int]"
assert stringify_annotation(Callable[..., int], "fully-qualified") == "typing.Callable[[...], int]"
assert stringify_annotation(Callable[..., int], "smart") == "~typing.Callable[[...], int]"
assert stringify_annotation(abc.Callable[..., int], 'fully-qualified-except-typing') == "collections.abc.Callable[[...], int]"
assert stringify_annotation(abc.Callable[..., int], "fully-qualified") == "collections.abc.Callable[[...], int]"
assert stringify_annotation(abc.Callable[..., int], "smart") == "~collections.abc.Callable[[...], int]"


def test_stringify_type_hints_Union():
Expand Down

0 comments on commit d8227d4

Please sign in to comment.