chiark / gitweb /
documentation/python: gracefully handle nested annotation parsing failures.
authorVladimír Vondruš <mosra@centrum.cz>
Wed, 4 Sep 2019 18:28:58 +0000 (20:28 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Thu, 5 Sep 2019 16:25:42 +0000 (18:25 +0200)
documentation/python.py
documentation/test_python/inspect_annotations/inspect_annotations.html
documentation/test_python/inspect_annotations/inspect_annotations.py

index b98b739587562aeb8fd0a0a8382f4b2e72e582a0..f0fc1bc57a6b1a2e52ec494fcb6eb4c41bdf9f2d 100755 (executable)
@@ -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)),
index 87d15ffa6727dfb6b1ab671514680ba37b1b3122..95aff5b1c1b54d56e24ad4533358fdb5de59795f 100644 (file)
               <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>
             <dd>Annotation with a function instead of a type, ignored</dd>
+            <dt id="annotation_func_instead_of_type_nested">
+              <span class="m-doc-wrap-bumper">def <a href="#annotation_func_instead_of_type_nested" class="m-doc-self">annotation_func_instead_of_type_nested</a>(</span><span class="m-doc-wrap">a, b, c)</span>
+            </dt>
+            <dd>Annotations with nested problems, ignoring the whole thing</dd>
             <dt id="annotation_generic">
               <span class="m-doc-wrap-bumper">def <a href="#annotation_generic" class="m-doc-self">annotation_generic</a>(</span><span class="m-doc-wrap">a: typing.List[Tp]) -&gt; Tp</span>
             </dt>
index 349d982472a7dcbecff579db4ce7c694255d552d..027aa9b7b6831bae4da4e4f3142ec68aa8b9b2d2 100644 (file)
@@ -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"""