示例#1
0
def test_restify_type_hints_alias():
    MyStr = str
    MyTuple = Tuple[str, str]
    assert restify(MyStr) == ":py:class:`str`"
    assert restify(
        MyTuple
    ) == ":py:class:`~typing.Tuple`\\ [:py:class:`str`, :py:class:`str`]"
示例#2
0
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`]")
示例#3
0
def test_restify_type_hints_containers():
    assert restify(List) == ":class:`~typing.List`"
    assert restify(Dict) == ":class:`~typing.Dict`"
    assert restify(List[int]) == ":class:`~typing.List`\\ [:class:`int`]"
    assert restify(List[str]) == ":class:`~typing.List`\\ [:class:`str`]"
    assert restify(Dict[str, float]) == (":class:`~typing.Dict`\\ "
                                         "[:class:`str`, :class:`float`]")
    assert restify(
        Tuple[str, str, str]) == (":class:`~typing.Tuple`\\ "
                                  "[:class:`str`, :class:`str`, :class:`str`]")
    assert restify(
        Tuple[str, ...]) == ":class:`~typing.Tuple`\\ [:class:`str`, ...]"
    assert restify(
        List[Dict[str, Tuple]]) == (":class:`~typing.List`\\ "
                                    "[:class:`~typing.Dict`\\ "
                                    "[:class:`str`, :class:`~typing.Tuple`]]")
    assert restify(
        MyList[Tuple[int,
                     int]]) == (":class:`tests.test_util_typing.MyList`\\ "
                                "[:class:`~typing.Tuple`\\ "
                                "[:class:`int`, :class:`int`]]")
    assert restify(
        Generator[None, None,
                  None]) == (":class:`~typing.Generator`\\ "
                             "[:obj:`None`, :obj:`None`, :obj:`None`]")
示例#4
0
def test_restify_type_hints_alias():
    MyStr = str
    MyTuple = Tuple[str, str]
    assert restify(MyStr) == ":class:`str`"
    assert restify(
        MyTuple
    ) == ":class:`Tuple`\\ [:class:`str`, :class:`str`]"  # type: ignore
示例#5
0
def test_restify_pep_585():
    assert restify(list[str]) == ":py:class:`list`\\ [:py:class:`str`]"  # type: ignore
    assert restify(dict[str, str]) == (":py:class:`dict`\\ "  # type: ignore
                                       "[:py:class:`str`, :py:class:`str`]")
    assert restify(dict[str, tuple[int, ...]]) == (":py:class:`dict`\\ "  # type: ignore
                                                   "[:py:class:`str`, :py:class:`tuple`\\ "
                                                   "[:py:class:`int`, ...]]")
示例#6
0
def test_restify_type_union_operator():
    assert restify(
        int | None) == ":py:class:`int` | :py:obj:`None`"  # type: ignore
    assert restify(
        int | str) == ":py:class:`int` | :py:class:`str`"  # type: ignore
    assert restify(int | str | None) == (
        ":py:class:`int` | :py:class:`str` | "  # type: ignore
        ":py:obj:`None`")
示例#7
0
def test_restify_type_hints_custom_class():
    assert restify(MyClass1) == ":py:class:`tests.test_util_typing.MyClass1`"
    assert restify(MyClass1,
                   "smart") == ":py:class:`~tests.test_util_typing.MyClass1`"

    assert restify(MyClass2) == ":py:class:`tests.test_util_typing.<MyClass2>`"
    assert restify(MyClass2,
                   "smart") == ":py:class:`~tests.test_util_typing.<MyClass2>`"
示例#8
0
def test_restify_mock():
    with mock(['unknown']):
        import unknown
        assert restify(unknown) == ':py:class:`unknown`'
        assert restify(
            unknown.secret.Class) == ':py:class:`unknown.secret.Class`'
        assert restify(unknown.secret.Class,
                       "smart") == ':py:class:`~unknown.secret.Class`'
示例#9
0
def test_restify_type_hints_typevars():
    T = TypeVar('T')
    T_co = TypeVar('T_co', covariant=True)
    T_contra = TypeVar('T_contra', contravariant=True)

    assert restify(T) == ":obj:`test_util_typing.T`"
    assert restify(T_co) == ":obj:`test_util_typing.T_co`"
    assert restify(T_contra) == ":obj:`test_util_typing.T_contra`"
    assert restify(List[T]) == ":class:`List`\\ [:obj:`test_util_typing.T`]"
示例#10
0
def test_restify_type_hints_Callable():
    assert restify(Callable) == ":class:`Callable`"

    if sys.version_info >= (3, 7):
        assert restify(Callable[[str], int]) == ":class:`Callable`\\ [[:class:`str`], :class:`int`]"
        assert restify(Callable[..., int]) == ":class:`Callable`\\ [[...], :class:`int`]"
    else:
        assert restify(Callable[[str], int]) == ":class:`Callable`\\ [:class:`str`, :class:`int`]"
        assert restify(Callable[..., int]) == ":class:`Callable`\\ [..., :class:`int`]"
示例#11
0
def test_restify_type_hints_Callable():
    assert restify(Callable) == ":py:class:`~typing.Callable`"

    if sys.version_info >= (3, 7):
        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`]")
    else:
        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`]")
示例#12
0
def add_return_type(app: Sphinx, what: str, fullname: str, object: Any,
                    options: Any, docs: list[str]) -> None:
    if what not in {"function", "method"}:
        return

    # this part of the numpy doc spec doesn't make much sense for pep 484 and I don't want to maintain two signatures.
    doc = "\n".join(docs)
    return_type = object.__annotations__.get(
        "return")  # full annotations should have been added by annotations.py
    if return_type in (
            None,
            "None") or "Yields" in doc or RETURNS_WITH_TYPE_RE.search(doc):
        return
    if re.search(RETURN_HEADER,
                 doc) is not None:  # clear any previous return info
        for i in reversed(range(docs.index("Returns"), len(docs))):
            del docs[i]

    docs += (
        "Returns",
        "-------",
        restify(return_type),
    )
    if match := RETURNS_WITH_INFO_RE.search(doc):
        # append the info to the return type
        info = match["info"].replace("\n", " ").strip()
        docs.append(f"     {info}")
示例#13
0
def test_stringify():
    assert stringify(int) == "int"
    assert stringify(str) == "str"
    assert stringify(None) == "None"
    assert stringify(Integral) == "numbers.Integral"
    assert restify(Struct) == ":class:`struct.Struct`"
    assert stringify(Any) == "Any"
示例#14
0
def test_restify():
    assert restify(int) == ":py:class:`int`"
    assert restify(str) == ":py:class:`str`"
    assert restify(None) == ":py:obj:`None`"
    assert restify(Integral) == ":py:class:`numbers.Integral`"
    assert restify(Struct) == ":py:class:`struct.Struct`"
    assert restify(TracebackType) == ":py:class:`types.TracebackType`"
    assert restify(Any) == ":py:obj:`~typing.Any`"
    assert restify('str') == "str"
示例#15
0
def test_restify_type_hints_Union():
    assert restify(
        Optional[int]) == ":py:obj:`~typing.Optional`\\ [:py:class:`int`]"
    assert restify(
        Union[str, None]) == ":py:obj:`~typing.Optional`\\ [:py:class:`str`]"
    assert restify(Union[int, str]) == (":py:obj:`~typing.Union`\\ "
                                        "[:py:class:`int`, :py:class:`str`]")

    assert restify(
        Union[int,
              Integral]) == (":py:obj:`~typing.Union`\\ "
                             "[:py:class:`int`, :py:class:`numbers.Integral`]")
    assert restify(Union[int, Integral],
                   "smart") == (":py:obj:`~typing.Union`\\ "
                                "[:py:class:`int`,"
                                " :py:class:`~numbers.Integral`]")

    assert (restify(
        Union[MyClass1,
              MyClass2]) == (":py:obj:`~typing.Union`\\ "
                             "[:py:class:`tests.test_util_typing.MyClass1`, "
                             ":py:class:`tests.test_util_typing.<MyClass2>`]"))
    assert (restify(
        Union[MyClass1, MyClass2],
        "smart") == (":py:obj:`~typing.Union`\\ "
                     "[:py:class:`~tests.test_util_typing.MyClass1`,"
                     " :py:class:`~tests.test_util_typing.<MyClass2>`]"))
示例#16
0
def test_restify():
    assert restify(int) == ":class:`int`"
    assert restify(str) == ":class:`str`"
    assert restify(None) == ":obj:`None`"
    assert restify(Integral) == ":class:`numbers.Integral`"
    assert restify(Struct) == ":class:`struct.Struct`"
    assert restify(Any) == ":obj:`Any`"
示例#17
0
def test_restify_type_hints_Union():
    assert restify(Optional[int]) == ":obj:`Optional`\\ [:class:`int`]"
    assert restify(Union[str, None]) == ":obj:`Optional`\\ [:class:`str`]"
    assert restify(Union[int, str]) == ":obj:`Union`\\ [:class:`int`, :class:`str`]"

    if sys.version_info >= (3, 7):
        assert restify(Union[int, Integral]) == ":obj:`Union`\\ [:class:`int`, :class:`numbers.Integral`]"
        assert (restify(Union[MyClass1, MyClass2]) ==
                ":obj:`Union`\\ [:class:`tests.test_util_typing.MyClass1`, :class:`tests.test_util_typing.<MyClass2>`]")
    else:
        assert restify(Union[int, Integral]) == ":class:`numbers.Integral`"
        assert restify(Union[MyClass1, MyClass2]) == ":class:`tests.test_util_typing.MyClass1`"
示例#18
0
def test_restify_type_hints_typevars():
    T = TypeVar('T')
    T_co = TypeVar('T_co', covariant=True)
    T_contra = TypeVar('T_contra', contravariant=True)

    assert restify(T) == ":py:obj:`tests.test_util_typing.T`"
    assert restify(T_co) == ":py:obj:`tests.test_util_typing.T_co`"
    assert restify(T_contra) == ":py:obj:`tests.test_util_typing.T_contra`"
    assert restify(List[T]) == ":py:class:`~typing.List`\\ [:py:obj:`tests.test_util_typing.T`]"

    if sys.version_info >= (3, 10):
        assert restify(MyInt) == ":py:class:`tests.test_util_typing.MyInt`"
    else:
        assert restify(MyInt) == ":py:class:`MyInt`"
示例#19
0
 def update_content(self, more_content):
     if istypealias(self.object):
         more_content.append(
             _('alias of %s') % restify(self.object), '')
         more_content.append('', '')
     super().update_content(more_content)
示例#20
0
def test_restify_broken_type_hints():
    assert restify(BrokenType) == ':class:`tests.test_util_typing.BrokenType`'
示例#21
0
def test_restify_type_union_operator():
    assert restify(int | None) == "Optional[:class:`int`]"  # type: ignore
    assert restify(int | str) == ":class:`int` | :class:`str`"  # type: ignore
    assert restify(int | str | None
                   ) == "Optional[:class:`int` | :class:`str`]"  # type: ignore
示例#22
0
def test_restify_type_ForwardRef():
    from typing import ForwardRef  # type: ignore
    assert restify(ForwardRef("myint")) == ":class:`myint`"
示例#23
0
def test_restify_type_Literal():
    from typing import Literal  # type: ignore
    assert restify(Literal[1, "2",
                           "\r"]) == ":obj:`~typing.Literal`\\ [1, '2', '\\r']"
示例#24
0
def test_restify_type_hints_custom_class():
    assert restify(MyClass1) == ":class:`test_util_typing.MyClass1`"
    assert restify(MyClass2) == ":class:`test_util_typing.<MyClass2>`"
示例#25
0
def test_restify_broken_type_hints():
    assert restify(
        BrokenType) == ':py:class:`tests.test_util_typing.BrokenType`'
    assert restify(BrokenType,
                   "smart") == ':py:class:`~tests.test_util_typing.BrokenType`'