From: Vladimír Vondruš Date: Wed, 4 Sep 2019 18:28:58 +0000 (+0200) Subject: documentation/python: gracefully handle nested annotation parsing failures. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=e0f38c84160c1333e5d170100e00706c27c43e88;p=blog.git documentation/python: gracefully handle nested annotation parsing failures. --- diff --git a/documentation/python.py b/documentation/python.py index b98b7395..f0fc1bc5 100755 --- a/documentation/python.py +++ b/documentation/python.py @@ -1059,6 +1059,11 @@ def extract_annotation(state: State, referrer_path: List[str], annotation) -> Tu nested_type_links += [nested_type_link] nested_return_type, nested_return_type_link = extract_annotation(state, referrer_path, args[-1]) + # If nested parsing failed (the invalid annotation below), + # fail the whole thing + if None in nested_types or nested_return_type is None: + return None, None + return ( '{}[[{}], {}]'.format(name, ', '.join(nested_types), nested_return_type), '{}[[{}], {}]'.format(name_link, ', '.join(nested_type_links), nested_return_type_link) @@ -1072,6 +1077,10 @@ def extract_annotation(state: State, referrer_path: List[str], annotation) -> Tu nested_types += [nested_type] nested_type_links += [nested_type_link] + # If nested parsing failed (the invalid annotation below), + # fail the whole thing + if None in nested_types: return None, None + return ( '{}[{}]'.format(name, ', '.join(nested_types)), '{}[{}]'.format(name_link, ', '.join(nested_type_links)), diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.html b/documentation/test_python/inspect_annotations/inspect_annotations.html index 87d15ffa..95aff5b1 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.html +++ b/documentation/test_python/inspect_annotations/inspect_annotations.html @@ -76,6 +76,10 @@ def annotation_func_instead_of_type(a)
Annotation with a function instead of a type, ignored
+
+ def annotation_func_instead_of_type_nested(a, b, c) +
+
Annotations with nested problems, ignoring the whole thing
def annotation_generic(a: typing.List[Tp]) -> Tp
diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.py b/documentation/test_python/inspect_annotations/inspect_annotations.py index 349d9824..027aa9b7 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.py +++ b/documentation/test_python/inspect_annotations/inspect_annotations.py @@ -41,6 +41,9 @@ def annotation_tuple_instead_of_tuple(a: (float, int)): def annotation_func_instead_of_type(a: open): """Annotation with a function instead of a type, ignored""" +def annotation_func_instead_of_type_nested(a: List[open], b: Callable[[open], str], c: Callable[[str], open]): + """Annotations with nested problems, ignoring the whole thing""" + def annotation_any(a: Any): """Annotation with the Any type"""