def _restify_py37(cls: Optional[Type]) -> str: """Convert python class to a reST reference.""" from sphinx.util import inspect # lazy loading if (inspect.isgenericalias(cls) and cls.__module__ == 'typing' and cls.__origin__ is Union): # Union if len(cls.__args__) > 1 and cls.__args__[-1] is NoneType: if len(cls.__args__) > 2: args = ', '.join(restify(a) for a in cls.__args__[:-1]) return ':py:obj:`~typing.Optional`\\ [:obj:`~typing.Union`\\ [%s]]' % args else: return ':py:obj:`~typing.Optional`\\ [%s]' % restify( cls.__args__[0]) else: args = ', '.join(restify(a) for a in cls.__args__) return ':py:obj:`~typing.Union`\\ [%s]' % args elif inspect.isgenericalias(cls): if isinstance(cls.__origin__, typing._SpecialForm): text = restify(cls.__origin__) # type: ignore elif getattr(cls, '_name', None): if cls.__module__ == 'typing': text = ':py:class:`~%s.%s`' % (cls.__module__, cls._name) else: text = ':py:class:`%s.%s`' % (cls.__module__, cls._name) else: text = restify(cls.__origin__) origin = getattr(cls, '__origin__', None) if not hasattr(cls, '__args__'): pass elif all(is_system_TypeVar(a) for a in cls.__args__): # Suppress arguments if all system defined TypeVars (ex. Dict[KT, VT]) pass elif cls.__module__ == 'typing' and cls._name == 'Callable': args = ', '.join(restify(a) for a in cls.__args__[:-1]) text += r"\ [[%s], %s]" % (args, restify(cls.__args__[-1])) elif cls.__module__ == 'typing' and getattr(origin, '_name', None) == 'Literal': text += r"\ [%s]" % ', '.join(repr(a) for a in cls.__args__) elif cls.__args__: text += r"\ [%s]" % ", ".join(restify(a) for a in cls.__args__) return text elif isinstance(cls, typing._SpecialForm): return ':py:obj:`~%s.%s`' % (cls.__module__, cls._name) elif hasattr(cls, '__qualname__'): if cls.__module__ == 'typing': return ':py:class:`~%s.%s`' % (cls.__module__, cls.__qualname__) else: return ':py:class:`%s.%s`' % (cls.__module__, cls.__qualname__) elif isinstance(cls, ForwardRef): return ':py:class:`%s`' % cls.__forward_arg__ else: # not a class (ex. TypeVar) if cls.__module__ == 'typing': return ':py:obj:`~%s.%s`' % (cls.__module__, cls.__name__) else: return ':py:obj:`%s.%s`' % (cls.__module__, cls.__name__)
def test_isgenericalias(app): from target.genericalias import C, T from target.methods import Base assert inspect.isgenericalias(C) is True assert inspect.isgenericalias(T) is True assert inspect.isgenericalias(object()) is False assert inspect.isgenericalias(Base) is False
def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any) -> bool: """ Called to see if a member can be documented by this documenter. """ return inspect.isgenericalias(member)