本文整理汇总了Python中astroid.FunctionDef方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.FunctionDef方法的具体用法?Python astroid.FunctionDef怎么用?Python astroid.FunctionDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.FunctionDef方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _looks_like_lru_cache
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [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
示例2: visit_raise
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def visit_raise(self, node):
func_node = node.frame()
if not isinstance(func_node, astroid.FunctionDef):
return
expected_excs = utils.possible_exc_types(node)
if not expected_excs:
return
if not func_node.doc:
# If this is a property setter,
# the property should have the docstring instead.
property_ = utils.get_setters_property(func_node)
if property_:
func_node = property_
doc = utils.docstringify(func_node.doc)
if not doc.is_valid():
if doc.doc:
self._handle_no_raise_doc(expected_excs, func_node)
return
found_excs = doc.exceptions()
missing_excs = expected_excs - found_excs
self._add_raise_message(missing_excs, func_node)
示例3: _check_misplaced_bare_raise
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def _check_misplaced_bare_raise(self, node):
# Filter out if it's present in __exit__.
scope = node.scope()
if (isinstance(scope, astroid.FunctionDef)
and scope.is_method()
and scope.name == '__exit__'):
return
current = node
# Stop when a new scope is generated or when the raise
# statement is found inside a TryFinally.
ignores = (astroid.ExceptHandler, astroid.FunctionDef, astroid.TryFinally)
while current and not isinstance(current.parent, ignores):
current = current.parent
expected = (astroid.ExceptHandler,)
if not current or not isinstance(current.parent, expected):
self.add_message('misplaced-bare-raise', node=node)
示例4: _check_redefined_argument_from_local
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def _check_redefined_argument_from_local(self, name_node):
if self._dummy_rgx and self._dummy_rgx.match(name_node.name):
return
if not name_node.lineno:
# Unknown position, maybe it is a manually built AST?
return
scope = name_node.scope()
if not isinstance(scope, astroid.FunctionDef):
return
for defined_argument in scope.args.nodes_of_class(astroid.AssignName):
if defined_argument.name == name_node.name:
self.add_message('redefined-argument-from-local',
node=name_node,
args=(name_node.name, ))
示例5: _check_consistent_returns
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def _check_consistent_returns(self, node):
"""Check that all return statements inside a function are consistent.
Return statements are consistent if:
- all returns are explicit and if there is no implicit return;
- all returns are empty and if there is, possibly, an implicit return.
Args:
node (astroid.FunctionDef): the function holding the return statements.
"""
# explicit return statements are those with a not None value
explicit_returns = [_node for _node in self._return_nodes[node.name]
if _node.value is not None]
if not explicit_returns:
return
if (len(explicit_returns) == len(self._return_nodes[node.name])
and self._is_node_return_ended(node)):
return
self.add_message('inconsistent-return-statements', node=node)
示例6: _check_nonlocal_without_binding
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (astroid.ClassDef, astroid.FunctionDef)):
self.add_message('nonlocal-without-binding', args=(name, ),
node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, astroid.FunctionDef):
self.add_message('nonlocal-without-binding', args=(name, ), node=node)
示例7: _check_in_loop
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def _check_in_loop(self, node, node_name):
"""check that a node is inside a for or while loop"""
_node = node.parent
while _node:
if isinstance(_node, (astroid.For, astroid.While)):
if node not in _node.orelse:
return
if isinstance(_node, (astroid.ClassDef, astroid.FunctionDef)):
break
if (isinstance(_node, astroid.TryFinally)
and node in _node.finalbody
and isinstance(node, astroid.Continue)):
self.add_message('continue-in-finally', node=node)
_node = _node.parent
self.add_message('not-in-loop', node=node, args=node_name)
示例8: visit_functiondef
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def visit_functiondef(self, node):
if self.config.no_docstring_rgx.match(node.name) is None:
ftype = 'method' if node.is_method() else 'function'
if node.decorators and self._is_setter_or_deleter(node):
return
if isinstance(node.parent.frame(), astroid.ClassDef):
overridden = False
confidence = (interfaces.INFERENCE if utils.has_known_bases(node.parent.frame())
else interfaces.INFERENCE_FAILURE)
# check if node is from a method overridden by its ancestor
for ancestor in node.parent.frame().ancestors():
if node.name in ancestor and \
isinstance(ancestor[node.name], astroid.FunctionDef):
overridden = True
break
self._check_docstring(ftype, node,
report_missing=not overridden,
confidence=confidence)
else:
self._check_docstring(ftype, node)
示例9: visit_call
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def visit_call(self, node):
"""Visit a Call node."""
try:
for inferred in node.func.infer():
if inferred is astroid.Uninferable:
continue
if inferred.root().name == OPEN_MODULE:
if getattr(node.func, 'name', None) in OPEN_FILES:
self._check_open_mode(node)
if inferred.root().name == UNITTEST_CASE:
self._check_redundant_assert(node, inferred)
if isinstance(inferred, astroid.ClassDef) and inferred.qname() == THREADING_THREAD:
self._check_bad_thread_instantiation(node)
if isinstance(inferred, astroid.FunctionDef) and inferred.qname() == COPY_COPY:
self._check_shallow_copy_environ(node)
self._check_deprecated_method(node, inferred)
except astroid.InferenceError:
return
示例10: visit_classdef
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def visit_classdef(self, node):
def _metaclass_name(metaclass):
if isinstance(metaclass, (astroid.ClassDef, astroid.FunctionDef)):
return metaclass.name
return metaclass.as_string()
metaclass = node.declared_metaclass()
if not metaclass:
return
if isinstance(metaclass, astroid.FunctionDef):
# Try to infer the result.
metaclass = _infer_from_metaclass_constructor(node, metaclass)
if not metaclass:
# Don't do anything if we cannot infer the result.
return
if isinstance(metaclass, astroid.ClassDef):
if _is_invalid_metaclass(metaclass):
self.add_message('invalid-metaclass', node=node,
args=(_metaclass_name(metaclass), ))
else:
self.add_message('invalid-metaclass', node=node,
args=(_metaclass_name(metaclass), ))
示例11: _called_in_methods
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [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
示例12: functiondef_node
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def functiondef_node(draw, name=None, annotated=False, returns=False):
name = name or draw(valid_identifier())
args = draw(arguments_node(annotated))
body = []
returns_node = astroid.Return()
arg_node, arg_type_node = draw(hs.sampled_from(list(zip(args.args, args.annotations))))
if returns:
returns_node.postinit(arg_node)
else:
returns_node.postinit(const_node(None))
body.append(returns_node)
node = astroid.FunctionDef(name=name)
node.parent = astroid.Module('Default', None)
node.postinit(
args,
body,
None,
arg_type_node
)
return node
示例13: test_functiondef_annotated_simple_return
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
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
示例14: test_function_def_args_simple_return
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def test_function_def_args_simple_return(function_name, arguments):
"""Test FunctionDef node visitors representing function definitions with paramater(s):
return one of its arguments."""
for argument in arguments:
program = _parse_to_function(function_name, arguments, argument)
module, inferer = cs._parse_text(program)
# get the functionDef node - there is only one in this test case.
function_def_node = next(module.nodes_of_class(astroid.FunctionDef))
expected_arg_type_vars = [function_def_node.type_environment.lookup_in_env(argument) for argument in arguments]
expected_arg_types = [inferer.type_constraints.resolve(type_var).getValue() for type_var in expected_arg_type_vars]
function_type_var = module.type_environment.lookup_in_env(function_name)
function_type = inferer.type_constraints.resolve(function_type_var).getValue()
actual_arg_types, actual_return_type = types_in_callable(inferer, function_type)
return_type_var = function_def_node.type_environment.lookup_in_env(argument)
expected_return_type = inferer.type_constraints.resolve(return_type_var).getValue()
assert Callable[actual_arg_types, actual_return_type] == Callable[expected_arg_types, expected_return_type]
示例15: _multiprocessing_transform
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import FunctionDef [as 别名]
def _multiprocessing_transform():
module = astroid.parse('''
from multiprocessing.managers import SyncManager
def Manager():
return SyncManager()
''')
if not PY34:
return module
# On Python 3.4, multiprocessing uses a getattr lookup inside contexts,
# in order to get the attributes they need. Since it's extremely
# dynamic, we use this approach to fake it.
node = astroid.parse('''
from multiprocessing.context import DefaultContext, BaseContext
default = DefaultContext()
base = BaseContext()
''')
try:
context = next(node['default'].infer())
base = next(node['base'].infer())
except exceptions.InferenceError:
return module
for node in (context, base):
for key, value in node.locals.items():
if key.startswith("_"):
continue
value = value[0]
if isinstance(value, astroid.FunctionDef):
# We need to rebound this, since otherwise
# it will have an extra argument (self).
value = astroid.BoundMethod(value, node)
module[key] = value
return module