Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union type hint with Python 3.6.0 is no longer a class #380

Closed
gaborbernat opened this issue Jan 30, 2017 · 1 comment
Closed

Union type hint with Python 3.6.0 is no longer a class #380

gaborbernat opened this issue Jan 30, 2017 · 1 comment

Comments

@gaborbernat
Copy link

gaborbernat commented Jan 30, 2017

Here's an interesting interaction, with Python 3.6 the union type hint is no longer a class (while tuple still is),, as opposed to 3.5 where both are. Is this a bug, or a valid change? And if valid what's the reason for the inconsistency?

# coding=utf-8
""" Test type hints. """
import inspect
from typing import Tuple, Union, get_type_hints


def tuple_test(param: Tuple[str, ...]):
    """

    :param param:
    :return:
    """
    return param


def union_test(param: Union[str, int]):
    """

    :param param:
    :return:
    """
    return param

print(inspect.isclass(get_type_hints(tuple_test)['param']))
print(inspect.isclass(get_type_hints(union_test)['param']))

Produces the following output:

bernat@uvm ~/work/git/sphinx-autodoc-typehints (master●)$ python --version
Python 3.5.0
bernat@uvm ~/work/git/sphinx-autodoc-typehints (master●)$ python tests/docs/annotation/test_code/__init__.py
True
True
bernat@uvm ~/work/git/sphinx-autodoc-typehints (master●)$ pyenv local 3.6.0
bernat@uvm ~/work/git/sphinx-autodoc-typehints (master●)$ python --version
Python 3.6.0
bernat@uvm ~/work/git/sphinx-autodoc-typehints (master●)$ python tests/docs/annotation/test_code/__init__.py
True
False
@ilevkivskyi
Copy link
Member

This is intentional. Union has no way to be a valid runtime class (one can neither inherit it, nor instantiate). At the same time Tuple has a runtime counterpart tuple equivalent to Tuple[Any, ...].

By subclassing Tuple one can indicate that a certain class supports "tuple protocol" and there is no way to annotate __getitem__ as expressive as inheriting from Tuple[str, int].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants