Hi everyone,
I’d like to continue the discussion from our last typing summit on explicit type aliases. A quick summary of the current state and the proposal --
Current state:
```
from typing import List
x = List[int] # considered a type alias
y : Type[List[int]] = List[int] # considered an expression
z : Any # considered a type alias
z : x = [] # fine
z : y = [] # invalid type error
```
Proposal (with explicit aliases):
```
from typing import List, TypeAlias
x = TypeAlias[List[int]] …
[View More] # considered a type alias
y = List[int] # considered an expression
z : Any # considered an expression
z = TypeAlias[Any] # considered a type alias
reveal_type(x) # Type[List[int]]
reveal_type(y) # Any (return type of __getitem__)
z : x = [] # fine
z : y = [] # invalid type error
```
Some of the benefits we’re hoping to gain from explicit type aliases:
* Clearly distinguish between an unannotated global assignment and a type alias, especially when parsing forward-referencing string annotations.
* Avoid the confusing case of type aliases in which some part of the type is invalid or undefined. This would facilitate warnings on invalid types at the alias definition rather than later on when the alias is used.
* Make valid and invalid types more intuitive to Python programmers by shifting from delineation of non-aliases with a meta annotation toward delineation of aliases with `TypeAlias`.
* Remove special treatment of values annotated as `Any`, which currently break all type aliasing rules. This will also allow us to clean up some of the distinctions we maintain between unannotated values and explicit Anys.
Note: We also considered denoting the alias as an annotation (e.g. `x: TypeAlias = int`) as an alternative to the syntax above.
I’m interested to hear your thoughts and suggestions on making type aliasing explicit going forward – we’d like to start implementing a version of this in Pyre soon!
Shannon
[View Less]
Hi,
We've just landed pep-593 (https://www.python.org/dev/peps/pep-0593/);
it proposes adding support to embed metadata inside type annotations.
I'm looking forward to hearing people's opinion.
Best,
Till
> Perhaps we can compromise and add an optional new syntax for type aliases that allows forward references? You would have to support both styles (old and new) but you could campaign with your users to switch to the new style and you could offer forward references in aliases and better diagnostics as a carrot (and maybe use a linter as a stick :-). An optional new way of doing things is a much easier sell to people who already have millions of lines of annotated code than a required change.
…
[View More]This sounds very reasonable & we’ll definitely start implementing this as an alternative approach to aliasing while supporting both syntaxes. Once it’s in place and working we’ll start pushing for a change to the new format, with the goal of eventually deprecating support for the old format in Pyre to reduce confusion.
In terms of the syntax itself, I agree with your rationale that `x: TypeAlias = int` is cleaner than the alternatives, so we’ll probably get started with that unless there are more concerns raised!
[View Less]