Skip to content

Tuple[] internals changes in Python 3.6  #11

Closed
@miohtama

Description

@miohtama

Python 3.6 TupleMeta no longer has __tuple_args__. Hitting a Tuple annotation when running sphinx-build causes exception.

Instead one can extra arguments from Tuple like:

def format_annotation(annotation):
    if inspect.isclass(annotation):
        if annotation.__module__ == 'builtins':
            if annotation.__qualname__ == 'NoneType':
                return '``None``'
            else:
                return ':class:`{}`'.format(annotation.__qualname__)

        extra = ''
        if annotation.__module__ in ('typing', 'backports.typing'):
            if annotation.__qualname__ == 'Union':
                params = annotation.__union_params__
                if len(params) == 2 and params[1].__qualname__ == 'NoneType':
                    annotation = Optional
                    params = (params[0],)
            elif annotation.__qualname__ == 'Tuple':

                if hasattr(annotation, "__tuple_params__"):
                    # Python 3.5
                    params = annotation.__tuple_params__
                    if annotation.__tuple_use_ellipsis__:
                        params += ('...',)
                else:
                    # Python 3.6
                    params = getattr(annotation, "__args__", None)
                    # TODO: Handle ellipsis

            else:
                params = getattr(annotation, '__parameters__', None)

            if params:
                extra = '\\[' + ', '.join(format_annotation(param) for param in params) + ']'

        return ':class:`~{}.{}`{}'.format(annotation.__module__, annotation.__qualname__, extra)

    return str(annotation)

However, I did not yet figure out how to handle the ellipsis case, as internals of typing.py are pretty mindblowing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions