From: Vladimír Vondruš Date: Wed, 4 Sep 2019 18:33:36 +0000 (+0200) Subject: documentation/python: handle ... in type annotations. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=0925097894b3479e8cbbbfc1dcdd32c963666b0a;p=blog.git documentation/python: handle ... in type annotations. Until now I didn't know code like `if annotation is ...` is possible. What will come next, using // as an operator?! --- diff --git a/documentation/python.py b/documentation/python.py index f0fc1bc5..25ae8388 100755 --- a/documentation/python.py +++ b/documentation/python.py @@ -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. diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.html b/documentation/test_python/inspect_annotations/inspect_annotations.html index 95aff5b1..393138c1 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.html +++ b/documentation/test_python/inspect_annotations/inspect_annotations.html @@ -72,6 +72,11 @@ def annotation_callable_no_args(a: typing.Callable[[], typing.Dict[int, float]])
Annotation with the Callable type w/o arguments
+
+ def annotation_ellipsis(a: typing.Callable[[...], int], + b: typing.Tuple[str, ...]) +
+
Annotation with ellipsis
def annotation_func_instead_of_type(a)
diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.py b/documentation/test_python/inspect_annotations/inspect_annotations.py index 027aa9b7..bc38f81b 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.py +++ b/documentation/test_python/inspect_annotations/inspect_annotations.py @@ -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"""