chiark / gitweb /
documentation: fix a crashy corner case with the typing module.
authorVladimír Vondruš <mosra@centrum.cz>
Fri, 27 Sep 2019 18:19:09 +0000 (20:19 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Fri, 27 Sep 2019 18:19:09 +0000 (20:19 +0200)
WHY IS IT SUCH A DUMPSTER FIRE

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

index ece774ee24cc8d2b88c50e7c6f2f5bc564bbc3e9..21bb2e9ac59c063ec5ccd352ecaf2a221b499561 100755 (executable)
@@ -1094,8 +1094,11 @@ def extract_annotation(state: State, referrer_path: List[str], annotation) -> Tu
     elif (hasattr(annotation, '__module__') and annotation.__module__ == 'typing'):
         # Optional or Union, handle those first
         if hasattr(annotation, '__origin__') and annotation.__origin__ is typing.Union:
-            # FOR SOME REASON `annotation.__args__[1] is None` is always False
-            if len(annotation.__args__) == 2 and isinstance(None, annotation.__args__[1]):
+            # FOR SOME REASON `annotation.__args__[1] is None` is always False,
+            # so we have to use isinstance(). HOWEVER, we *can't* use
+            # isinstance if it's a "bracketed" type -- it'll die. So check that
+            # first.
+            if len(annotation.__args__) == 2 and not hasattr(annotation.__args__[1], '__args__') and isinstance(None, annotation.__args__[1]):
                 name = 'typing.Optional'
                 args = annotation.__args__[:1]
             else:
index 393138c199408be3a3ac9aaa55b84b15160a41cc..70c0960a3e8c4c9bb9b0390fac30f92c81cdfdf7 100644 (file)
               <span class="m-doc-wrap-bumper">def <a href="#annotation_union" class="m-doc-self">annotation_union</a>(</span><span class="m-doc-wrap">a: typing.Union[float, int])</span>
             </dt>
             <dd>Annotation with the Union type</dd>
+            <dt id="annotation_union_second_bracketed">
+              <span class="m-doc-wrap-bumper">def <a href="#annotation_union_second_bracketed" class="m-doc-self">annotation_union_second_bracketed</a>(</span><span class="m-doc-wrap">a: typing.Union[float, typing.List[int]])</span>
+            </dt>
+            <dd>Annotation with the Union type and second type bracketed, where we can&#x27;t use isinstance</dd>
             <dt id="args_kwargs">
               <span class="m-doc-wrap-bumper">def <a href="#args_kwargs" class="m-doc-self">args_kwargs</a>(</span><span class="m-doc-wrap">a, b, *args, **kwargs)</span>
             </dt>
index bc38f81bb08096a52333a85d831f0199f2043ee3..e4b6597324eabf100070feba6d53ec60c8c5e09f 100644 (file)
@@ -63,6 +63,9 @@ def annotation_generic(a: List[_T]) -> _T:
 def annotation_optional(a: Optional[float]):
     """Annotation with the Optional type"""
 
+def annotation_union_second_bracketed(a: Union[float, List[int]]):
+    """Annotation with the Union type and second type bracketed, where we can't use isinstance"""
+
 def annotation_callable(a: Callable[[float, int], str]):
     """Annotation with the Callable type"""