Skip to content

Commit 832ccc4

Browse files
committed
Move function_type and callable_type
1 parent 61cdc95 commit 832ccc4

File tree

8 files changed

+54
-51
lines changed

8 files changed

+54
-51
lines changed

mypy/checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
Type, AnyType, CallableType, FunctionLike, Overloaded, TupleType, TypedDictType,
3535
Instance, NoneType, strip_type, TypeType, TypeOfAny,
3636
UnionType, TypeVarId, TypeVarType, PartialType, DeletedType, UninhabitedType, TypeVarDef,
37-
function_type, is_named_instance, union_items, TypeQuery, LiteralType,
37+
is_named_instance, union_items, TypeQuery, LiteralType,
3838
is_optional, remove_optional, TypeTranslator, StarType, get_proper_type, ProperType,
3939
get_proper_types, is_literal_type
4040
)
@@ -50,7 +50,7 @@
5050
from mypy.typeops import (
5151
map_type_from_supertype, bind_self, erase_to_bound, make_simplified_union,
5252
erase_def_to_union_or_bound, erase_to_union_or_bound,
53-
true_only, false_only,
53+
true_only, false_only, function_type,
5454
)
5555
from mypy import message_registry
5656
from mypy.subtypes import (

mypy/checkexpr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Type, AnyType, CallableType, Overloaded, NoneType, TypeVarDef,
1818
TupleType, TypedDictType, Instance, TypeVarType, ErasedType, UnionType,
1919
PartialType, DeletedType, UninhabitedType, TypeType, TypeOfAny, LiteralType, LiteralValue,
20-
is_named_instance, function_type, callable_type, FunctionLike,
20+
is_named_instance, FunctionLike,
2121
StarType, is_optional, remove_optional, is_generic_instance, get_proper_type, ProperType,
2222
get_proper_types
2323
)
@@ -59,6 +59,7 @@
5959
from mypy.plugin import Plugin, MethodContext, MethodSigContext, FunctionContext
6060
from mypy.typeops import (
6161
tuple_fallback, make_simplified_union, true_only, false_only, erase_to_union_or_bound,
62+
function_type, callable_type,
6263
)
6364
import mypy.errorcodes as codes
6465

mypy/checkmember.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mypy.types import (
77
Type, Instance, AnyType, TupleType, TypedDictType, CallableType, FunctionLike, TypeVarDef,
88
Overloaded, TypeVarType, UnionType, PartialType, TypeOfAny, LiteralType,
9-
DeletedType, NoneType, TypeType, function_type, get_type_vars, get_proper_type, ProperType
9+
DeletedType, NoneType, TypeType, get_type_vars, get_proper_type, ProperType
1010
)
1111
from mypy.nodes import (
1212
TypeInfo, FuncBase, Var, FuncDef, SymbolNode, Context, MypyFile, TypeVarExpr,
@@ -24,7 +24,7 @@
2424
from mypy import meet
2525
from mypy.typeops import (
2626
tuple_fallback, bind_self, erase_to_bound, class_callable, type_object_type_from_function,
27-
make_simplified_union,
27+
make_simplified_union, function_type,
2828
)
2929

3030
if TYPE_CHECKING: # import for forward declaration only

mypy/semanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@
8484
from mypy.errorcodes import ErrorCode
8585
from mypy import message_registry, errorcodes as codes
8686
from mypy.types import (
87-
FunctionLike, UnboundType, TypeVarDef, TupleType, UnionType, StarType, function_type,
87+
FunctionLike, UnboundType, TypeVarDef, TupleType, UnionType, StarType,
8888
CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
8989
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
9090
get_proper_type, get_proper_types
9191
)
92+
from mypy.typeops import function_type
9293
from mypy.type_visitor import TypeQuery
9394
from mypy.nodes import implicit_module_attrs
9495
from mypy.typeanal import (

mypy/semanal_infer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from mypy.nodes import Expression, Decorator, CallExpr, FuncDef, RefExpr, Var, ARG_POS
66
from mypy.types import (
7-
Type, CallableType, AnyType, TypeOfAny, TypeVarType, function_type, ProperType, get_proper_type
7+
Type, CallableType, AnyType, TypeOfAny, TypeVarType, ProperType, get_proper_type
88
)
9+
from mypy.typeops import function_type
910
from mypy.typevars import has_no_typevars
1011
from mypy.semanal_shared import SemanticAnalyzerInterface
1112

mypy/subtypes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing_extensions import Final
55

66
from mypy.types import (
7-
Type, AnyType, UnboundType, TypeVisitor, FormalArgument, NoneType, function_type,
7+
Type, AnyType, UnboundType, TypeVisitor, FormalArgument, NoneType,
88
Instance, TypeVarType, CallableType, TupleType, TypedDictType, UnionType, Overloaded,
99
ErasedType, PartialType, DeletedType, UninhabitedType, TypeType, is_named_instance,
1010
FunctionLike, TypeOfAny, LiteralType, ProperType, get_proper_type
@@ -616,8 +616,8 @@ def find_node_type(node: Union[Var, FuncBase], itype: Instance, subtype: Type) -
616616
from mypy.typeops import bind_self
617617

618618
if isinstance(node, FuncBase):
619-
typ = function_type(node,
620-
fallback=Instance(itype.type.mro[-1], [])) # type: Optional[Type]
619+
typ = mypy.typeops.function_type(
620+
node, fallback=Instance(itype.type.mro[-1], [])) # type: Optional[Type]
621621
else:
622622
typ = node.type
623623
typ = get_proper_type(typ)

mypy/typeops.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
from mypy.types import (
1111
TupleType, Instance, FunctionLike, Type, CallableType, TypeVarDef, Overloaded,
1212
TypeVarType, TypeType, UninhabitedType, FormalArgument, UnionType, NoneType,
13-
ProperType, get_proper_type, get_proper_types, copy_type
13+
AnyType, TypeOfAny, ProperType, get_proper_type, get_proper_types, copy_type
1414
)
1515
from mypy.nodes import (
16-
TypeInfo, TypeVar, ARG_STAR,
16+
FuncBase, FuncItem, OverloadedFuncDef, TypeInfo, TypeVar, ARG_STAR, ARG_STAR2,
1717
)
1818
from mypy.maptype import map_instance_to_supertype
1919
from mypy.expandtype import expand_type_by_instance, expand_type
20+
from mypy.sharedparse import argument_elide_name
2021

2122
from mypy.typevars import fill_typevars
2223

@@ -369,3 +370,41 @@ def erase_to_union_or_bound(typ: TypeVarType) -> ProperType:
369370
return make_simplified_union(typ.values)
370371
else:
371372
return get_proper_type(typ.upper_bound)
373+
374+
375+
def function_type(func: FuncBase, fallback: Instance) -> FunctionLike:
376+
if func.type:
377+
assert isinstance(func.type, FunctionLike)
378+
return func.type
379+
else:
380+
# Implicit type signature with dynamic types.
381+
if isinstance(func, FuncItem):
382+
return callable_type(func, fallback)
383+
else:
384+
# Broken overloads can have self.type set to None.
385+
# TODO: should we instead always set the type in semantic analyzer?
386+
assert isinstance(func, OverloadedFuncDef)
387+
any_type = AnyType(TypeOfAny.from_error)
388+
dummy = CallableType([any_type, any_type],
389+
[ARG_STAR, ARG_STAR2],
390+
[None, None], any_type,
391+
fallback,
392+
line=func.line, is_ellipsis_args=True)
393+
# Return an Overloaded, because some callers may expect that
394+
# an OverloadedFuncDef has an Overloaded type.
395+
return Overloaded([dummy])
396+
397+
398+
def callable_type(fdef: FuncItem, fallback: Instance,
399+
ret_type: Optional[Type] = None) -> CallableType:
400+
return CallableType(
401+
[AnyType(TypeOfAny.unannotated)] * len(fdef.arg_names),
402+
fdef.arg_kinds,
403+
[None if argument_elide_name(n) else n for n in fdef.arg_names],
404+
ret_type or AnyType(TypeOfAny.unannotated),
405+
fallback,
406+
name=fdef.name(),
407+
line=fdef.line,
408+
column=fdef.column,
409+
implicit=True,
410+
)

mypy/types.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
INVARIANT, SymbolNode, ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, ARG_NAMED, ARG_NAMED_OPT,
1818
FuncDef,
1919
)
20-
from mypy.sharedparse import argument_elide_name
2120
from mypy.util import IdMapper
2221
from mypy.bogus_type import Bogus
2322

@@ -2081,44 +2080,6 @@ def copy_type(t: TP) -> TP:
20812080
return copy.copy(t)
20822081

20832082

2084-
def function_type(func: mypy.nodes.FuncBase, fallback: Instance) -> FunctionLike:
2085-
if func.type:
2086-
assert isinstance(func.type, FunctionLike)
2087-
return func.type
2088-
else:
2089-
# Implicit type signature with dynamic types.
2090-
if isinstance(func, mypy.nodes.FuncItem):
2091-
return callable_type(func, fallback)
2092-
else:
2093-
# Broken overloads can have self.type set to None.
2094-
# TODO: should we instead always set the type in semantic analyzer?
2095-
assert isinstance(func, mypy.nodes.OverloadedFuncDef)
2096-
any_type = AnyType(TypeOfAny.from_error)
2097-
dummy = CallableType([any_type, any_type],
2098-
[ARG_STAR, ARG_STAR2],
2099-
[None, None], any_type,
2100-
fallback,
2101-
line=func.line, is_ellipsis_args=True)
2102-
# Return an Overloaded, because some callers may expect that
2103-
# an OverloadedFuncDef has an Overloaded type.
2104-
return Overloaded([dummy])
2105-
2106-
2107-
def callable_type(fdef: mypy.nodes.FuncItem, fallback: Instance,
2108-
ret_type: Optional[Type] = None) -> CallableType:
2109-
return CallableType(
2110-
[AnyType(TypeOfAny.unannotated)] * len(fdef.arg_names),
2111-
fdef.arg_kinds,
2112-
[None if argument_elide_name(n) else n for n in fdef.arg_names],
2113-
ret_type or AnyType(TypeOfAny.unannotated),
2114-
fallback,
2115-
name=fdef.name(),
2116-
line=fdef.line,
2117-
column=fdef.column,
2118-
implicit=True,
2119-
)
2120-
2121-
21222083
def replace_alias_tvars(tp: Type, vars: List[str], subs: List[Type],
21232084
newline: int, newcolumn: int) -> ProperType:
21242085
"""Replace type variables in a generic type alias tp with substitutions subs

0 commit comments

Comments
 (0)