本文整理汇总了Python中astroid.Call方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Call方法的具体用法?Python astroid.Call怎么用?Python astroid.Call使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Call方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: attr_attributes_transform
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def attr_attributes_transform(node):
"""Given that the ClassNode has an attr decorator,
rewrite class attributes as instance attributes
"""
# Astroid can't infer this attribute properly
# Prevents https://github.com/PyCQA/pylint/issues/1884
node.locals["__attrs_attrs__"] = [astroid.Unknown(parent=node.body)]
for cdefbodynode in node.body:
if not isinstance(cdefbodynode, astroid.Assign):
continue
if isinstance(cdefbodynode.value, astroid.Call):
if cdefbodynode.value.func.as_string() != ATTR_IB:
continue
else:
continue
for target in cdefbodynode.targets:
rhs_node = astroid.Unknown(
lineno=cdefbodynode.lineno,
col_offset=cdefbodynode.col_offset,
parent=cdefbodynode
)
node.locals[target.name] = [rhs_node]
示例2: _looks_like_lru_cache
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def _looks_like_lru_cache(node):
"""Check if the given function node is decorated with lru_cache."""
if not node.decorators:
return False
for decorator in node.decorators.nodes:
if not isinstance(decorator, astroid.Call):
continue
func = helpers.safe_infer(decorator.func)
if func in (None, astroid.Uninferable):
continue
if isinstance(func, astroid.FunctionDef) and func.qname() == LRU_CACHE:
return True
return False
示例3: visit_starred
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def visit_starred(self, node):
"""Check that a Starred expression is used in an assignment target."""
if isinstance(node.parent, astroid.Call):
# f(*args) is converted to Call(args=[Starred]), so ignore
# them for this check.
return
if PY35 and isinstance(node.parent,
(astroid.List, astroid.Tuple,
astroid.Set, astroid.Dict)):
# PEP 448 unpacking.
return
stmt = node.statement()
if not isinstance(stmt, astroid.Assign):
return
if stmt.value is node or stmt.value.parent_of(node):
self.add_message('star-needs-assignment-target', node=node)
示例4: visit_call
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def visit_call(self, node):
"""visit a Call node -> check if this is not a blacklisted builtin
call and check for * or ** use
"""
if isinstance(node.func, astroid.Name):
name = node.func.name
# ignore the name if it's not a builtin (i.e. not defined in the
# locals nor globals scope)
if not (name in node.frame() or
name in node.root()):
if name == 'exec':
self.add_message('exec-used', node=node)
elif name == 'reversed':
self._check_reversed(node)
elif name == 'eval':
self.add_message('eval-used', node=node)
示例5: _called_in_methods
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def _called_in_methods(func, klass, methods):
""" Check if the func was called in any of the given methods,
belonging to the *klass*. Returns True if so, False otherwise.
"""
if not isinstance(func, astroid.FunctionDef):
return False
for method in methods:
try:
infered = klass.getattr(method)
except astroid.NotFoundError:
continue
for infer_method in infered:
for call in infer_method.nodes_of_class(astroid.Call):
try:
bound = next(call.func.infer())
except (astroid.InferenceError, StopIteration):
continue
if not isinstance(bound, astroid.BoundMethod):
continue
func_obj = bound._proxied
if isinstance(func_obj, astroid.UnboundMethod):
func_obj = func_obj._proxied
if func_obj.name == func.name:
return True
return False
示例6: test_imported_module_function
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_imported_module_function():
src = """
import mod
x = mod.func1(0, 1)
y = mod.func1(0, 1) + 1
undefined_mod.func2(0, 1)
"""
ast_mod, ti = cs._parse_text(src, reset=True)
for call_node in ast_mod.nodes_of_class(astroid.Call):
if call_node.func.attrname == 'func1':
assert call_node.inf_type.getValue() == Any
if call_node.func.attrname == 'func2':
assert isinstance(call_node.inf_type, TypeFail)
for assgn_node in ast_mod.nodes_of_class(astroid.Assign):
if assgn_node.targets[0].name == 'x':
x = ti.lookup_typevar(assgn_node, 'x')
assert ti.type_constraints.resolve(x).getValue() == Any
if assgn_node.targets[0].name == 'y':
y = ti.lookup_typevar(assgn_node, 'y')
assert ti.type_constraints.resolve(y).getValue() == int
示例7: test_from_import_function
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_from_import_function():
src = """
from mod import func
x = func(0, 1)
y = func(0, 1) + 1
undefined_func(0, 1)
"""
ast_mod, ti = cs._parse_text(src, reset=True)
for call_node in ast_mod.nodes_of_class(astroid.Call):
if call_node.func.name == 'func1':
assert call_node.inf_type.getValue() == Any
if call_node.func.name == 'func2':
assert isinstance(call_node.inf_type, TypeFail)
for assgn_node in ast_mod.nodes_of_class(astroid.Assign):
if assgn_node.targets[0].name == 'x':
x = ti.lookup_typevar(assgn_node, 'x')
assert ti.type_constraints.resolve(x).getValue() == Any
if assgn_node.targets[0].name == 'y':
y = ti.lookup_typevar(assgn_node, 'y')
assert ti.type_constraints.resolve(y).getValue() == int
示例8: test_attribute_unification_fail
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_attribute_unification_fail():
skip('This case is not supported yet')
program = \
'''
class A:
def __init__(self):
self.name = 'abc'
def f(x):
x.name = 3
a = A()
f(a)
f(A)
'''
module, ti = cs._parse_text(program, reset=True)
call_node1, call_node2 = module.nodes_of_class(astroid.Call)[1:]
assert isinstance(call_node.inf_type1, TypeFail)
assert isinstance(call_node.inf_type2, TypeFail)
示例9: test_user_defined_annotated_call_wrong_arguments_type
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_user_defined_annotated_call_wrong_arguments_type():
""" User tries to call an annotated user-defined function on the wrongly-typed arguments.
"""
program = f'def add_3(num1: int, num2: int, num3: int) -> int:\n' \
f' return num1 + num2 + num3\n' \
f'\n' \
f'add_3(1, "bob", 1.0)\n'
try:
module, inferer = cs._parse_text(program)
except:
skip()
call_node = list(module.nodes_of_class(astroid.Call))[0]
expected_msg = f'In the Call node in line 4, there was an error in calling the annotated function "add_3":\n' \
f'in parameter (2), the annotated type is int but was given an object of type str.\n' \
f'in parameter (3), the annotated type is int but was given an object of type float.\n'
assert call_node.inf_type.getValue() == expected_msg
示例10: test_conflicting_inferred_type_variable
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_conflicting_inferred_type_variable():
""" User calls two functions on an object, which contradicts the inferred type of the variable.
"""
program = '''
def return_num(num: int) -> int:
return num
def return_str(str: str) -> str:
return str
def f(x):
return_num(x)
return_str(x)
'''
try:
module, inferer = cs._parse_text(program)
except:
skip()
call_node = list(module.nodes_of_class(astroid.Call))[1]
expected_msg = f'In the Call node in line 8, there was an error in calling the annotated function "return_str":\n' \
f'in parameter (1), the annotated type is str but was given an object of inferred type int.'
# TODO: test case redundant because recursive..?
assert call_node.inf_type.getValue() == expected_msg
示例11: test_non_annotated_function_call_bad_arguments
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_non_annotated_function_call_bad_arguments():
""" User tries to call a non-annotated function on arguments of the wrong type.
"""
skip('Skipping this test until error messages are fixed')
program = f'def add_num(num1, num2):\n' \
f' return num1 + num2\n' \
f'\n' \
f'add_num("bob", 1.0)\n'
try:
module, inferer = cs._parse_text(program)
except:
skip()
call_node = next(module.nodes_of_class(astroid.Call))
# TODO: This error is flawed because the unification error occurs for both arguments due to our current implementation,
# which "chooses" the first valid function type from TypeStore.
# Should we fix this implementation first or save it for later and hard-code the correct error message for now?
expected_msg = f'In the Call node in line 4, there was an error in calling the function "add_num":\n' \
f'in parameter (1), the function was expecting an object of inferred type ' \
f'int but was given an object of type str.\n' \
f'in parameter (2), the function was expecting an object of inferred type ' \
f'int but was given an object of type float.\n'
# TODO: should we use the term inferred?
assert call_node.inf_type.getValue() == expected_msg
示例12: test_magic_call
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_magic_call():
program = '''
class A:
def __init__(self):
self.attr = 0
def __call__(self):
return self.attr
a = A()
a()
a.__call__()
'''
module, inferer = cs._parse_text(program, reset=True)
for call_node in list(module.nodes_of_class(astroid.Call))[1:]:
assert call_node.inf_type.getValue() == int
示例13: test_magic_call_wrong_args
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def test_magic_call_wrong_args():
program = '''
class A:
def __init__(self):
self.attr = 0
def __call__(self, x: int):
return self.attr + x
a = A()
a()
a.__call__()
'''
module, inferer = cs._parse_text(program, reset=True)
for call_node in list(module.nodes_of_class(astroid.Call))[1:]:
assert isinstance(call_node.inf_type, TypeFailFunction)
示例14: _get_child_disallowed_global_var_nodes
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def _get_child_disallowed_global_var_nodes(node):
"""Return a list of all top-level Name or AssignName nodes for a given
global, non-constant variable.
TODO: use the configured NamingStyle instead of hard-coded SnakeCaseStyle
for the CONST_NAME_RGX value.
"""
node_list = []
if ((isinstance(node, (astroid.AssignName, astroid.Name)) and not isinstance(node.parent, astroid.Call)) and
not re.match(UpperCaseStyle.CONST_NAME_RGX, node.name) and
node.scope() is node.root()):
return [node]
for child_node in node.get_children():
node_list += _get_child_disallowed_global_var_nodes(child_node)
return node_list
示例15: attr_attributes_transform
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Call [as 别名]
def attr_attributes_transform(node):
"""Given that the ClassNode has an attr decorator,
rewrite class attributes as instance attributes
"""
# Astroid can't infer this attribute properly
# Prevents https://github.com/PyCQA/pylint/issues/1884
node.locals["__attrs_attrs__"] = [astroid.Unknown(parent=node)]
for cdefbodynode in node.body:
if not isinstance(cdefbodynode, astroid.Assign):
continue
if isinstance(cdefbodynode.value, astroid.Call):
if cdefbodynode.value.func.as_string() not in ATTRIB_NAMES:
continue
else:
continue
for target in cdefbodynode.targets:
rhs_node = astroid.Unknown(
lineno=cdefbodynode.lineno,
col_offset=cdefbodynode.col_offset,
parent=cdefbodynode,
)
node.locals[target.name] = [rhs_node]