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

[very much a wip] pep 612 #9250

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions mypy/applytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import mypy.sametypes
from mypy.expandtype import expand_type
from mypy.types import (
Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType, get_proper_types
Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType, get_proper_types,
TypeVarDef, TypeVarLikeDef, ProperType
)
from mypy.nodes import Context

Expand All @@ -29,20 +30,22 @@ def apply_generic_arguments(
# Check that inferred type variable values are compatible with allowed
# values and bounds. Also, promote subtype values to allowed values.
types = get_proper_types(orig_types)
for i, type in enumerate(types):
assert not isinstance(type, PartialType), "Internal error: must never apply partial type"
values = get_proper_types(callable.variables[i].values)
if type is None:
continue

# Create a map from type variable id to target type.
id_to_type = {} # type: Dict[TypeVarId, Type]

def get_target_type(tvar: TypeVarLikeDef, type: ProperType) -> Optional[Type]:
assert isinstance(tvar, TypeVarDef), "TODO(shantanu) paramspec"
values = get_proper_types(tvar.values)
if values:
if isinstance(type, AnyType):
continue
return type
if isinstance(type, TypeVarType) and type.values:
# Allow substituting T1 for T if every allowed value of T1
# is also a legal value of T.
if all(any(mypy.sametypes.is_same_type(v, v1) for v in values)
for v1 in type.values):
continue
return type
matching = []
for value in values:
if mypy.subtypes.is_subtype(type, value):
Expand All @@ -53,28 +56,26 @@ def apply_generic_arguments(
for match in matching[1:]:
if mypy.subtypes.is_subtype(match, best):
best = match
types[i] = best
else:
if skip_unsatisfied:
types[i] = None
else:
report_incompatible_typevar_value(callable, type, callable.variables[i].name,
context)
return best
if skip_unsatisfied:
return None
report_incompatible_typevar_value(callable, type, tvar.name, context)
else:
upper_bound = callable.variables[i].upper_bound
upper_bound = tvar.upper_bound
if not mypy.subtypes.is_subtype(type, upper_bound):
if skip_unsatisfied:
types[i] = None
else:
report_incompatible_typevar_value(callable, type, callable.variables[i].name,
context)
return None
report_incompatible_typevar_value(callable, type, tvar.name, context)
return type

# Create a map from type variable id to target type.
id_to_type = {} # type: Dict[TypeVarId, Type]
for i, tv in enumerate(tvars):
typ = types[i]
if typ:
id_to_type[tv.id] = typ
for tvar, type in zip(tvars, types):
assert not isinstance(type, PartialType), "Internal error: must never apply partial type"
if type is None:
continue

target_type = get_target_type(tvar, type)
if target_type is not None:
id_to_type[tvar.id] = target_type

# Apply arguments to argument types.
arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]
Expand Down
15 changes: 7 additions & 8 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
erase_def_to_union_or_bound, erase_to_union_or_bound, coerce_to_literal,
try_getting_str_literals_from_type, try_getting_int_literals_from_type,
tuple_fallback, is_singleton_type, try_expanding_enum_to_union,
true_only, false_only, function_type, TypeVarExtractor, custom_special_method,
true_only, false_only, function_type, get_type_vars, custom_special_method,
is_literal_type_like,
)
from mypy import message_registry
Expand Down Expand Up @@ -1389,15 +1389,14 @@ def expand_typevars(self, defn: FuncItem,
typ: CallableType) -> List[Tuple[FuncItem, CallableType]]:
# TODO use generator
subst = [] # type: List[List[Tuple[TypeVarId, Type]]]
tvars = typ.variables or []
tvars = tvars[:]
tvars = list(typ.variables) or []
if defn.info:
# Class type variables
tvars += defn.info.defn.type_vars or []
# TODO(shantanu): audit for paramspec
for tvar in tvars:
if tvar.values:
subst.append([(tvar.id, value)
for value in tvar.values])
if isinstance(tvar, TypeVarDef) and tvar.values:
subst.append([(tvar.id, value) for value in tvar.values])
# Make a copy of the function to check for each combination of
# value restricted type variables. (Except when running mypyc,
# where we need one canonical version of the function.)
Expand Down Expand Up @@ -5325,7 +5324,7 @@ def detach_callable(typ: CallableType) -> CallableType:

appear_map = {} # type: Dict[str, List[int]]
for i, inner_type in enumerate(type_list):
typevars_available = inner_type.accept(TypeVarExtractor())
typevars_available = get_type_vars(inner_type)
for var in typevars_available:
if var.fullname not in appear_map:
appear_map[var.fullname] = []
Expand All @@ -5335,7 +5334,7 @@ def detach_callable(typ: CallableType) -> CallableType:
for var_name, appearances in appear_map.items():
used_type_var_names.add(var_name)

all_type_vars = typ.accept(TypeVarExtractor())
all_type_vars = get_type_vars(typ)
new_variables = []
for var in set(all_type_vars):
if var.fullname not in used_type_var_names:
Expand Down
4 changes: 4 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DictionaryComprehension, ComplexExpr, EllipsisExpr, StarExpr, AwaitExpr, YieldExpr,
YieldFromExpr, TypedDictExpr, PromoteExpr, NewTypeExpr, NamedTupleExpr, TypeVarExpr,
TypeAliasExpr, BackquoteExpr, EnumCallExpr, TypeAlias, SymbolNode, PlaceholderNode,
ParamSpecExpr,
ARG_POS, ARG_OPT, ARG_NAMED, ARG_STAR, ARG_STAR2, LITERAL_TYPE, REVEAL_TYPE,
)
from mypy.literals import literal
Expand Down Expand Up @@ -3969,6 +3970,9 @@ def visit_temp_node(self, e: TempNode) -> Type:
def visit_type_var_expr(self, e: TypeVarExpr) -> Type:
return AnyType(TypeOfAny.special_form)

def visit_paramspec_var_expr(self, e: ParamSpecExpr) -> Type:
return AnyType(TypeOfAny.special_form)

def visit_newtype_expr(self, e: NewTypeExpr) -> Type:
return AnyType(TypeOfAny.special_form)

Expand Down
10 changes: 5 additions & 5 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Type checking of attribute access"""

from typing import cast, Callable, Optional, Union, List
from typing import cast, Callable, Optional, Union, List, Sequence
from typing_extensions import TYPE_CHECKING

from mypy.types import (
Type, Instance, AnyType, TupleType, TypedDictType, CallableType, FunctionLike, TypeVarDef,
Overloaded, TypeVarType, UnionType, PartialType, TypeOfAny, LiteralType,
TypeVarLikeDef, Overloaded, TypeVarType, UnionType, PartialType, TypeOfAny, LiteralType,
DeletedType, NoneType, TypeType, has_type_vars, get_proper_type, ProperType
)
from mypy.nodes import (
Expand Down Expand Up @@ -676,7 +676,7 @@ def analyze_class_attribute_access(itype: Instance,
name: str,
mx: MemberContext,
override_info: Optional[TypeInfo] = None,
original_vars: Optional[List[TypeVarDef]] = None
original_vars: Optional[Sequence[TypeVarLikeDef]] = None
) -> Optional[Type]:
"""Analyze access to an attribute on a class object.

Expand Down Expand Up @@ -839,7 +839,7 @@ def analyze_enum_class_attribute_access(itype: Instance,
def add_class_tvars(t: ProperType, isuper: Optional[Instance],
is_classmethod: bool,
original_type: Type,
original_vars: Optional[List[TypeVarDef]] = None) -> Type:
original_vars: Optional[Sequence[TypeVarLikeDef]] = None) -> Type:
"""Instantiate type variables during analyze_class_attribute_access,
e.g T and Q in the following:

Expand Down Expand Up @@ -883,7 +883,7 @@ class B(A[str]): pass
assert isuper is not None
t = cast(CallableType, expand_type_by_instance(t, isuper))
freeze_type_vars(t)
return t.copy_modified(variables=tvars + t.variables)
return t.copy_modified(variables=list(tvars) + list(t.variables))
elif isinstance(t, Overloaded):
return Overloaded([cast(CallableType, add_class_tvars(item, isuper,
is_classmethod, original_type,
Expand Down
12 changes: 9 additions & 3 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
NoneType, TypeVarType, Overloaded, TupleType, TypedDictType, UnionType,
ErasedType, PartialType, DeletedType, UninhabitedType, TypeType, TypeVarId,
FunctionLike, TypeVarDef, LiteralType, get_proper_type, ProperType,
TypeAliasType)
TypeAliasType, ParamSpecDef, ParamSpecType)


def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type:
Expand Down Expand Up @@ -40,9 +40,15 @@ def freshen_function_type_vars(callee: F) -> F:
tvdefs = []
tvmap = {} # type: Dict[TypeVarId, Type]
for v in callee.variables:
tvdef = TypeVarDef.new_unification_variable(v)
tvdef = v.new_unification_variable()
tvdefs.append(tvdef)
tvmap[v.id] = TypeVarType(tvdef)
if isinstance(tvdef, TypeVarDef):
tvtype = TypeVarType(tvdef) # type: Type
elif isinstance(tvdef, ParamSpecDef):
tvtype = ParamSpecType(tvdef)
else:
assert False
tvmap[v.id] = tvtype
fresh = cast(CallableType, expand_type(callee, tvmap)).copy_modified(variables=tvdefs)
return cast(F, fresh)
else:
Expand Down
14 changes: 9 additions & 5 deletions mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from mypy.types import (
CallableType, Instance, Overloaded, TupleType, TypedDictType,
TypeVarType, UnboundType, UnionType, TypeVisitor, LiteralType,
TypeType, NOT_READY, TypeAliasType, AnyType, TypeOfAny)
TypeType, NOT_READY, TypeAliasType, AnyType, TypeOfAny, TypeVarDef
)
from mypy.visitor import NodeVisitor
from mypy.lookup import lookup_fully_qualified

Expand Down Expand Up @@ -183,10 +184,13 @@ def visit_callable_type(self, ct: CallableType) -> None:
if ct.ret_type is not None:
ct.ret_type.accept(self)
for v in ct.variables:
if v.values:
for val in v.values:
val.accept(self)
v.upper_bound.accept(self)
if isinstance(v, TypeVarDef):
if v.values:
for val in v.values:
val.accept(self)
v.upper_bound.accept(self)
else:
assert False # TODO(shantanu): add param spec
for arg in ct.bound_args:
if arg:
arg.accept(self)
Expand Down
5 changes: 4 additions & 1 deletion mypy/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ConditionalExpr, EllipsisExpr, YieldFromExpr, YieldExpr, RevealExpr, SuperExpr,
TypeApplication, LambdaExpr, ListComprehension, SetComprehension, DictionaryComprehension,
GeneratorExpr, BackquoteExpr, TypeVarExpr, TypeAliasExpr, NamedTupleExpr, EnumCallExpr,
TypedDictExpr, NewTypeExpr, PromoteExpr, AwaitExpr, TempNode, AssignmentExpr,
TypedDictExpr, NewTypeExpr, PromoteExpr, AwaitExpr, TempNode, AssignmentExpr, ParamSpecExpr
)
from mypy.visitor import ExpressionVisitor

Expand Down Expand Up @@ -213,6 +213,9 @@ def visit_backquote_expr(self, e: BackquoteExpr) -> None:
def visit_type_var_expr(self, e: TypeVarExpr) -> None:
return None

def visit_paramspec_var_expr(self, e: ParamSpecExpr) -> None:
return None

def visit_type_alias_expr(self, e: TypeAliasExpr) -> None:
return None

Expand Down
24 changes: 14 additions & 10 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from mypy.errors import Errors
from mypy.types import (
Type, CallableType, Instance, TypeVarType, TupleType, TypedDictType, LiteralType,
UnionType, NoneType, AnyType, Overloaded, FunctionLike, DeletedType, TypeType,
UnionType, NoneType, AnyType, Overloaded, FunctionLike, DeletedType, TypeType, TypeVarDef,
UninhabitedType, TypeOfAny, UnboundType, PartialType, get_proper_type, ProperType,
get_proper_types
)
Expand Down Expand Up @@ -1868,16 +1868,20 @@ def [T <: int] f(self, x: int, y: T) -> None
if tp.variables:
tvars = []
for tvar in tp.variables:
upper_bound = get_proper_type(tvar.upper_bound)
if (isinstance(upper_bound, Instance) and
upper_bound.type.fullname != 'builtins.object'):
tvars.append('{} <: {}'.format(tvar.name, format_type_bare(upper_bound)))
elif tvar.values:
tvars.append('{} in ({})'
.format(tvar.name, ', '.join([format_type_bare(tp)
for tp in tvar.values])))
if isinstance(tvar, TypeVarDef):
upper_bound = get_proper_type(tvar.upper_bound)
if (isinstance(upper_bound, Instance) and
upper_bound.type.fullname != 'builtins.object'):
tvars.append('{} <: {}'.format(tvar.name, format_type_bare(upper_bound)))
elif tvar.values:
tvars.append('{} in ({})'
.format(tvar.name, ', '.join([format_type_bare(tp)
for tp in tvar.values])))
else:
tvars.append(tvar.name)
else:
tvars.append(tvar.name)
# For other TypeVarLikeDefs, just use the repr
tvars.append(repr(tvar))
s = '[{}] {}'.format(', '.join(tvars), s)
return 'def {}'.format(s)

Expand Down
Loading