本文整理汇总了Python中tests.custom_hypothesis_support._parse_text函数的典型用法代码示例。如果您正苦于以下问题:Python _parse_text函数的具体用法?Python _parse_text怎么用?Python _parse_text使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_parse_text函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_subscript_del_ctx
def test_subscript_del_ctx(node):
"""Test visitor of Subscript node within a del statement."""
del_node = astroid.Delete()
del_node.postinit([node])
module, _ = cs._parse_text(del_node)
for subscript_node in module.nodes_of_class(astroid.Subscript):
assert subscript_node.inf_type.getValue() == type(None)
示例2: test_annotation_forward_ref_space
def test_annotation_forward_ref_space():
src = """
x: 'Some Class'
"""
module, inferer = cs._parse_text(src, reset=True)
for ann_node in module.nodes_of_class(astroid.AnnAssign):
assert isinstance(ann_node.inf_type, TypeFailAnnotationInvalid)
示例3: test_set_comprehension_reproduce_homogeneous
def test_set_comprehension_reproduce_homogeneous(iterable):
"""Test SetComp node visitor representing a comprehension expression reproducing a set of elements of
a homogeneous iterable."""
program = '{elt for elt in ' + repr(iterable) + '}'
module, _ = cs._parse_text(program)
setcomp_node = list(module.nodes_of_class(astroid.SetComp))[0]
assert setcomp_node.type_constraints.type == Set[setcomp_node.generators[0].iter.type_constraints.type.__args__[0]]
示例4: test_functiondef_annotated_simple_return
def test_functiondef_annotated_simple_return(functiondef_node):
"""Test whether type annotations are set properly for a FunctionDef node representing a function definition
with type annotations."""
arg_names = [arg.name for arg in functiondef_node.args.args]
assume(functiondef_node.name not in arg_names)
for arg in functiondef_node.args.args:
assume(arg_names.count(arg.name) == 1)
module, inferer = cs._parse_text(functiondef_node)
functiondef_node = next(module.nodes_of_class(astroid.FunctionDef))
# arguments and annotations are not changing, so test this once.
for i in range(len(functiondef_node.args.annotations)):
arg_name = functiondef_node.args.args[i].name
expected_type = inferer.type_constraints.resolve(functiondef_node.type_environment.lookup_in_env(arg_name)).getValue()
# need to do by name because annotations must be name nodes.
if isinstance(expected_type, _GenericAlias):
assert _gorg(expected_type).__name__ == functiondef_node.args.annotations[i].name
else:
assert expected_type.__name__ == functiondef_node.args.annotations[i].name
# test return type
return_node = functiondef_node.body[0].value
expected_rtype = inferer.type_constraints.resolve(functiondef_node.type_environment.lookup_in_env(return_node.name)).getValue()
if isinstance(expected_rtype, _GenericAlias):
assert _gorg(expected_rtype).__name__ == functiondef_node.returns.name
else:
assert expected_rtype.__name__ == functiondef_node.returns.name
示例5: test_subscript_slice
def test_subscript_slice():
program = '''
x = List[:]
'''
module, _ = cs._parse_text(program)
assign_node = next(module.nodes_of_class(astroid.Assign))
assert isinstance(assign_node.inf_type, TypeFail)
示例6: test_multi_starred
def test_multi_starred():
src = """
*a, b, *c = [1, 2, 3, 4, 5]
"""
ast_mod, ti = cs._parse_text(src, reset=True)
assign_node = next(ast_mod.nodes_of_class(astroid.Assign))
assert isinstance(assign_node.inf_type, TypeFailStarred)
示例7: test_userdefn_mro_diamond
def test_userdefn_mro_diamond(draw=False):
src = """
class A:
pass
class B(A):
def foo(self):
return 'a'
class C(A):
def foo(self):
return 0
class D(B,C):
pass
d = D()
x = d.foo() # this is a call to B.foo()
"""
ast_mod, ti = cs._parse_text(src, reset=True)
_, _, d, x = [ti.lookup_typevar(node, node.name) for node
in ast_mod.nodes_of_class(astroid.AssignName)]
assert ti.type_constraints.resolve(x).getValue() == str
if draw:
gen_graph_from_nodes(ti.type_constraints._nodes)
示例8: test_invalid_annassign_and_assign
def test_invalid_annassign_and_assign():
src = """
x: List[str] = [1, 2, 3]
"""
module, inferer = cs._parse_text(src, reset=True)
for ann_node in module.nodes_of_class(astroid.AnnAssign):
assert isinstance(ann_node.inf_type, TypeFailUnify)
示例9: test_set_name_unassigned
def test_set_name_unassigned(variable_name):
"""Test visitor for name nodes representing a single unassigned variable in module."""
program = variable_name
module, _ = cs._parse_text(program)
for name_node in module.nodes_of_class(astroid.Name):
name_type_var = name_node.frame().type_environment.lookup_in_env(name_node.name)
assert name_node.type_constraints.type == name_type_var
示例10: test_userdefn_inheritance_multilevel
def test_userdefn_inheritance_multilevel(draw=False):
src = """
class A:
pass
class B(A):
pass
class C(B):
pass
a = A()
b = B()
c = C()
"""
ast_mod, ti = cs._parse_text(src, reset=True)
tc = ti.type_constraints
a, b, c = [ti.lookup_typevar(node, node.name) for node
in ast_mod.nodes_of_class(astroid.AssignName)]
assert tc.unify(b, a).getValue() == ForwardRef('B')
assert tc.unify(c, b).getValue() == ForwardRef('C')
assert tc.unify(c, a).getValue() == ForwardRef('C')
assert isinstance(ti.type_constraints.unify(b, c), TypeFail)
if draw:
gen_graph_from_nodes(tc._nodes)
示例11: test_binop_reverse
def test_binop_reverse():
src = """
x = 3 * [1,2,3]
"""
ast_mod, ti = cs._parse_text(src, reset=True)
x = [ti.lookup_typevar(node, node.name) for node
in ast_mod.nodes_of_class(astroid.AssignName)][0]
assert ti.type_constraints.resolve(x).getValue() == List[int]
示例12: test_dict_comprehension_reproduce_heterogeneous
def test_dict_comprehension_reproduce_heterogeneous(node):
"""Test DictComp node visitor representing a comprehension expression reproducing the the key, value pairs
of a heterogeneous Dictionary."""
dictionary = node.as_string()
program = f"{{key: {dictionary}[key] for key in {dictionary}}}"
module, _ = cs._parse_text(program)
dictcomp_node = list(module.nodes_of_class(astroid.DictComp))[0]
assert dictcomp_node.inf_type.getValue() == dictcomp_node.generators[0].iter.inf_type.getValue()
示例13: test_multi_variable
def test_multi_variable():
src = """
lst = [1, 2, 3, 4, 5]
*a, b, *c = lst
"""
ast_mod, ti = cs._parse_text(src, reset=True)
assign_node = list(ast_mod.nodes_of_class(astroid.Assign))[1]
assert isinstance(assign_node.inf_type, TypeFailStarred)
示例14: test_homogeneous_lists
def test_homogeneous_lists(lst):
"""Test List nodes representing a list of values of the same primitive type."""
module, _ = cs._parse_text(lst)
list_node = list(module.nodes_of_class(astroid.List))[0]
if len(list_node.elts) == 0:
assert list_node.type_constraints.type == List[Any]
else:
cs._verify_type_setting(module, astroid.List, List[type(lst.elts[0].value)])
示例15: test_tuple_int_str
def test_tuple_int_str():
src = """
l = (1, "Hello")
"""
ast_mod, ti = cs._parse_text(src)
actual_set = tc_to_disjoint(ti.type_constraints)
expected_set = [{'~_TV0', 'typing.Tuple[int, str]'}]
compare_list_sets(actual_set, expected_set)