本文整理汇总了Python中nope.nodes.assign函数的典型用法代码示例。如果您正苦于以下问题:Python assign函数的具体用法?Python assign怎么用?Python assign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assign函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: 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"))
示例2: error_is_raised_if_all_is_redeclared
def error_is_raised_if_all_is_redeclared():
try:
all_node = nodes.assign(["__all__"], nodes.list_literal([]))
second_all_node = nodes.assign(["__all__"], nodes.list_literal([]))
_exported_names(nodes.module([all_node, second_all_node]))
assert False, "Expected error"
except errors.AllAssignmentError as error:
assert_equal(second_all_node, error.node)
assert_equal("__all__ cannot be redeclared", str(error))
示例3: module_exports_default_to_values_without_leading_underscore_if_all_is_not_specified
def module_exports_default_to_values_without_leading_underscore_if_all_is_not_specified():
module_node = nodes.module([
nodes.assign(["x"], nodes.str_literal("one")),
nodes.assign(["_y"], nodes.str_literal("two")),
nodes.assign(["z"], nodes.int_literal(3)),
])
module, type_lookup = _check(LocalModule(None, module_node))
assert_equal(types.str_type, module.attrs.type_of("x"))
assert_equal(None, module.attrs.get("_y"))
assert_equal(types.int_type, module.attrs.type_of("z"))
示例4: module_exports_are_specified_using_all
def module_exports_are_specified_using_all():
module_node = nodes.module([
nodes.assign(["__all__"], nodes.list_literal([nodes.str_literal("x"), nodes.str_literal("z")])),
nodes.assign(["x"], nodes.str_literal("one")),
nodes.assign(["y"], nodes.str_literal("two")),
nodes.assign(["z"], nodes.int_literal(3)),
])
module, type_lookup = _check(LocalModule(None, module_node))
assert_equal(types.str_type, module.attrs.type_of("x"))
assert_equal(None, module.attrs.get("y"))
assert_equal(types.int_type, module.attrs.type_of("z"))
示例5: 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"])])
示例6: 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))
示例7: can_assign_value_of_subtype_to_variable
def can_assign_value_of_subtype_to_variable():
target_node = nodes.ref("x")
node = nodes.assign([target_node], nodes.int_literal(1))
type_bindings = {"x": types.object_type}
context = update_context(node, type_bindings=type_bindings)
assert_equal(types.object_type, context.lookup_name("x"))
示例8: names_in_class_are_not_declared_in_outer_scope
def names_in_class_are_not_declared_in_outer_scope():
node = nodes.class_("User", [
nodes.assign([nodes.ref("x")], nodes.none())
])
declarations = find_declarations(node)
assert not declarations.is_declared("x")
示例9: 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")
示例10: 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")
示例11: test_transform_setitem_subscript
def test_transform_setitem_subscript(self):
_assert_transform(
nodes.assign([nodes.subscript(nodes.ref("x"), nodes.ref("y"))], nodes.ref("z")),
"""
var __nope_u_tmp0 = z
x.__setitem__(y, __nope_u_tmp0)
"""
)
示例12: check_generates_type_lookup_for_all_expressions
def check_generates_type_lookup_for_all_expressions():
int_ref_node = nodes.ref("a")
int_node = nodes.int_literal(3)
str_node = nodes.str_literal("Hello")
module_node = nodes.module([
nodes.assign(["a"], int_node),
nodes.func("f", nodes.args([]), [
nodes.assign("b", int_ref_node),
nodes.assign("c", str_node),
], type=None),
])
module, type_lookup = _check(LocalModule(None, module_node))
assert_equal(types.int_type, type_lookup.type_of(int_node))
assert_equal(types.int_type, type_lookup.type_of(int_ref_node))
assert_equal(types.str_type, type_lookup.type_of(str_node))
示例13: only_values_that_are_definitely_bound_are_exported
def only_values_that_are_definitely_bound_are_exported():
module_node = nodes.module([
nodes.if_(
nodes.bool_literal(True),
[
nodes.assign(["x"], nodes.str_literal("one")),
nodes.assign(["y"], nodes.str_literal("two")),
],
[
nodes.assign(["y"], nodes.str_literal("three")),
]
)
])
module, type_lookup = _check(LocalModule(None, module_node))
assert_equal(None, module.attrs.get("x"))
assert_equal(types.str_type, module.attrs.type_of("y"))
示例14: attributes_with_function_type_defined_in_class_definition_body_are_not_present_on_meta_type
def attributes_with_function_type_defined_in_class_definition_body_are_not_present_on_meta_type():
node = nodes.class_("User", [
nodes.assign([nodes.ref("is_person")], nodes.ref("true_func")),
])
meta_type = _infer_meta_type(node, ["is_person"], type_bindings={
"true_func": types.func([types.object_type], types.none_type)
})
assert "is_person" not in meta_type.attrs
示例15: declarations_in_class_include_declarations_in_body
def declarations_in_class_include_declarations_in_body():
node = nodes.class_("User", [
nodes.assign([nodes.ref("x")], nodes.none())
])
declarations = _declarations_in(node)
assert isinstance(declarations.declaration("x"), name_declaration.VariableDeclarationNode)
assert not declarations.is_declared("User")