forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'generic-func-subtyping2'
- Loading branch information
Showing
12 changed files
with
280 additions
and
212 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from typing import List, Dict | ||
|
||
import mypy.subtypes | ||
from mypy.expandtype import expand_type | ||
from mypy.types import Type, Callable, AnyType | ||
from mypy.messages import MessageBuilder | ||
from mypy.nodes import Context | ||
|
||
|
||
def apply_generic_arguments(callable: Callable, types: List[Type], | ||
msg: MessageBuilder, context: Context) -> Type: | ||
"""Apply generic type arguments to a callable type. | ||
For example, applying [int] to 'def [T] (T) -> T' results in | ||
'def [-1:int] (int) -> int'. Here '[-1:int]' is an implicit bound type | ||
variable. | ||
Note that each type can be None; in this case, it will not be applied. | ||
""" | ||
tvars = callable.variables | ||
if len(tvars) != len(types): | ||
msg.incompatible_type_application(len(tvars), len(types), context) | ||
return AnyType() | ||
|
||
# Check that inferred type variable values are compatible with allowed | ||
# values. Also, promote subtype values to allowed values. | ||
types = types[:] | ||
for i, type in enumerate(types): | ||
values = callable.variables[i].values | ||
if values and type: | ||
if isinstance(type, AnyType): | ||
continue | ||
for value in values: | ||
if mypy.subtypes.is_subtype(type, value): | ||
types[i] = value | ||
break | ||
else: | ||
msg.incompatible_typevar_value(callable, i + 1, type, context) | ||
|
||
# Create a map from type variable id to target type. | ||
id_to_type = {} # type: Dict[int, Type] | ||
for i, tv in enumerate(tvars): | ||
if types[i]: | ||
id_to_type[tv.id] = types[i] | ||
|
||
# Apply arguments to argument types. | ||
arg_types = [expand_type(at, id_to_type) for at in callable.arg_types] | ||
|
||
bound_vars = [(tv.id, id_to_type[tv.id]) | ||
for tv in tvars | ||
if tv.id in id_to_type] | ||
|
||
# The callable may retain some type vars if only some were applied. | ||
remaining_tvars = [tv for tv in tvars if tv.id not in id_to_type] | ||
|
||
return Callable(arg_types, | ||
callable.arg_kinds, | ||
callable.arg_names, | ||
expand_type(callable.ret_type, id_to_type), | ||
callable.fallback, | ||
callable.name, | ||
remaining_tvars, | ||
callable.bound_vars + bound_vars, | ||
callable.line, callable.repr) |
This file contains 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 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 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 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 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
Oops, something went wrong.