Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_issue5_inferenceerror_should_not_propagate(self):
node = astroid.extract_node("""
foo = 'bar/baz'.split('/')[-1]
""")
try:
self.walk(node.root())
except astroid.exceptions.InferenceError:
pytest.fail("InferenceError should not propagate")
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()
else:
seen.add(baseobj_name)
if isinstance(baseobj, bases.Instance):
# not abstract
return False
if baseobj is util.Uninferable:
continue
if baseobj is klass:
continue
if not isinstance(baseobj, ClassDef):
continue
if baseobj._type == 'metaclass':
return True
if _is_metaclass(baseobj, seen):
return True
except exceptions.InferenceError:
continue
return False
# set lookup name since this is necessary to infer on import nodes for
# instance
context = contextmod.copy_context(context)
context.lookupname = name
metaclass = self.declared_metaclass(context=context)
try:
attr = self.getattr(name, context, class_context=class_context)[0]
for inferred in bases._infer_stmts([attr], context, frame=self):
# yield Uninferable object instead of descriptors when necessary
if not isinstance(inferred, node_classes.Const) and isinstance(
inferred, bases.Instance
):
try:
inferred._proxied.getattr("__get__", context)
except exceptions.AttributeInferenceError:
yield inferred
else:
yield util.Uninferable
elif isinstance(inferred, objects.Property):
function = inferred.function
if not class_context:
# Through an instance so we can solve the property
yield from function.infer_call_result(
caller=self, context=context
)
# If we have a metaclass, we're accessing this attribute through
# the class itself, which means we can solve the property
elif metaclass:
# Resolve a property as long as it is not accessed through
# the class itself.
yield from function.infer_call_result(
yield_point = next(
(node for node in possible_yield_points if node.scope() == func), None
)
if yield_point:
if not yield_point.value:
const = nodes.Const(None)
const.parent = yield_point
const.lineno = yield_point.lineno
yield const
else:
yield from yield_point.value.infer(context=context)
elif isinstance(inferred, bases.Instance):
try:
enter = next(inferred.igetattr("__enter__", context=context))
except (exceptions.InferenceError, exceptions.AttributeInferenceError):
raise exceptions.InferenceError(node=inferred)
if not isinstance(enter, bases.BoundMethod):
raise exceptions.InferenceError(node=enter)
yield from enter.infer_call_result(self, context)
else:
raise exceptions.InferenceError(node=mgr)
modname = ".".join(modutils.modpath_from_file(filepath))
except ImportError:
modname = filepath
if (
modname in self.astroid_cache
and self.astroid_cache[modname].file == filepath
):
return self.astroid_cache[modname]
if source:
# pylint: disable=import-outside-toplevel; circular import
from astroid.builder import AstroidBuilder
return AstroidBuilder(self).file_build(filepath, modname)
if fallback and modname:
return self.ast_from_module_name(modname)
raise exceptions.AstroidBuildingError(
"Unable to build an AST for {path}.", path=filepath
)
def _is_pathlib_write(expr) -> bool:
if not isinstance(expr, astroid.Call):
return False
if not isinstance(expr.func, astroid.Attribute):
return False
if expr.func.attrname not in ('write_text', 'write_bytes', 'open'):
return False
# if it's open, check that mode is "w"
if expr.func.attrname == 'open':
if not _is_open_to_write(expr):
return False
try:
guesses = tuple(expr.func.expr.infer())
except astroid.exceptions.NameInferenceError:
return False
for value in guesses:
if isinstance(value, astroid.Instance):
if value.pytype().startswith('pathlib.'):
return True
return False
"""Lookup the given special method name in the given *node*
If the special method was found, then a list of attributes
will be returned. Otherwise, `astroid.AttributeInferenceError`
is going to be raised.
"""
if isinstance(
node, (astroid.List, astroid.Tuple, astroid.Const, astroid.Dict, astroid.Set)
):
return _builtin_lookup(node, name)
if isinstance(node, astroid.Instance):
return _lookup_in_mro(node, name)
if isinstance(node, astroid.ClassDef):
return _class_lookup(node, name)
raise exceptions.AttributeInferenceError(attribute=name, target=node)
def igetattr(self, name, context=None):
"""Retrieve the inferred values of the given attribute name."""
if name in self.special_attributes:
yield self.special_attributes.lookup(name)
return
try:
mro = self.super_mro()
# Don't let invalid MROs or invalid super calls
# leak out as is from this function.
except exceptions.SuperError as exc:
raise exceptions.AttributeInferenceError(
(
"Lookup for {name} on {target!r} because super call {super!r} "
"is invalid."
),
target=self,
attribute=name,
context=context,
super_=exc.super_,
) from exc
except exceptions.MroError as exc:
raise exceptions.AttributeInferenceError(
(
"Lookup for {name} on {target!r} failed because {cls!r} has an "
"invalid MRO."
),
target=self,
:param argname: The name of the argument to get the default value for.
:type argname: str
:raises NoDefault: If there is no default value defined for the
given argument.
"""
i = _find_arg(argname, self.args)[0]
if i is not None:
idx = i - (len(self.args) - len(self.defaults))
if idx >= 0:
return self.defaults[idx]
i = _find_arg(argname, self.kwonlyargs)[0]
if i is not None and self.kw_defaults[i] is not None:
return self.kw_defaults[i]
raise exceptions.NoDefault(func=self.parent, name=argname)