本文整理匯總了Python中astroid.InferenceError方法的典型用法代碼示例。如果您正苦於以下問題:Python astroid.InferenceError方法的具體用法?Python astroid.InferenceError怎麽用?Python astroid.InferenceError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類astroid
的用法示例。
在下文中一共展示了astroid.InferenceError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _nose_tools_functions
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def _nose_tools_functions():
"""Get an iterator of names and bound methods."""
module = _BUILDER.string_build(textwrap.dedent('''
import unittest
class Test(unittest.TestCase):
pass
a = Test()
'''))
try:
case = next(module['a'].infer())
except astroid.InferenceError:
return
for method in case.methods():
if method.name.startswith('assert') and '_' not in method.name:
pep8_name = _pep8(method.name)
yield pep8_name, astroid.BoundMethod(method, case)
if method.name == 'assertEqual':
# nose also exports assert_equals.
yield 'assert_equals', astroid.BoundMethod(method, case)
示例2: visit_raise
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def visit_raise(self, node):
if node.exc is None:
self._check_misplaced_bare_raise(node)
return
if PY3K and node.cause:
self._check_bad_exception_context(node)
expr = node.exc
try:
inferred_value = next(expr.infer())
except astroid.InferenceError:
inferred_value = None
ExceptionRaiseRefVisitor(self, node).visit(expr)
if inferred_value:
ExceptionRaiseLeafVisitor(self, node).visit(inferred_value)
示例3: safe_infer
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def safe_infer(node, context=None):
"""Return the inferred value for the given node.
Return None if inference failed or if there is some ambiguity (more than
one node has been inferred).
"""
try:
inferit = node.infer(context=context)
value = next(inferit)
except astroid.InferenceError:
return None
try:
next(inferit)
return None # None if there is ambiguity on the inferred node
except astroid.InferenceError:
return None # there is some kind of ambiguity
except StopIteration:
return value
示例4: node_type
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def node_type(node):
"""Return the inferred type for `node`
If there is more than one possible type, or if inferred type is YES or None,
return None
"""
# check there is only one possible type for the assign node. Else we
# don't handle it for now
types = set()
try:
for var_type in node.infer():
if var_type == astroid.Uninferable or is_none(var_type):
continue
types.add(var_type)
if len(types) > 1:
return None
except astroid.InferenceError:
return None
return types.pop() if types else None
示例5: is_enum_class
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def is_enum_class(node):
"""Check if a class definition defines an Enum class.
:param node: The class node to check.
:type node: astroid.ClassDef
:returns: True if the given node represents an Enum class. False otherwise.
:rtype: bool
"""
for base in node.bases:
try:
inferred_bases = base.inferred()
except astroid.InferenceError:
continue
for ancestor in inferred_bases:
if not isinstance(ancestor, astroid.ClassDef):
continue
if ancestor.name == 'Enum' and ancestor.root().name == 'enum':
return True
return False
示例6: visit_attribute
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def visit_attribute(self, node):
"""Look for removed attributes"""
if node.attrname == 'xreadlines':
self.add_message('xreadlines-attribute', node=node)
return
exception_message = 'message'
try:
for inferred in node.expr.infer():
if (isinstance(inferred, astroid.Instance) and
utils.inherit_from_std_ex(inferred)):
if node.attrname == exception_message:
# Exceptions with .message clearly defined are an exception
if exception_message in inferred.instance_attrs:
continue
self.add_message('exception-message-attribute', node=node)
if isinstance(inferred, astroid.Module):
self._warn_if_deprecated(node, inferred.name, {node.attrname},
report_on_modules=False)
except astroid.InferenceError:
return
示例7: visit_raise
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def visit_raise(self, node):
"""Visit a raise statement and check for raising
strings or old-raise-syntax.
"""
if six.PY2:
if (node.exc is not None and
node.inst is not None):
self.add_message('old-raise-syntax', node=node)
# Ignore empty raise.
if node.exc is None:
return
expr = node.exc
if self._check_raise_value(node, expr):
return
try:
value = next(astroid.unpack_infer(expr))
except astroid.InferenceError:
return
self._check_raise_value(node, value)
示例8: visit_call
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [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
示例9: _has_data_descriptor
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def _has_data_descriptor(cls, attr):
attributes = cls.getattr(attr)
for attribute in attributes:
try:
for inferred in attribute.infer():
if isinstance(inferred, astroid.Instance):
try:
inferred.getattr('__get__')
inferred.getattr('__set__')
except astroid.NotFoundError:
continue
else:
return True
except astroid.InferenceError:
# Can't infer, avoid emitting a false positive in this case.
return True
return False
示例10: _called_in_methods
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [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
示例11: _safe_infer_call_result
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def _safe_infer_call_result(node, caller, context=None):
"""
Safely infer the return value of a function.
Returns None if inference failed or if there is some ambiguity (more than
one node has been inferred). Otherwise returns infered value.
"""
try:
inferit = node.infer_call_result(caller, context=context)
value = next(inferit)
except astroid.InferenceError:
return None # inference failed
except StopIteration:
return None # no values infered
try:
next(inferit)
return None # there is ambiguity on the inferred node
except astroid.InferenceError:
return None # there is some kind of ambiguity
except StopIteration:
return value
示例12: _get_attribute_property_setter
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def _get_attribute_property_setter(name, klass):
"""Return the name of a setter for name in klass, if name is a property."""
try:
attributes = klass.getattr(name)
except astroid.NotFoundError:
return None
property_name = "{0}.property".format(BUILTINS)
for attr in attributes:
try:
infered = next(attr.infer())
except astroid.InferenceError:
continue
if infered.pytype() == property_name:
try:
# In this case, attr is in a statement of the form
# <attr> = property(<fget>, <fset>); return the name of <fset>,
# assuming it is an identifier (and not a more complex expression).
return attr.parent.value.args[1].name
except Exception:
return None
示例13: _nose_tools_functions
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def _nose_tools_functions():
"""Get an iterator of names and bound methods."""
module = _BUILDER.string_build(
textwrap.dedent(
"""
import unittest
class Test(unittest.TestCase):
pass
a = Test()
"""
)
)
try:
case = next(module["a"].infer())
except astroid.InferenceError:
return
for method in case.methods():
if method.name.startswith("assert") and "_" not in method.name:
pep8_name = _pep8(method.name)
yield pep8_name, astroid.BoundMethod(method, case)
if method.name == "assertEqual":
# nose also exports assert_equals.
yield "assert_equals", astroid.BoundMethod(method, case)
示例14: interfaces
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def interfaces(node, herited=True, handler_func=_iface_hdlr):
"""Return an iterator on interfaces implemented by the given class node."""
# FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)...
try:
implements = bases.Instance(node).getattr("__implements__")[0]
except exceptions.NotFoundError:
return
if not herited and implements.frame() is not node:
return
found = set()
missing = False
for iface in node_classes.unpack_infer(implements):
if iface is astroid.Uninferable:
missing = True
continue
if iface not in found and handler_func(iface):
found.add(iface)
yield iface
if missing:
raise exceptions.InferenceError()
示例15: _is_enum_class
# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import InferenceError [as 別名]
def _is_enum_class(node: astroid.ClassDef) -> bool:
"""Check if a class definition defines an Enum class.
:param node: The class node to check.
:type node: astroid.ClassDef
:returns: True if the given node represents an Enum class. False otherwise.
:rtype: bool
"""
for base in node.bases:
try:
inferred_bases = base.inferred()
except astroid.InferenceError:
continue
for ancestor in inferred_bases:
if not isinstance(ancestor, astroid.ClassDef):
continue
if ancestor.name == "Enum" and ancestor.root().name == "enum":
return True
return False