E52E Various updates to typing.py by gvanrossum · Pull Request #2382 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@gvanrossum
Copy link
Member

Hopefully this is all for Python 3.6b3.

@gvanrossum gvanrossum merged commit 7bc8fca into master Oct 31, 2016
@gvanrossum gvanrossum deleted the newtyping branch October 31, 2016 15:32
gvanrossum pushed a commit that referenced this pull request Nov 3, 2016
Fixes #606 as per PEP 484.

Now type aliases may be generic, so that the example in PEP works. Generic type aliases are allowed for generic classes, unions, callables, and tuples. For example:

Vec = Iterable[Tuple[T, T]]
TInt = Tuple[T, int]
UInt = Union[T, int]
CBack = Callable[..., T]

The aliases could be used as specified in PEP 484, e.g. either one specifies all free type variables, or if unspecified they are all substituted by Any, for example:

def fun(v: Vec[T]) -> Vec[T]: # Same as Iterable[Tuple[T, T]]
    ...
v1: Vec[int] = []      # Same as Iterable[Tuple[int, int]]
v2: Vec = []           # Same as Iterable[Tuple[Any, Any]]
v3: Vec[int, int] = [] # Error: Invalid alias, too many type arguments!

Generic aliases may be used everywhere where a normal generic type could be used (in annotations, generic base classes etc, and as of recently also in runtime expressions). Nested and chained aliases are allowed, but excessive use of those is discouraged in the docs. Like ordinary (non-generic) aliases, generic ones may be imported from other modules.

NOTE: Many examples in the tests and in docs require a recent version of typing.py from python/typing to work at runtime (see #2382). This feature may be used with older versions of typing.py by using type comments or "forward references".
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

Successfully merging this pull request may close these issues.

2 participants

0