本文整理汇总了Python中mypy.nodes.function_type函数的典型用法代码示例。如果您正苦于以下问题:Python function_type函数的具体用法?Python function_type怎么用?Python function_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了function_type函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: analyze_class_attribute_access
def analyze_class_attribute_access(itype: Instance,
name: str,
context: Context,
is_lvalue: bool,
builtin_type: Callable[[str], Instance],
msg: MessageBuilder) -> Type:
node = itype.type.get(name)
if not node:
return None
is_decorated = isinstance(node.node, Decorator)
is_method = is_decorated or isinstance(node.node, FuncDef)
if is_lvalue:
if is_method:
msg.cant_assign_to_method(context)
if isinstance(node.node, TypeInfo):
msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, context)
if itype.type.is_enum and not (is_lvalue or is_decorated or is_method):
return itype
t = node.type
if t:
is_classmethod = is_decorated and cast(Decorator, node.node).func.is_class
return add_class_tvars(t, itype.type, is_classmethod, builtin_type)
if isinstance(node.node, TypeInfo):
return type_object_type(cast(TypeInfo, node.node), builtin_type)
return function_type(cast(FuncBase, node.node), builtin_type('builtins.function'))
示例2: analyse_class_attribute_access
def analyse_class_attribute_access(itype: Instance, name: str,
context: Context, is_lvalue: bool,
msg: MessageBuilder) -> Type:
node = itype.type.get(name)
if not node:
return None
is_decorated = isinstance(node.node, Decorator)
is_method = is_decorated or isinstance(node.node, FuncDef)
if is_lvalue:
if is_method:
msg.cant_assign_to_method(context)
if isinstance(node.node, TypeInfo):
msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, context)
t = node.type
if t:
is_classmethod = is_decorated and cast(Decorator, node.node).func.is_class
return add_class_tvars(t, itype.type, is_classmethod)
if isinstance(node.node, TypeInfo):
# TODO add second argument
return type_object_type(cast(TypeInfo, node.node), None)
return function_type(cast(FuncBase, node.node))
示例3: transform_function_body
def transform_function_body(self, fdef: FuncDef) -> None:
"""Transform the body of a function."""
self.dynamic_funcs.append(fdef.is_implicit)
# FIX overloads
self.return_types.append(cast(Callable, function_type(fdef)).ret_type)
super().visit_func_def(fdef)
self.return_types.pop()
self.dynamic_funcs.pop()
示例4: is_simple_override
def is_simple_override(fdef, info):
"""Is function an override with the same type precision as the original?
Compare to the original method in the superclass of info.
"""
# If this is not an override, this can't be a simple override either.
# Generic inheritance is not currently supported, since we need to map
# type variables between types; in the future this restriction can be
# lifted.
if info.base is None or info.base.type_vars != []:
return False
orig = info.base.get_method(fdef.name())
# Ignore the first argument (self) when determining type sameness.
# TODO overloads
newtype = function_type(fdef)
newtype = replace_self_type(newtype, Any())
origtype = function_type(orig)
origtype = replace_self_type(origtype, Any())
return is_same_type(newtype, origtype)
示例5: visit_overloaded_func_def
def visit_overloaded_func_def(self, defn):
t = []
for f in defn.items:
f.is_overload = True
f.accept(self)
t.append(function_type(f))
defn.type = Overloaded(t)
defn.type.line = defn.line
if self.type:
self.type.methods[defn.name()] = defn
defn.info = self.type
示例6: is_simple_override
def is_simple_override(fdef: FuncDef, info: TypeInfo) -> bool:
"""Is function an override with the same type precision as the original?
Compare to the original method in the superclass of info.
"""
# If this is not an override, this can't be a simple override either.
# Generic inheritance is not currently supported, since we need to map
# type variables between types; in the future this restriction can be
# lifted.
if len(info.mro) <= 1:
return False
base = info.mro[1]
if base.type_vars != []:
return False
orig = base.get_method(fdef.name())
# Ignore the first argument (self) when determining type sameness.
# TODO overloads
newtype = cast(Callable, function_type(fdef))
newtype = replace_leading_arg_type(newtype, AnyType())
origtype = cast(Callable, function_type(orig))
origtype = replace_leading_arg_type(origtype, AnyType())
return is_same_type(newtype, origtype)
示例7: get_wrapper_sig
def get_wrapper_sig(self, act_as_func_def, is_dynamic):
"""Return the signature of the wrapper method.
The wrapper method signature has an additional type variable
argument (with type 'any'), and all type variables have been
erased.
"""
sig = function_type(act_as_func_def)
if is_dynamic:
return dynamic_sig(sig)
elif is_generic(act_as_func_def):
return erase_generic_types(sig) # FIX REFACTOR?
else:
return sig
示例8: get_target_sig
def get_target_sig(self, act_as_func_def, target_func_def, is_dynamic, is_wrapper_class):
"""Return the target method signature for a method wrapper."""
sig = function_type(target_func_def)
if is_wrapper_class:
if sig.is_generic() and is_dynamic:
sig = translate_function_type_vars_to_dynamic(sig)
return translate_type_vars_to_wrapped_object_vars(sig)
elif is_dynamic:
if sig.is_generic():
return translate_function_type_vars_to_dynamic(sig)
else:
return sig
else:
return sig
示例9: analyze_class_attribute_access
def analyze_class_attribute_access(
itype: Instance,
name: str,
context: Context,
is_lvalue: bool,
builtin_type: Callable[[str], Instance],
not_ready_callback: Callable[[str, Context], None],
msg: MessageBuilder,
) -> Type:
node = itype.type.get(name)
if not node:
if itype.type.fallback_to_any:
return AnyType()
return None
is_decorated = isinstance(node.node, Decorator)
is_method = is_decorated or isinstance(node.node, FuncDef)
if is_lvalue:
if is_method:
msg.cant_assign_to_method(context)
if isinstance(node.node, TypeInfo):
msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, context)
if itype.type.is_enum and not (is_lvalue or is_decorated or is_method):
return itype
t = node.type
if t:
if isinstance(t, PartialType):
return handle_partial_attribute_type(t, is_lvalue, msg, node.node)
is_classmethod = is_decorated and cast(Decorator, node.node).func.is_class
return add_class_tvars(t, itype.type, is_classmethod, builtin_type)
elif isinstance(node.node, Var):
not_ready_callback(name, context)
return AnyType()
if isinstance(node.node, TypeInfo):
return type_object_type(node.node, builtin_type)
if isinstance(node.node, MypyFile):
# Reference to a module object.
return builtin_type("builtins.module")
if is_decorated:
# TODO: Return type of decorated function. This is quick hack to work around #998.
return AnyType()
else:
return function_type(cast(FuncBase, node.node), builtin_type("builtins.function"))
示例10: make_overload_check
def make_overload_check(self, f, fixed_args, rest_args):
a = []
i = 0
if rest_args:
a.append(self.make_argument_count_check(f, len(fixed_args),
rest_args))
for t in function_type(f).arg_types:
if not isinstance(t, Any) and (t.repr or
isinstance(t, Callable)):
a.append(self.make_argument_check(
self.argument_ref(i, fixed_args, rest_args), t))
i += 1
if len(a) > 0:
return ' and '.join(a)
else:
return 'True'
示例11: transform_method_implementation
def transform_method_implementation(self, fdef, name):
"""Transform the implementation of a method (i.e. unwrapped)."""
args = fdef.args
arg_kinds = fdef.arg_kinds
typ = function_type(fdef)
init = fdef.init_expressions()
if fdef.name() == '__init__' and is_generic(fdef):
args, arg_kinds, init, typ = self.add_constructor_tvar_args(
fdef, typ, args, arg_kinds, init)
fdef2 = FuncDef(name, args, arg_kinds, init, fdef.body, typ)
fdef2.info = fdef.info
self.tf.prepend_generic_function_tvar_args(fdef2)
return fdef2
示例12: get_target_sig
def get_target_sig(self, act_as_func_def: FuncDef,
target_func_def: FuncDef,
is_dynamic: bool, is_wrapper_class: bool) -> Callable:
"""Return the target method signature for a method wrapper."""
sig = cast(Callable, function_type(target_func_def))
if is_wrapper_class:
if sig.is_generic() and is_dynamic:
sig = cast(Callable,
translate_function_type_vars_to_dynamic(sig))
return cast(Callable,
translate_type_vars_to_wrapped_object_vars(sig))
elif is_dynamic:
if sig.is_generic():
return cast(Callable,
translate_function_type_vars_to_dynamic(sig))
else:
return sig
else:
return sig
示例13: analyse_class_attribute_access
def analyse_class_attribute_access(itype: Instance, name: str,
context: Context, is_lvalue: bool,
msg: MessageBuilder) -> Type:
node = itype.type.get(name)
if node:
if is_lvalue and isinstance(node.node, FuncDef):
msg.fail(messages.CANNOT_ASSIGN_TO_METHOD, context)
if is_lvalue and isinstance(node.node, TypeInfo):
msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, context)
t = node.type
if t:
return add_class_tvars(t, itype.type)
elif isinstance(node.node, TypeInfo):
# TODO add second argument
return type_object_type(cast(TypeInfo, node.node), None)
else:
return function_type(cast(FuncBase, node.node))
else:
return None
示例14: get_call_sig
def get_call_sig(self, act_as_func_def: FuncDef,
current_class: TypeInfo, is_dynamic: bool,
is_wrapper_class: bool, is_override: bool) -> Callable:
"""Return the source signature in a wrapped call.
It has type variables replaced with 'Any', but as an
exception, type variables are intact in the return type in
generic wrapper classes. The exception allows omitting an
extra return value coercion, as the target return type and the
source return type will be the same.
"""
sig = cast(Callable, function_type(act_as_func_def))
if is_dynamic:
return dynamic_sig(sig)
elif is_generic(act_as_func_def):
call_sig = sig
# If this is an override wrapper, keep type variables
# intact. Otherwise replace them with dynamic to get
# desired coercions that check argument types.
if not is_override or is_wrapper_class:
call_sig = (cast(Callable, replace_type_vars(call_sig, False)))
else:
call_sig = cast(Callable, map_type_from_supertype(
call_sig, current_class, act_as_func_def.info))
if is_wrapper_class:
# Replace return type with the original return within
# wrapper classes to get rid of an unnecessary
# coercion. There will still be a coercion due to the
# extra coercion generated for generic wrapper
# classes. However, function generic type variables
# still need to be replaced, as the wrapper does not
# affect them.
ret = sig.ret_type
if is_dynamic:
ret = translate_function_type_vars_to_dynamic(ret)
call_sig = replace_ret_type(
call_sig, translate_type_vars_to_wrapper_vars(ret))
return call_sig
else:
return sig
示例15: prepend_generic_function_tvar_args
def prepend_generic_function_tvar_args(self, fdef):
"""Add implicit function type variable arguments if fdef is generic."""
sig = function_type(fdef)
tvars = sig.variables.items
if not fdef.type:
fdef.type = sig
tv = []
ntvars = len(tvars)
if fdef.is_method():
# For methods, add type variable arguments after the self arg.
for n in range(ntvars):
tv.append(Var(tvar_arg_name(-1 - n)))
fdef.type = add_arg_type_after_self(fdef.type, Any())
fdef.args = [fdef.args[0]] + tv + fdef.args[1:]
else:
# For ordinary functions, prepend type variable arguments.
for n in range(ntvars):
tv.append(Var(tvar_arg_name(-1 - n)))
fdef.type = prepend_arg_type(fdef.type, Any())
fdef.args = tv + fdef.args
fdef.init = [None] * ntvars + fdef.init