Skip to content

Remove bound_vars field of CallableType #1375

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 1 commit into from
Apr 13, 2016
Merged
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
5 changes: 0 additions & 5 deletions mypy/applytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ def apply_generic_arguments(callable: CallableType, types: List[Type],
# 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.copy_modified(
arg_types=arg_types,
ret_type=expand_type(callable.ret_type, id_to_type),
variables=remaining_tvars,
bound_vars=callable.bound_vars + bound_vars,
)
26 changes: 2 additions & 24 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ def expand_type_by_instance(typ: Type, instance: Instance) -> Type:
variables = {} # type: Dict[int, Type]
for i in range(len(instance.args)):
variables[i + 1] = instance.args[i]
typ = expand_type(typ, variables)
if isinstance(typ, CallableType):
bounds = [] # type: List[Tuple[int, Type]]
for j in range(len(instance.args)):
bounds.append((j + 1, instance.args[j]))
typ = update_callable_implicit_bounds(cast(CallableType, typ), bounds)
else:
pass
return typ
return expand_type(typ, variables)


class ExpandTypeVisitor(TypeVisitor[Type]):
Expand Down Expand Up @@ -83,8 +75,7 @@ def visit_type_var(self, t: TypeVarType) -> Type:

def visit_callable_type(self, t: CallableType) -> Type:
return t.copy_modified(arg_types=self.expand_types(t.arg_types),
ret_type=t.ret_type.accept(self),
bound_vars=self.expand_bound_vars(t.bound_vars))
ret_type=t.ret_type.accept(self))

def visit_overloaded(self, t: Overloaded) -> Type:
items = [] # type: List[CallableType]
Expand All @@ -106,16 +97,3 @@ def expand_types(self, types: List[Type]) -> List[Type]:
for t in types:
a.append(t.accept(self))
return a

def expand_bound_vars(
self, types: List[Tuple[int, Type]]) -> List[Tuple[int, Type]]:
a = [] # type: List[Tuple[int, Type]]
for id, t in types:
a.append((id, t.accept(self)))
return a


def update_callable_implicit_bounds(
t: CallableType, arg_types: List[Tuple[int, Type]]) -> CallableType:
# FIX what if there are existing bounds?
return t.copy_modified(bound_vars=arg_types)
2 changes: 0 additions & 2 deletions mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ def visit_callable_type(self, ct: CallableType) -> None:
for val in v.values:
val.accept(self)
v.upper_bound.accept(self)
for i, t in ct.bound_vars:
t.accept(self)

def visit_ellipsis_type(self, e: EllipsisType) -> None:
pass # Nothing to descend into.
Expand Down
2 changes: 1 addition & 1 deletion mypy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def construct_function_type(self, args: List[Argument], ret_type: Type,
arg_kinds = [arg.kind for arg in args]
arg_names = [arg.variable.name() for arg in args]
return CallableType(arg_types, arg_kinds, arg_names, ret_type, None, name=None,
variables=None, bound_vars=[], line=line)
variables=None, line=line)

# Parsing statements

Expand Down
84 changes: 42 additions & 42 deletions mypy/test/data/typexport-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def g() -> None:
a = None # type: A[B]
f = a.f
[out]
MemberExpr(9) : def [1:B] () -> B
MemberExpr(9) : def () -> B

[case testImplicitBoundTypeVarsForSelfMethodReference]
from typing import TypeVar, Generic
Expand All @@ -319,7 +319,7 @@ class A(Generic[T]):
self.f()
[out]
CallExpr(5) : T`1
MemberExpr(5) : def [1:T`1] () -> T`1
MemberExpr(5) : def () -> T`1
NameExpr(5) : A[T`1]

[case testGenericFunctionCallWithTypeApp-skip]
Expand All @@ -335,13 +335,13 @@ def f(a: T) -> Tuple[T, T]: pass
CallExpr(5) : A
CallExpr(5) : Tuple[A, A]
NameExpr(5) : def () -> A
NameExpr(5) : def [-1:A] (a: A) -> Tuple[A, A]
TypeApplication(5) : def [-1:A] (a: A) -> Tuple[A, A]
NameExpr(5) : def (a: A) -> Tuple[A, A]
TypeApplication(5) : def (a: A) -> Tuple[A, A]
CallExpr(6) : A
CallExpr(6) : Tuple[Any, Any]
NameExpr(6) : def () -> A
NameExpr(6) : def [-1:Any] (a: Any) -> Tuple[Any, Any]
TypeApplication(6) : def [-1:Any] (a: Any) -> Tuple[Any, Any]
NameExpr(6) : def (a: Any) -> Tuple[Any, Any]
TypeApplication(6) : def (a: Any) -> Tuple[Any, Any]

-- NOTE: Type applications are not supported for generic methods, so the
-- following test cases are commented out.
Expand All @@ -358,11 +358,11 @@ TypeApplication(6) : def [-1:Any] (a: Any) -> Tuple[Any, Any]
--[builtins fixtures/tuple.py]
--[out]
--CallExpr(2) : Tuple[A, A]
--MemberExpr(2) : def [-1:A] (A a) -> Tuple[A, A]
--TypeApplication(2) : def [-1:A] (A a) -> Tuple[A, A]
--MemberExpr(2) : def (A a) -> Tuple[A, A]
--TypeApplication(2) : def (A a) -> Tuple[A, A]
--CallExpr(3) : Tuple[Any, Any]
--MemberExpr(3) : def [-1:Any] (any a) -> Tuple[Any, Any]
--TypeApplication(3) : def [-1:Any] (any a) -> Tuple[Any, Any]
--MemberExpr(3) : def (any a) -> Tuple[Any, Any]
--TypeApplication(3) : def (any a) -> Tuple[Any, Any]

--[case testGenericMethodCallInGenericTypeWithTypeApp]
--## CallExpr|MemberExpr|TypeApplication
Expand All @@ -380,11 +380,11 @@ TypeApplication(6) : def [-1:Any] (a: Any) -> Tuple[Any, Any]
--[builtins fixtures/tuple.py]
--[out]
--CallExpr(6) : Tuple[C, B]
--MemberExpr(6) : def [1:C, -1:B] (B a) -> Tuple[C, B]
--TypeApplication(6) : def [1:C, -1:B] (B a) -> Tuple[C, B]
--MemberExpr(6) : def (B a) -> Tuple[C, B]
--TypeApplication(6) : def (B a) -> Tuple[C, B]
--CallExpr(7) : Tuple[C, Any]
--MemberExpr(7) : def [1:C, -1:Any] (any a) -> Tuple[C, Any]
--TypeApplication(7) : def [1:C, -1:Any] (any a) -> Tuple[C, Any]
--MemberExpr(7) : def (any a) -> Tuple[C, Any]
--TypeApplication(7) : def (any a) -> Tuple[C, Any]

[case testGenericTypeVariableInference]
from typing import TypeVar, Generic
Expand All @@ -397,8 +397,8 @@ A(A(B()))
CallExpr(6) : A[A[B]]
CallExpr(6) : A[B]
CallExpr(6) : B
NameExpr(6) : def [-1:A[B]] (a: A[B]) -> A[A[B]]
NameExpr(6) : def [-1:B] (a: B) -> A[B]
NameExpr(6) : def (a: A[B]) -> A[A[B]]
NameExpr(6) : def (a: B) -> A[B]
NameExpr(6) : def () -> B


Expand All @@ -417,7 +417,7 @@ class B(A[C]):
self.f(c)
[out]
CallExpr(8) : void
MemberExpr(8) : def [1:C] (a: C)
MemberExpr(8) : def (a: C)
NameExpr(8) : C
NameExpr(8) : B

Expand All @@ -433,7 +433,7 @@ class B(A[C, T], Generic[T]):
self.f(c)
[out]
CallExpr(9) : void
MemberExpr(9) : def [1:C, 2:T`1] (a: C)
MemberExpr(9) : def (a: C)
NameExpr(9) : C
NameExpr(9) : B[T`1]

Expand All @@ -449,7 +449,7 @@ c = None # type: C
b.f(c)
[out]
CallExpr(9) : void
MemberExpr(9) : def [1:C] (a: C)
MemberExpr(9) : def (a: C)
NameExpr(9) : B
NameExpr(9) : C

Expand Down Expand Up @@ -536,10 +536,10 @@ class B: pass
b = None # type: B
[out]
CallExpr(4) : A[B]
NameExpr(4) : def [-1:B] (a: B) -> A[B]
NameExpr(4) : def (a: B) -> A[B]
NameExpr(4) : B
CallExpr(5) : A[builtins.object]
NameExpr(5) : def [-1:builtins.object] (a: builtins.object) -> A[builtins.object]
NameExpr(5) : def (a: builtins.object) -> A[builtins.object]
NameExpr(5) : B

[case testGenericCallInDynamicallyTypedFunction]
Expand All @@ -550,7 +550,7 @@ def f():
class A(Generic[T]): pass
[out]
CallExpr(4) : A[Any]
NameExpr(4) : def [-1:Any] () -> A[Any]
NameExpr(4) : def () -> A[Any]

[case testGenericCallInDynamicallyTypedFunction2]
from typing import TypeVar, Generic
Expand All @@ -561,7 +561,7 @@ class A(Generic[T]):
def __init__(self, x: T) -> None: pass
[out]
CallExpr(4) : A[Any]
NameExpr(4) : def [-1:Any] (x: Any) -> A[Any]
NameExpr(4) : def (x: Any) -> A[Any]
NameExpr(4) : def () -> Any

[case testGenericCallInDynamicallyTypedFunction3]
Expand All @@ -572,7 +572,7 @@ def f():
def g(x: t) -> t: pass
[out]
CallExpr(4) : Any
NameExpr(4) : def [-1:Any] (x: Any) -> Any
NameExpr(4) : def (x: Any) -> Any


-- Generic types and type inference
Expand Down Expand Up @@ -623,14 +623,14 @@ class B: pass
class C: pass
[out]
CallExpr(4) : A[B]
NameExpr(4) : def [-1:B] (x: B) -> A[B]
NameExpr(4) : def (x: B) -> A[B]
NameExpr(4) : A[B]
NameExpr(4) : B
NameExpr(5) : A[B]
CallExpr(6) : A[B]
CallExpr(6) : A[C]
NameExpr(6) : def [-1:B] (x: B) -> A[B]
NameExpr(6) : def [-1:C] (x: C) -> A[C]
NameExpr(6) : def (x: B) -> A[B]
NameExpr(6) : def (x: C) -> A[C]
NameExpr(6) : A[B]
NameExpr(6) : A[C]
NameExpr(6) : B
Expand All @@ -656,8 +656,8 @@ def g(a: S) -> B[S]: pass
CallExpr(5) : A[C]
CallExpr(5) : B[A[C]]
NameExpr(5) : C
NameExpr(5) : def [-1:C] (a: C) -> A[C]
NameExpr(5) : def [-1:A[C]] (a: A[C]) -> B[A[C]]
NameExpr(5) : def (a: C) -> A[C]
NameExpr(5) : def (a: A[C]) -> B[A[C]]

[case testInferListLiterals]
from typing import List
Expand Down Expand Up @@ -688,7 +688,7 @@ def f(a: A) -> B: pass
[builtins fixtures/list.py]
[out]
CallExpr(4) : builtins.list[B]
NameExpr(4) : def [-1:A, -2:B] (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(4) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(5) : def (a: A) -> B
CallExpr(6) : A
ListExpr(6) : builtins.list[A]
Expand Down Expand Up @@ -752,7 +752,7 @@ def f(a: A) -> B: pass
[builtins fixtures/list.py]
[out]
CallExpr(5) : builtins.list[B]
NameExpr(5) : def [-1:A, -2:B] (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(5) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
CallExpr(6) : B
FuncExpr(6) : def (A) -> B
NameExpr(6) : def (a: A) -> B
Expand All @@ -773,7 +773,7 @@ class B: pass
def f(a: A) -> B: pass
[builtins fixtures/list.py]
[out]
NameExpr(6) : def [-1:A, -2:B] (f: def (A) -> builtins.list[B], a: builtins.list[A]) -> builtins.list[B]
NameExpr(6) : def (f: def (A) -> builtins.list[B], a: builtins.list[A]) -> builtins.list[B]
FuncExpr(7) : def (A) -> builtins.list[B]
ListExpr(7) : builtins.list[B]
NameExpr(7) : def (a: A) -> B
Expand All @@ -795,7 +795,7 @@ class A: pass
-- TODO We probably should not silently infer 'Any' types in statically typed
-- context. Perhaps just fail instead?
CallExpr(5) : builtins.list[Any]
NameExpr(5) : def [-1:A, -2:Any] (f: builtins.list[def (A) -> Any], a: builtins.list[A]) -> builtins.list[Any]
NameExpr(5) : def (f: builtins.list[def (A) -> Any], a: builtins.list[A]) -> builtins.list[Any]
FuncExpr(6) : def (A) -> A
ListExpr(6) : builtins.list[def (A) -> Any]
NameExpr(6) : A
Expand All @@ -816,7 +816,7 @@ class B: pass
[builtins fixtures/list.py]
[out]
CallExpr(5) : builtins.list[B]
NameExpr(5) : def [-1:A, -2:B] (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(5) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
FuncExpr(6) : def (A) -> B
MemberExpr(6) : B
NameExpr(6) : A
Expand All @@ -837,7 +837,7 @@ class B: pass
[builtins fixtures/list.py]
[out]
CallExpr(5) : builtins.list[B]
NameExpr(5) : def [-1:A, -2:B] (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(5) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(6) : builtins.list[A]
FuncExpr(7) : def (A) -> B
MemberExpr(7) : B
Expand Down Expand Up @@ -937,7 +937,7 @@ class A:
def f(self, x: t) -> None: pass
A.f(A(), B())
[out]
MemberExpr(7) : def [-1:B] (self: A, x: B)
MemberExpr(7) : def (self: A, x: B)

[case testUnboundMethodOfGenericClass]
## MemberExpr
Expand All @@ -951,7 +951,7 @@ a_b = A() # type: A[B]
A.f(a_b, B())
[out]
MemberExpr(7) : def [t] (self: A[t`1], x: t`1)
MemberExpr(9) : def [1:B] (self: A[B], x: B)
MemberExpr(9) : def (self: A[B], x: B)

[case testUnboundOverloadedMethodOfGenericClass]
## CallExpr
Expand Down Expand Up @@ -991,7 +991,7 @@ ab = None # type: A[B]
o = None # type: object
A.f(ab, o)
[out]
MemberExpr(10) : def [1:B, -1:builtins.object] (self: A[B], y: builtins.object)
MemberExpr(10) : def (self: A[B], y: builtins.object)


-- Type variables with value restriction
Expand All @@ -1006,8 +1006,8 @@ def f(x: T) -> None: pass
f(1)
f('x')
[out]
NameExpr(5) : def [-1:builtins.int] (x: builtins.int)
NameExpr(6) : def [-1:builtins.str] (x: builtins.str)
NameExpr(5) : def (x: builtins.int)
NameExpr(6) : def (x: builtins.str)

[case testTypeVariableWithValueRestrictionAndSubtype]
## NameExpr|CallExpr
Expand All @@ -1019,7 +1019,7 @@ s = None # type: S
f(s)
[out]
CallExpr(7) : builtins.str
NameExpr(7) : def [-1:builtins.str] (x: builtins.str) -> builtins.str
NameExpr(7) : def (x: builtins.str) -> builtins.str
NameExpr(7) : S


Expand Down Expand Up @@ -1108,7 +1108,7 @@ IntExpr(13) : builtins.int
ListExpr(13) : builtins.list[builtins.int]
CallExpr(14) : void
NameExpr(14) : def (s: builtins.int) -> builtins.int
NameExpr(14) : def [-1:builtins.int, -2:builtins.int] (fun: def (builtins.int) -> builtins.int, iter: builtins.list[builtins.int])
NameExpr(14) : def (fun: def (builtins.int) -> builtins.int, iter: builtins.list[builtins.int])
NameExpr(15) : builtins.list[builtins.int]


Expand Down
10 changes: 1 addition & 9 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ def visit_callable_type(self, t: CallableType) -> Type:
return t.copy_modified(arg_types=self.anal_array(t.arg_types),
ret_type=t.ret_type.accept(self),
fallback=t.fallback or self.builtin_type('builtins.function'),
variables=self.anal_var_defs(t.variables),
bound_vars=self.anal_bound_vars(t.bound_vars))
variables=self.anal_var_defs(t.variables))

def visit_tuple_type(self, t: TupleType) -> Type:
if t.implicit:
Expand Down Expand Up @@ -247,13 +246,6 @@ def anal_array(self, a: List[Type]) -> List[Type]:
res.append(t.accept(self))
return res

def anal_bound_vars(self,
a: List[Tuple[int, Type]]) -> List[Tuple[int, Type]]:
res = [] # type: List[Tuple[int, Type]]
for id, t in a:
res.append((id, t.accept(self)))
return res

def anal_var_defs(self, var_defs: List[TypeVarDef]) -> List[TypeVarDef]:
a = [] # type: List[TypeVarDef]
for vd in var_defs:
Expand Down
Loading