-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Generic type aliases #2378
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
Merged
Merged
Generic type aliases #2378
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5dc0090
Initial crude implementation of GA
f6d862b
Add support for Union, Tuple, Callable
ilevkivskyi b0790d4
Add runtime behaviour; outline tests
ilevkivskyi 7ddd6ab
Formatting + better types
ilevkivskyi 633e3d1
Add some tests
ilevkivskyi 07baa51
More tests, better error reporting
ilevkivskyi 56b7ae7
Add tests, comments, and doctrings
ilevkivskyi cbcb2c0
Add even more tests, add documentation
ilevkivskyi 5c92b12
Last tests
ilevkivskyi 13e1fff
Corrections due to upstream refactoring
ee1c25c
Add few more examples to docs
ilevkivskyi 2edcf48
Do not substitute type variables if they are in runtime expression, r…
ilevkivskyi 64fc96e
First part of response to comments (bigger things)
ilevkivskyi 8c64111
Update tests; some formatting
ilevkivskyi c106204
Second part of response to comments: minor things (but many); Two mor…
ilevkivskyi feb9413
Fix last bits
ilevkivskyi d402a68
Minor correction to docs
ilevkivskyi 8cea156
Yet another tiny correction to docs
ilevkivskyi 382c53e
Improving error messages (+column numbers); main part
ilevkivskyi 863c576
Remaining part of response to comments (minor thigs)
544ff66
Remove debugging print()
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for Union, Tuple, Callable
- Loading branch information
commit f6d862bd70a9ef513d04cb76d8e6aed836ba659c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,11 @@ def analyze_type_alias(node: Expression, | |
# Quickly return None if the expression doesn't look like a type. Note | ||
# that we don't support straight string literals as type aliases | ||
# (only string literals within index expressions). | ||
|
||
if isinstance(node, RefExpr): | ||
if node.kind == UNBOUND_TVAR or node.kind == BOUND_TVAR: | ||
fail_func('Invalid type "{}" for aliasing'.format(node.fullname), node) | ||
return None | ||
if not (isinstance(node.node, TypeInfo) or | ||
node.fullname == 'typing.Any' or | ||
node.kind == TYPE_ALIAS): | ||
|
@@ -48,7 +52,8 @@ def analyze_type_alias(node: Expression, | |
base = node.base | ||
if isinstance(base, RefExpr): | ||
if not (isinstance(base.node, TypeInfo) or | ||
base.fullname in type_constructors): | ||
base.fullname in type_constructors or | ||
base.kind == TYPE_ALIAS): | ||
return None | ||
else: | ||
return None | ||
|
@@ -61,7 +66,7 @@ def analyze_type_alias(node: Expression, | |
except TypeTranslationError: | ||
fail_func('Invalid type alias', node) | ||
return None | ||
analyzer = TypeAnalyser(lookup_func, lookup_fqn_func, fail_func) | ||
analyzer = TypeAnalyser(lookup_func, lookup_fqn_func, fail_func, aliasing=True) | ||
return type.accept(analyzer) | ||
|
||
|
||
|
@@ -74,60 +79,12 @@ class TypeAnalyser(TypeVisitor[Type]): | |
def __init__(self, | ||
lookup_func: Callable[[str, Context], SymbolTableNode], | ||
lookup_fqn_func: Callable[[str], SymbolTableNode], | ||
fail_func: Callable[[str, Context], None]) -> None: | ||
fail_func: Callable[[str, Context], None], *, | ||
aliasing = False) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
self.lookup = lookup_func | ||
self.lookup_fqn_func = lookup_fqn_func | ||
self.fail = fail_func | ||
|
||
|
||
def get_unbound_tvar_name(self, t: Type) -> str: | ||
if not isinstance(t, UnboundType): | ||
return None | ||
unbound = t | ||
sym = self.lookup(unbound.name, unbound) | ||
if sym is not None and (sym.kind == UNBOUND_TVAR or sym.kind == BOUND_TVAR): | ||
return unbound.name | ||
return None | ||
|
||
|
||
def unique_vars(self, tvars): | ||
# Get unbound type variables in order of appearance | ||
all_tvars = set(tvars) | ||
new_tvars = [] | ||
for t in tvars: | ||
if t in all_tvars: | ||
new_tvars.append(t) | ||
all_tvars.remove(t) | ||
return new_tvars | ||
|
||
|
||
def get_type_var_names(self, tp: Instance) -> List[str]: | ||
tvars = [] # type: List[str] | ||
if not isinstance(tp, Instance): | ||
return tvars | ||
for arg in tp.args: | ||
tvar = self.get_unbound_tvar_name(arg) | ||
if tvar: | ||
tvars.append(tvar) | ||
elif isinstance(arg, Instance): | ||
subvars = self.get_type_var_names(arg) | ||
if subvars: | ||
tvars.extend(subvars) | ||
return tvars | ||
|
||
|
||
def replace_alias_tvars(self, tp: Instance, vars: List[str], subs: List[Type]) -> None: | ||
if not isinstance(tp, Instance) or not subs: | ||
return AnyType() | ||
new_args = tp.args[:] | ||
for i, arg in enumerate(tp.args): | ||
tvar = self.get_unbound_tvar_name(arg) | ||
if tvar and tvar in vars: | ||
new_args[i] = subs[vars.index(tvar)] | ||
elif isinstance(arg, Instance): | ||
new_args[i] = self.replace_alias_tvars(arg, vars, subs) | ||
return Instance(tp.type, new_args, tp.line) | ||
|
||
self.aliasing = aliasing | ||
|
||
def visit_unbound_type(self, t: UnboundType) -> Type: | ||
if t.optional: | ||
|
@@ -193,19 +150,18 @@ def visit_unbound_type(self, t: UnboundType) -> Type: | |
elif sym.kind == TYPE_ALIAS: | ||
override = sym.type_override | ||
an_args = self.anal_array(t.args) | ||
found_vars = self.get_type_var_names(override) | ||
all_vars = self.unique_vars(found_vars) | ||
print(all_vars, an_args) | ||
all_vars = self.get_type_var_names(override) | ||
exp_len = len(all_vars) | ||
act_len = len(an_args) | ||
if exp_len > 0 and act_len == 0: | ||
# Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...] | ||
return self.replace_alias_tvars(override, all_vars, [AnyType()] * exp_len) | ||
if exp_len == 0 and act_len == 0: | ||
return override | ||
if act_len != exp_len: | ||
self.fail('Bad number of arguments for type alias, expected: %s, given: %s' | ||
% (exp_len, act_len), t) | ||
return AnyType() | ||
return t | ||
return self.replace_alias_tvars(override, all_vars, an_args) | ||
elif not isinstance(sym.node, TypeInfo): | ||
name = sym.fullname | ||
|
@@ -217,7 +173,9 @@ def visit_unbound_type(self, t: UnboundType) -> Type: | |
# as a base class -- however, this will fail soon at runtime so the problem | ||
# is pretty minor. | ||
return AnyType() | ||
self.fail('Invalid type "{}"'.format(name), t) | ||
# Allow unbount type variables when defining an alias | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: unbount. |
||
if not (self.aliasing and sym.kind == UNBOUND_TVAR): | ||
self.fail('Invalid type "{}"'.format(name), t) | ||
return t | ||
info = sym.node # type: TypeInfo | ||
if len(t.args) > 0 and info.fullname() == 'builtins.tuple': | ||
|
@@ -245,6 +203,60 @@ def visit_unbound_type(self, t: UnboundType) -> Type: | |
else: | ||
return AnyType() | ||
|
||
def get_tvar_name(self, t: Type) -> str: | ||
if not isinstance(t, UnboundType): | ||
return None | ||
sym = self.lookup(t.name, t) | ||
if sym is not None and (sym.kind == UNBOUND_TVAR or sym.kind == BOUND_TVAR): | ||
return t.name | ||
return None | ||
|
||
def get_type_var_names(self, tp: Type) -> List[str]: | ||
tvars = [] # type: List[str] | ||
if not isinstance(tp, (Instance, UnionType, TupleType, CallableType)): | ||
return tvars | ||
typ_args = (tp.args if isinstance(tp, Instance) else | ||
tp.items if not isinstance(tp, CallableType) else | ||
tp.arg_types + [tp.ret_type]) | ||
for arg in typ_args: | ||
tvar = self.get_tvar_name(arg) | ||
if tvar: | ||
tvars.append(tvar) | ||
else: | ||
subvars = self.get_type_var_names(arg) | ||
if subvars: | ||
tvars.extend(subvars) | ||
# Get unique type variables in order of appearance | ||
all_tvars = set(tvars) | ||
new_tvars = [] | ||
for t in tvars: | ||
if t in all_tvars: | ||
new_tvars.append(t) | ||
all_tvars.remove(t) | ||
return new_tvars | ||
|
||
def replace_alias_tvars(self, tp: Type, vars: List[str], subs: List[Type]) -> Type: | ||
if not isinstance(tp, (Instance, UnionType, TupleType, CallableType)) or not subs: | ||
return tp | ||
typ_args = (tp.args if isinstance(tp, Instance) else | ||
tp.items if not isinstance(tp, CallableType) else | ||
tp.arg_types + [tp.ret_type]) | ||
new_args = typ_args[:] | ||
for i, arg in enumerate(typ_args): | ||
tvar = self.get_tvar_name(arg) | ||
if tvar and tvar in vars: | ||
new_args[i] = subs[vars.index(tvar)] | ||
else: | ||
new_args[i] = self.replace_alias_tvars(arg, vars, subs) | ||
if isinstance(tp, Instance): | ||
return Instance(tp.type, new_args, tp.line) | ||
if isinstance(tp, TupleType): | ||
return tp.copy_modified(items=new_args) | ||
if isinstance(tp, UnionType): | ||
return UnionType.make_union(new_args, tp.line) | ||
if isinstance(tp, CallableType): | ||
return tp.copy_modified(arg_types=new_args[:-1], ret_type=new_args[-1]) | ||
|
||
def visit_any(self, t: AnyType) -> Type: | ||
return t | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find any tests revealing this message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum Good catch! Added a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make the error message clearer? I think it should explicitly state that you cannot create an alias for a type variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum OK, now the error explicitly says that type variable is invalid as target for type alias.