def render_annotation(anno: Any) -> str: """Convert an annotation into its stub representation.""" if _is_optional(anno): elem_type = _get_optional_elem(anno) rendered = 'Optional[' + render_annotation(elem_type) + ']' elif hasattr(anno, '__supertype__'): rendered = anno.__name__ elif is_forward_ref(anno): rendered = repr(anno.__forward_arg__) elif getattr(anno, '__module__', None) == 'typing': rendered = repr(anno).replace('typing.', '') elif isinstance(anno, NoneType): rendered = 'None' elif isinstance(anno, type): if anno.__module__ in ('builtins',): rendered = anno.__qualname__ else: rendered = anno.__module__ + '.' + anno.__qualname__ elif isinstance(anno, str): rendered = anno else: rendered = repr(anno) # Temporary hacky workaround for #76 to fix remaining NoneType hints by search-replace return rendered.replace('NoneType', 'None')
def generic_rewrite(self, typ: Any) -> str: if hasattr(typ, '__supertype__'): rendered = typ.__name__ elif is_forward_ref(typ): rendered = repr(typ.__forward_arg__) elif isinstance(typ, NoneType) or typ is NoneType: rendered = 'None' elif is_generic(typ): rendered = repr(typ) elif isinstance(typ, type): if typ.__module__ in ('builtins', ): rendered = typ.__qualname__ else: rendered = typ.__module__ + '.' + typ.__qualname__ elif isinstance(typ, str): rendered = typ else: rendered = repr(typ) return rendered