From: Vladimír Vondruš Date: Fri, 23 Aug 2019 13:58:23 +0000 (+0200) Subject: documentation/python: prefix pybind's annotations with the typing module. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=b6f339368375fc6808ff0c057addf38205e947f0;p=blog.git documentation/python: prefix pybind's annotations with the typing module. So it's consistent with pure Python annotations. --- diff --git a/documentation/python.py b/documentation/python.py index 03e0ca1e..c57d0eac 100755 --- a/documentation/python.py +++ b/documentation/python.py @@ -614,8 +614,13 @@ def parse_pybind_type(state: State, referrer_path: List[str], signature: str) -> if match: input_type = match.group(0) signature = signature[len(input_type):] - type = map_name_prefix(state, input_type) - type_link = make_name_link(state, referrer_path, type) + # Prefix types with the typing module to be consistent with pure + # Python annotations + if input_type in ['Callable', 'Dict', 'List', 'Optional', 'Set', 'Tuple', 'Union']: + type = type_link = 'typing.' + input_type + else: + type = map_name_prefix(state, input_type) + type_link = make_name_link(state, referrer_path, type) else: assert signature[0] == '[' type = '' diff --git a/documentation/test_python/pybind_signatures/pybind_signatures.MyClass.html b/documentation/test_python/pybind_signatures/pybind_signatures.MyClass.html index 258e917a..7d1628c6 100644 --- a/documentation/test_python/pybind_signatures/pybind_signatures.MyClass.html +++ b/documentation/test_python/pybind_signatures/pybind_signatures.MyClass.html @@ -57,13 +57,13 @@
def instance_function(self, arg0: int, - arg1: str, /) -> Tuple[float, int] + arg1: str, /) -> typing.Tuple[float, int]
Instance method with positional-only args
def instance_function_kwargs(self, hey: int, - what: str = '<eh?>') -> Tuple[float, int] + what: str = '<eh?>') -> typing.Tuple[float, int]
Instance method with position or keyword args
diff --git a/documentation/test_python/pybind_signatures/pybind_signatures.html b/documentation/test_python/pybind_signatures/pybind_signatures.html index d7136b39..43fd799a 100644 --- a/documentation/test_python/pybind_signatures/pybind_signatures.html +++ b/documentation/test_python/pybind_signatures/pybind_signatures.html @@ -73,16 +73,16 @@ argument: float) -> int
Scale an integer, kwargs
-
- def takes_a_function(arg0: Callable[[float, List[float]], int], /) -> None +
+ def takes_a_function(arg0: typing.Callable[[float, typing.List[float]], int], /) -> None
A function taking a Callable
-
- def takes_a_function_returning_none(arg0: Callable[[], None], /) -> None +
+ def takes_a_function_returning_none(arg0: typing.Callable[[], None], /) -> None
A function taking a Callable that returns None
-
- def taking_a_list_returning_a_tuple(arg0: List[float], /) -> Tuple[int, int, int] +
+ def taking_a_list_returning_a_tuple(arg0: typing.List[float], /) -> typing.Tuple[int, int, int]
Takes a list, returns a tuple
diff --git a/documentation/test_python/pybind_type_links/pybind_type_links.html b/documentation/test_python/pybind_type_links/pybind_type_links.html index 49291baa..03428c30 100644 --- a/documentation/test_python/pybind_type_links/pybind_type_links.html +++ b/documentation/test_python/pybind_type_links/pybind_type_links.html @@ -61,8 +61,8 @@ def type_enum(value: Enum = Enum.SECOND) -> None
A function taking an enum
-
- def type_nested(arg0: Tuple[Foo, List[Enum]], /) -> None +
+ def type_nested(arg0: typing.Tuple[Foo, typing.List[Enum]], /) -> None
A function with nested type annotation
diff --git a/documentation/test_python/test_pybind.py b/documentation/test_python/test_pybind.py index 451df575..0d40947f 100644 --- a/documentation/test_python/test_pybind.py +++ b/documentation/test_python/test_pybind.py @@ -91,30 +91,30 @@ class Signature(unittest.TestCase): 'thingy(self, the_other_thing: Callable[[], None])'), ('thingy', '', [ ('self', None, None, None), - ('the_other_thing', 'Callable[[], None]', 'Callable[[], None]', None), + ('the_other_thing', 'typing.Callable[[], None]', 'typing.Callable[[], None]', None), ], None)) def test_square_brackets(self): self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: Tuple[int, str], no_really: str) -> List[str]'), ('foo', '', [ - ('a', 'Tuple[int, str]', 'Tuple[int, str]', None), + ('a', 'typing.Tuple[int, str]', 'typing.Tuple[int, str]', None), ('no_really', 'str', 'str', None), - ], 'List[str]')) + ], 'typing.List[str]')) def test_nested_square_brackets(self): self.assertEqual(parse_pybind_signature(self.state, [], - 'foo(a: Tuple[int, List[Tuple[int, int]]], another: float) -> Union[str, Any]'), + 'foo(a: Tuple[int, List[Tuple[int, int]]], another: float) -> Union[str, None]'), ('foo', '', [ - ('a', 'Tuple[int, List[Tuple[int, int]]]', 'Tuple[int, List[Tuple[int, int]]]', None), + ('a', 'typing.Tuple[int, typing.List[typing.Tuple[int, int]]]', 'typing.Tuple[int, typing.List[typing.Tuple[int, int]]]', None), ('another', 'float', 'float', None), - ], 'Union[str, Any]')) + ], 'typing.Union[str, None]')) def test_callable(self): self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: Callable[[int, Tuple[int, int]], float], another: float)'), ('foo', '', [ - ('a', 'Callable[[int, Tuple[int, int]], float]', 'Callable[[int, Tuple[int, int]], float]', None), + ('a', 'typing.Callable[[int, typing.Tuple[int, int]], float]', 'typing.Callable[[int, typing.Tuple[int, int]], float]', None), ('another', 'float', 'float', None), ], None)) @@ -184,9 +184,9 @@ class Signature(unittest.TestCase): state.module_mapping['module._module'] = 'module' self.assertEqual(parse_pybind_signature(state, [], - 'foo(a: module._module.Foo, b: Tuple[int, module._module.Bar]) -> module._module.Baz'), + 'foo(a: module._module.Foo, b: typing.Tuple[int, module._module.Bar]) -> module._module.Baz'), ('foo', '', [('a', 'module.Foo', 'module.Foo', None), - ('b', 'Tuple[int, module.Bar]', 'Tuple[int, module.Bar]', None)], 'module.Baz')) + ('b', 'typing.Tuple[int, module.Bar]', 'typing.Tuple[int, module.Bar]', None)], 'module.Baz')) class Signatures(BaseInspectTestCase): def test_positional_args(self): diff --git a/documentation/test_python/test_search.py b/documentation/test_python/test_search.py index 16424d73..287100bd 100644 --- a/documentation/test_python/test_search.py +++ b/documentation/test_python/test_search.py @@ -198,7 +198,7 @@ search_long_suffix_length [2] many_parameters [0] | ($ | ) [1] -0: .many_parameters(arg0: Tuple[int, float, str, List[Tuple[int, int…) [prefix=2[:30], suffix_length=53, type=FUNCTION] -> #many_parameters-5ce5b +0: .many_parameters(arg0: typing.Tuple[int, float, str, typing.List[…) [prefix=2[:30], suffix_length=53, type=FUNCTION] -> #many_parameters-31300 1: [prefix=0[:52], suffix_length=51, type=FUNCTION] -> 2: search_long_suffix_length [type=MODULE] -> search_long_suffix_length.html (EntryType.PAGE, CssClass.SUCCESS, 'page'),