chiark / gitweb /
documentation/python: prefix pybind's annotations with the typing module.
authorVladimír Vondruš <mosra@centrum.cz>
Fri, 23 Aug 2019 13:58:23 +0000 (15:58 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Sun, 25 Aug 2019 10:47:45 +0000 (12:47 +0200)
So it's consistent with pure Python annotations.

documentation/python.py
documentation/test_python/pybind_signatures/pybind_signatures.MyClass.html
documentation/test_python/pybind_signatures/pybind_signatures.html
documentation/test_python/pybind_type_links/pybind_type_links.html
documentation/test_python/test_pybind.py
documentation/test_python/test_search.py

index 03e0ca1e884b19a77b592b89ee5d126828930f4a..c57d0eacaf7b8331706af0c0b7ecad2997e83b08 100755 (executable)
@@ -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 = ''
index 258e917afb5773ed75d4b606cfd2b3e9d8224cfe..7d1628c640808029f78555be49492510b80ace76 100644 (file)
             <dt id="instance_function-a8577">
               <span class="m-doc-wrap-bumper">def <a href="#instance_function-a8577" class="m-doc-self">instance_function</a>(</span><span class="m-doc-wrap">self,
               arg0: int,
-              arg1: str<span class="m-text m-dim">, /</span>) -&gt; Tuple[float, int]</span>
+              arg1: str<span class="m-text m-dim">, /</span>) -&gt; typing.Tuple[float, int]</span>
             </dt>
             <dd>Instance method with positional-only args</dd>
             <dt id="instance_function_kwargs-a8577">
               <span class="m-doc-wrap-bumper">def <a href="#instance_function_kwargs-a8577" class="m-doc-self">instance_function_kwargs</a>(</span><span class="m-doc-wrap">self,
               hey: int,
-              what: str = &#x27;&lt;eh?&gt;&#x27;) -&gt; Tuple[float, int]</span>
+              what: str = &#x27;&lt;eh?&gt;&#x27;) -&gt; typing.Tuple[float, int]</span>
             </dt>
             <dd>Instance method with position or keyword args</dd>
           </dl>
index d7136b39c322afb45538035826689cdf4de02477..43fd799a45d0882b79316756c45d26d3bbf6a2a4 100644 (file)
               argument: float) -&gt; int</span>
             </dt>
             <dd>Scale an integer, kwargs</dd>
-            <dt id="takes_a_function-b0069">
-              <span class="m-doc-wrap-bumper">def <a href="#takes_a_function-b0069" class="m-doc-self">takes_a_function</a>(</span><span class="m-doc-wrap">arg0: Callable[[float, List[float]], int]<span class="m-text m-dim">, /</span>) -&gt; None</span>
+            <dt id="takes_a_function-515df">
+              <span class="m-doc-wrap-bumper">def <a href="#takes_a_function-515df" class="m-doc-self">takes_a_function</a>(</span><span class="m-doc-wrap">arg0: typing.Callable[[float, typing.List[float]], int]<span class="m-text m-dim">, /</span>) -&gt; None</span>
             </dt>
             <dd>A function taking a Callable</dd>
-            <dt id="takes_a_function_returning_none-08451">
-              <span class="m-doc-wrap-bumper">def <a href="#takes_a_function_returning_none-08451" class="m-doc-self">takes_a_function_returning_none</a>(</span><span class="m-doc-wrap">arg0: Callable[[], None]<span class="m-text m-dim">, /</span>) -&gt; None</span>
+            <dt id="takes_a_function_returning_none-0a9eb">
+              <span class="m-doc-wrap-bumper">def <a href="#takes_a_function_returning_none-0a9eb" class="m-doc-self">takes_a_function_returning_none</a>(</span><span class="m-doc-wrap">arg0: typing.Callable[[], None]<span class="m-text m-dim">, /</span>) -&gt; None</span>
             </dt>
             <dd>A function taking a Callable that returns None</dd>
-            <dt id="taking_a_list_returning_a_tuple-54d79">
-              <span class="m-doc-wrap-bumper">def <a href="#taking_a_list_returning_a_tuple-54d79" class="m-doc-self">taking_a_list_returning_a_tuple</a>(</span><span class="m-doc-wrap">arg0: List[float]<span class="m-text m-dim">, /</span>) -&gt; Tuple[int, int, int]</span>
+            <dt id="taking_a_list_returning_a_tuple-11ba6">
+              <span class="m-doc-wrap-bumper">def <a href="#taking_a_list_returning_a_tuple-11ba6" class="m-doc-self">taking_a_list_returning_a_tuple</a>(</span><span class="m-doc-wrap">arg0: typing.List[float]<span class="m-text m-dim">, /</span>) -&gt; typing.Tuple[int, int, int]</span>
             </dt>
             <dd>Takes a list, returns a tuple</dd>
             <dt id="tenOverloads-fe11a">
index 49291baaf1af177131a81cde9a452a1b42ae65f7..03428c305a73105954a6f5dbb4d66387b1f0bc1c 100644 (file)
@@ -61,8 +61,8 @@
               <span class="m-doc-wrap-bumper">def <a href="#type_enum-3b87d" class="m-doc-self">type_enum</a>(</span><span class="m-doc-wrap">value: <a href="pybind_type_links.html#Enum" class="m-doc">Enum</a> = <a href="pybind_type_links.html#Enum-SECOND" class="m-doc">Enum.SECOND</a>) -&gt; None</span>
             </dt>
             <dd>A function taking an enum</dd>
-            <dt id="type_nested-9cd35">
-              <span class="m-doc-wrap-bumper">def <a href="#type_nested-9cd35" class="m-doc-self">type_nested</a>(</span><span class="m-doc-wrap">arg0: Tuple[<a href="pybind_type_links.Foo.html" class="m-doc">Foo</a>, List[<a href="pybind_type_links.html#Enum" class="m-doc">Enum</a>]]<span class="m-text m-dim">, /</span>) -&gt; None</span>
+            <dt id="type_nested-0e174">
+              <span class="m-doc-wrap-bumper">def <a href="#type_nested-0e174" class="m-doc-self">type_nested</a>(</span><span class="m-doc-wrap">arg0: typing.Tuple[<a href="pybind_type_links.Foo.html" class="m-doc">Foo</a>, typing.List[<a href="pybind_type_links.html#Enum" class="m-doc">Enum</a>]]<span class="m-text m-dim">, /</span>) -&gt; None</span>
             </dt>
             <dd>A function with nested type annotation</dd>
             <dt id="type_return-da39a">
index 451df5755e936be0c377c327df9bf200afa72b09..0d40947f2ebb5c176521df16007295f4fe166da5 100644 (file)
@@ -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):
index 16424d73f080cdd52389ad6deebe85c80079ed52..287100bd0dfedc71c8fda277a6a5d8e412780bc8 100644 (file)
@@ -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'),