本文整理汇总了Python中nope.nodes.func函数的典型用法代码示例。如果您正苦于以下问题:Python func函数的具体用法?Python func怎么用?Python func使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了func函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: function_definitions_in_statement_lists_can_be_defined_out_of_order
def function_definitions_in_statement_lists_can_be_defined_out_of_order():
f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(nodes.ref("g"), []))
])
g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[])
_updated_bindings(nodes.module([f, g]))
示例2: attributes_assigned_in_init_can_be_used_in_methods_when_init_method_is_defined_after_other_method
def attributes_assigned_in_init_can_be_used_in_methods_when_init_method_is_defined_after_other_method():
init_func = nodes.func(
name="__init__",
args=nodes.args([nodes.arg("self_init")]),
body=[
nodes.assign(
[nodes.attr(nodes.ref("self_init"), "message")],
nodes.str_literal("Hello"),
type=nodes.ref("str"),
)
],
type=nodes.signature(
args=[nodes.signature_arg(nodes.ref("Self"))],
returns=nodes.ref("none")
),
)
node = nodes.class_("User", [
nodes.func(
name="g",
args=nodes.args([nodes.arg("self_g")]),
body=[nodes.ret(nodes.attr(nodes.ref("self_g"), "message"))],
type=nodes.signature(
args=[nodes.signature_arg(nodes.ref("Self"))],
returns=nodes.ref("str")
),
),
init_func,
])
_infer_class_type(node, ["__init__", "g"], [(init_func, ["self_init"])])
示例3: init_method_cannot_call_other_methods
def init_method_cannot_call_other_methods():
node = nodes.class_("User", [
nodes.func(
name="__init__",
args=nodes.args([nodes.arg("self_init")]),
body=[
nodes.assign([nodes.ref("x")], nodes.call(nodes.attr(nodes.ref("self_init"), "g"), []))
],
type=nodes.signature(
args=[nodes.signature_arg(nodes.ref("Self"))],
returns=nodes.ref("none")
),
),
nodes.func(
name="g",
args=nodes.args([nodes.arg("self_g")]),
body=[],
type=nodes.signature(
args=[nodes.signature_arg(nodes.ref("Self"))],
returns=nodes.ref("none")
),
),
])
try:
_infer_class_type(node, ["__init__", "g"])
assert False, "Expected error"
except errors.InitMethodCannotGetSelfAttributes as error:
assert_equal("__init__ methods cannot get attributes of self", str(error))
示例4: method_can_call_method_on_same_instance_defined_later_in_body
def method_can_call_method_on_same_instance_defined_later_in_body():
node = nodes.class_("User", [
nodes.func(
name="f",
args=nodes.args([nodes.arg("self_f")]),
body=[
nodes.ret(nodes.call(nodes.attr(nodes.ref("self_f"), "g"), []))
],
type=nodes.signature(
args=[nodes.signature_arg(nodes.ref("Self"))],
returns=nodes.ref("none")
),
),
nodes.func(
name="g",
args=nodes.args([nodes.arg("self_g")]),
body=[
nodes.ret(nodes.call(nodes.attr(nodes.ref("self_g"), "f"), []))
],
type=nodes.signature(
args=[nodes.signature_arg(nodes.ref("Self"))],
returns=nodes.ref("none")
),
)
])
_infer_class_type(node, ["f", "g"])
示例5: function_definitions_in_statement_lists_can_be_mutually_recursive
def function_definitions_in_statement_lists_can_be_mutually_recursive():
f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(nodes.ref("g"), []))
])
g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(nodes.ref("f"), []))
])
_update_context([f, g])
示例6: function_definitions_can_be_mutually_recursive
def function_definitions_can_be_mutually_recursive():
f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(nodes.ref("g"), []))
])
g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(nodes.ref("f"), []))
])
_updated_bindings(nodes.module([f, g]))
示例7: method_can_reference_later_function_if_class_is_not_used_in_the_interim
def method_can_reference_later_function_if_class_is_not_used_in_the_interim():
g_ref = nodes.ref("g")
f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(g_ref, []))
])
g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[])
_updated_bindings(nodes.module([
nodes.class_("a", [f]),
g,
]))
示例8: names_in_function_are_not_declared_in_outer_scope
def names_in_function_are_not_declared_in_outer_scope():
node = nodes.func("f", nodes.arguments([]), [
nodes.assign([nodes.ref("x")], nodes.none())
], type=None)
declarations = find_declarations(node)
assert not declarations.is_declared("x")
示例9: exception_handler_targets_cannot_be_accessed_from_nested_function
def exception_handler_targets_cannot_be_accessed_from_nested_function():
target_node = nodes.ref("error")
ref_node = nodes.ref("error")
body = [nodes.ret(ref_node)]
func_node = nodes.func("f", nodes.arguments([]), body, type=None)
try_node = nodes.try_(
[],
handlers=[
nodes.except_(nodes.none(), target_node, [func_node])
],
)
declaration = name_declaration.ExceptionHandlerTargetNode("error")
references = References([
(target_node, declaration),
(ref_node, declaration),
(func_node, name_declaration.VariableDeclarationNode("f")),
])
try:
_updated_bindings(try_node, references=references)
assert False, "Expected error"
except errors.UnboundLocalError as error:
assert_equal(ref_node, error.node)
assert_is("error", error.name)
示例10: class_cannot_be_referenced_if_method_dependencies_are_not_bound
def class_cannot_be_referenced_if_method_dependencies_are_not_bound():
g_ref = nodes.ref("g")
f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(g_ref, []))
])
g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[])
try:
_updated_bindings(nodes.module([
nodes.class_("a", [f]),
nodes.expression_statement(nodes.ref("a")),
g,
]))
assert False, "Expected error"
except errors.UnboundLocalError as error:
assert_equal(g_ref, error.node)
示例11: can_infer_type_of_function_with_explicit_signature_of_aliased_function_type
def can_infer_type_of_function_with_explicit_signature_of_aliased_function_type():
args = nodes.arguments([])
node = nodes.func("f", args=args, body=[], type=nodes.ref("Action"))
type_bindings = {
"Action": types.meta_type(types.func([], types.none_type))
}
assert_equal(types.func([], types.none_type), _infer_func_type(node, type_bindings))
示例12: attributes_assigned_in_init_can_be_used_outside_of_class
def attributes_assigned_in_init_can_be_used_outside_of_class():
init_func = nodes.func(
name="__init__",
args=nodes.args([nodes.arg("self_init")]),
body=[
nodes.assign(
[nodes.attr(nodes.ref("self_init"), "message")],
nodes.str_literal("Hello"),
type=nodes.ref("str"),
)
],
type=nodes.signature(
args=[nodes.signature_arg(nodes.ref("Self"))],
returns=nodes.ref("none")
)
)
class_node = nodes.class_("User", [init_func])
node = [
class_node,
nodes.assign([nodes.ref("x")], nodes.attr(nodes.call(nodes.ref("User"), []), "message")),
]
context = _update_context(node, declared_names_in_node=NodeDict([
(class_node, ["Self", "__init__"]),
(init_func, ["self_init"]),
]))
assert_equal(types.str_type, context.lookup_name("x"))
示例13: function_cannot_be_referenced_before_definition_of_dependencies
def function_cannot_be_referenced_before_definition_of_dependencies():
g_ref = nodes.ref("g")
f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
nodes.ret(nodes.call(g_ref, []))
])
g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[])
try:
_updated_bindings(nodes.module([
f,
nodes.expression_statement(nodes.ref("f")),
g,
]))
assert False, "Expected error"
except errors.UnboundLocalError as error:
assert_equal(g_ref, error.node)
示例14: declarations_in_function_include_declarations_in_body
def declarations_in_function_include_declarations_in_body():
node = nodes.func("f", nodes.arguments([]), [
nodes.assign([nodes.ref("x")], nodes.none())
], type=None)
declarations = _declarations_in(node)
assert isinstance(declarations.declaration("x"), name_declaration.VariableDeclarationNode)
assert not declarations.is_declared("f")
示例15: break_is_not_valid_in_function_in_while_loop_body
def break_is_not_valid_in_function_in_while_loop_body():
break_node = nodes.break_()
func_node = nodes.func("f", nodes.args([]), [break_node], type=None)
node = nodes.while_(nodes.bool_literal(True), [func_node], [])
error = assert_raises(errors.TypeCheckError,
lambda: check_loop_control(node, False))
assert_equal(break_node, error.node)
assert_equal("'break' outside loop", str(error))