chiark / gitweb /
documentation/python: handle ... in type annotations.
authorVladimír Vondruš <mosra@centrum.cz>
Wed, 4 Sep 2019 18:33:36 +0000 (20:33 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Thu, 5 Sep 2019 16:25:42 +0000 (18:25 +0200)
Until now I didn't know code like `if annotation is ...` is possible.
What will come next, using // as an operator?!

documentation/python.py
documentation/test_python/inspect_annotations/inspect_annotations.html
documentation/test_python/inspect_annotations/inspect_annotations.py

index f0fc1bc57a6b1a2e52ec494fcb6eb4c41bdf9f2d..25ae8388729d1fc31dffa33207420d1821621d99 100755 (executable)
@@ -1010,6 +1010,11 @@ def extract_annotation(state: State, referrer_path: List[str], annotation) -> Tu
     elif isinstance(annotation, typing.TypeVar):
         return annotation.__name__, annotation.__name__
 
+    # Ellipsis -- print a literal `...`
+    # TODO: any chance to link this to python official docs?
+    elif annotation is ...:
+        return '...', '...'
+
     # If the annotation is from the typing module, it ... gets complicated. It
     # could be a "bracketed" type, in which case we want to recurse to its
     # types as well.
index 95aff5b1c1b54d56e24ad4533358fdb5de59795f..393138c199408be3a3ac9aaa55b84b15160a41cc 100644 (file)
               <span class="m-doc-wrap-bumper">def <a href="#annotation_callable_no_args" class="m-doc-self">annotation_callable_no_args</a>(</span><span class="m-doc-wrap">a: typing.Callable[[], typing.Dict[int, float]])</span>
             </dt>
             <dd>Annotation with the Callable type w/o arguments</dd>
+            <dt id="annotation_ellipsis">
+              <span class="m-doc-wrap-bumper">def <a href="#annotation_ellipsis" class="m-doc-self">annotation_ellipsis</a>(</span><span class="m-doc-wrap">a: typing.Callable[[...], int],
+              b: typing.Tuple[str, ...])</span>
+            </dt>
+            <dd>Annotation with ellipsis</dd>
             <dt id="annotation_func_instead_of_type">
               <span class="m-doc-wrap-bumper">def <a href="#annotation_func_instead_of_type" class="m-doc-self">annotation_func_instead_of_type</a>(</span><span class="m-doc-wrap">a)</span>
             </dt>
index 027aa9b7b6831bae4da4e4f3142ec68aa8b9b2d2..bc38f81bb08096a52333a85d831f0199f2043ee3 100644 (file)
@@ -69,6 +69,9 @@ def annotation_callable(a: Callable[[float, int], str]):
 def annotation_callable_no_args(a: Callable[[], Dict[int, float]]):
     """Annotation with the Callable type w/o arguments"""
 
+def annotation_ellipsis(a: Callable[..., int], b: Tuple[str, ...]):
+    """Annotation with ellipsis"""
+
 # Only possible with native code now, https://www.python.org/dev/peps/pep-0570/
 #def positionals_only(positional_only, /, positional_kw):
     #"""Function with explicitly delimited positional args"""