def restify(cls: Optional["Type"]) -> str: """Convert python class to a reST reference.""" from sphinx.util import inspect # lazy loading if cls is None or cls is NoneType: return ':obj:`None`' elif cls is Ellipsis: return '...' elif cls in INVALID_BUILTIN_CLASSES: return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls] elif inspect.isNewType(cls): return ':class:`%s`' % cls.__name__ elif types_Union and isinstance(cls, types_Union): if len(cls.__args__) > 1 and None in cls.__args__: args = ' | '.join(restify(a) for a in cls.__args__ if a) return 'Optional[%s]' % args else: return ' | '.join(restify(a) for a in cls.__args__) elif cls.__module__ in ('__builtin__', 'builtins'): return ':class:`%s`' % cls.__name__ else: if sys.version_info >= (3, 7): # py37+ return _restify_py37(cls) else: return _restify_py36(cls)
def stringify(annotation: Any) -> str: """Stringify type annotation object.""" from sphinx.util import inspect # lazy loading if isinstance(annotation, str): if annotation.startswith("'") and annotation.endswith("'"): # might be a double Forward-ref'ed type. Go unquoting. return annotation[1:-1] else: return annotation elif isinstance(annotation, TypeVar): if annotation.__module__ == 'typing': return annotation.__name__ else: return '.'.join([annotation.__module__, annotation.__name__]) elif inspect.isNewType(annotation): # Could not get the module where it defiend return annotation.__name__ elif not annotation: return repr(annotation) elif annotation is NoneType: return 'None' elif annotation in INVALID_BUILTIN_CLASSES: return INVALID_BUILTIN_CLASSES[annotation] elif (getattr(annotation, '__module__', None) == 'builtins' and hasattr(annotation, '__qualname__')): return annotation.__qualname__ elif annotation is Ellipsis: return '...' if sys.version_info >= (3, 7): # py37+ return _stringify_py37(annotation) else: return _stringify_py36(annotation)
def stringify(annotation: Any) -> str: """Stringify type annotation object.""" from sphinx.util import inspect # lazy loading if isinstance(annotation, str): if annotation.startswith("'") and annotation.endswith("'"): # might be a double Forward-ref'ed type. Go unquoting. return annotation[1:-1] else: return annotation elif isinstance(annotation, TypeVar): return annotation.__name__ elif inspect.isNewType(annotation): # Could not get the module where it defiend return annotation.__name__ elif not annotation: return repr(annotation) elif annotation is NoneType: return 'None' elif (getattr(annotation, '__module__', None) == 'builtins' and hasattr(annotation, '__qualname__')): return annotation.__qualname__ elif annotation is Ellipsis: return '...' elif annotation is Struct: # Before Python 3.9, struct.Struct class has incorrect __module__. return 'struct.Struct' if sys.version_info >= (3, 7): # py37+ return _stringify_py37(annotation) else: return _stringify_py36(annotation)
def restify(cls: Optional[Type], mode: str = 'fully-qualified-except-typing') -> str: """Convert python class to a reST reference. :param mode: Specify a method how annotations will be stringified. 'fully-qualified-except-typing' Show the module name and qualified name of the annotation except the "typing" module. 'smart' Show the name of the annotation. """ from sphinx.ext.autodoc.mock import ismock, ismockmodule # lazy loading from sphinx.util import inspect # lazy loading if mode == 'smart': modprefix = '~' else: modprefix = '' try: if cls is None or cls is NoneType: return ':py:obj:`None`' elif cls is Ellipsis: return '...' elif isinstance(cls, str): return cls elif ismockmodule(cls): return ':py:class:`%s%s`' % (modprefix, cls.__name__) elif ismock(cls): return ':py:class:`%s%s.%s`' % (modprefix, cls.__module__, cls.__name__) elif is_invalid_builtin_class(cls): return ':py:class:`%s%s`' % (modprefix, INVALID_BUILTIN_CLASSES[cls]) elif inspect.isNewType(cls): if sys.version_info > (3, 10): # newtypes have correct module info since Python 3.10+ return ':py:class:`%s%s.%s`' % (modprefix, cls.__module__, cls.__name__) else: return ':py:class:`%s`' % cls.__name__ elif UnionType and isinstance(cls, UnionType): if len(cls.__args__) > 1 and None in cls.__args__: args = ' | '.join(restify(a, mode) for a in cls.__args__ if a) return 'Optional[%s]' % args else: return ' | '.join(restify(a, mode) for a in cls.__args__) elif cls.__module__ in ('__builtin__', 'builtins'): if hasattr(cls, '__args__'): return ':py:class:`%s`\\ [%s]' % ( cls.__name__, ', '.join(restify(arg, mode) for arg in cls.__args__), ) else: return ':py:class:`%s`' % cls.__name__ else: return _restify_py37(cls, mode) except (AttributeError, TypeError): return inspect.object_description(cls)
def restify(cls: Optional[Type]) -> str: """Convert python class to a reST reference.""" from sphinx.util import inspect # lazy loading try: if cls is None or cls is NoneType: return ':obj:`None`' elif cls is Ellipsis: return '...' elif cls in INVALID_BUILTIN_CLASSES: return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls] elif inspect.isNewType(cls): return ':class:`%s`' % cls.__name__ elif UnionType and isinstance(cls, UnionType): if len(cls.__args__) > 1 and None in cls.__args__: args = ' | '.join(restify(a) for a in cls.__args__ if a) return 'Optional[%s]' % args else: return ' | '.join(restify(a) for a in cls.__args__) elif cls.__module__ in ('__builtin__', 'builtins'): if hasattr(cls, '__args__'): return ':class:`%s`\\ [%s]' % ( cls.__name__, ', '.join(restify(arg) for arg in cls.__args__), ) else: return ':class:`%s`' % cls.__name__ else: if sys.version_info >= (3, 7): # py37+ return _restify_py37(cls) else: return _restify_py36(cls) except (AttributeError, TypeError): return repr(cls)
def stringify(annotation: Any, smartref: bool = False) -> str: """Stringify type annotation object. :param smartref: If true, add "~" prefix to the result to remove the leading module and class names from the reference text """ from sphinx.util import inspect # lazy loading if smartref: prefix = '~' else: prefix = '' if isinstance(annotation, str): if annotation.startswith("'") and annotation.endswith("'"): # might be a double Forward-ref'ed type. Go unquoting. return annotation[1:-1] else: return annotation elif isinstance(annotation, TypeVar): if annotation.__module__ == 'typing': return annotation.__name__ else: return prefix + '.'.join( [annotation.__module__, annotation.__name__]) elif inspect.isNewType(annotation): if sys.version_info > (3, 10): # newtypes have correct module info since Python 3.10+ return prefix + '%s.%s' % (annotation.__module__, annotation.__name__) else: return annotation.__name__ elif not annotation: return repr(annotation) elif annotation is NoneType: return 'None' elif annotation in INVALID_BUILTIN_CLASSES: return prefix + INVALID_BUILTIN_CLASSES[annotation] elif str(annotation).startswith('typing.Annotated'): # for py310+ pass elif (getattr(annotation, '__module__', None) == 'builtins' and getattr(annotation, '__qualname__', None)): if hasattr(annotation, '__args__'): # PEP 585 generic return repr(annotation) else: return annotation.__qualname__ elif annotation is Ellipsis: return '...' if sys.version_info >= (3, 7): # py37+ return _stringify_py37(annotation, smartref) else: return _stringify_py36(annotation, smartref)
def restify(cls: Optional["Type"]) -> str: """Convert python class to a reST reference.""" from sphinx.util import inspect # lazy loading if cls is None or cls is NoneType: return ':obj:`None`' elif cls is Ellipsis: return '...' elif inspect.isNewType(cls): return ':class:`%s`' % cls.__name__ elif cls.__module__ in ('__builtin__', 'builtins'): return ':class:`%s`' % cls.__name__ else: if sys.version_info >= (3, 7): # py37+ return _restify_py37(cls) else: return _restify_py36(cls)
def stringify(annotation: Any, mode: str = 'fully-qualified-except-typing') -> str: """Stringify type annotation object. :param mode: Specify a method how annotations will be stringified. 'fully-qualified-except-typing' Show the module name and qualified name of the annotation except the "typing" module. 'smart' Show the name of the annotation. 'fully-qualified' Show the module name and qualified name of the annotation. """ from sphinx.util import inspect # lazy loading if mode == 'smart': modprefix = '~' else: modprefix = '' if isinstance(annotation, str): if annotation.startswith("'") and annotation.endswith("'"): # might be a double Forward-ref'ed type. Go unquoting. return annotation[1:-1] else: return annotation elif isinstance(annotation, TypeVar): if (annotation.__module__ == 'typing' and mode in ('fully-qualified-except-typing', 'smart')): return annotation.__name__ else: return modprefix + '.'.join( [annotation.__module__, annotation.__name__]) elif inspect.isNewType(annotation): if sys.version_info > (3, 10): # newtypes have correct module info since Python 3.10+ return modprefix + '%s.%s' % (annotation.__module__, annotation.__name__) else: return annotation.__name__ elif not annotation: return repr(annotation) elif annotation is NoneType: return 'None' elif annotation in INVALID_BUILTIN_CLASSES: return modprefix + INVALID_BUILTIN_CLASSES[annotation] elif str(annotation).startswith('typing.Annotated'): # for py310+ pass elif (getattr(annotation, '__module__', None) == 'builtins' and getattr(annotation, '__qualname__', None)): if hasattr(annotation, '__args__'): # PEP 585 generic return repr(annotation) else: return annotation.__qualname__ elif annotation is Ellipsis: return '...' if sys.version_info >= (3, 7): # py37+ return _stringify_py37(annotation, mode) else: return _stringify_py36(annotation, mode)