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:
<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'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>
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"""