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_igetattr(self):
func = builder.extract_node(
"""
def test():
pass
"""
)
func.instance_attrs["value"] = [nodes.Const(42)]
value = func.getattr("value")
self.assertEqual(len(value), 1)
self.assertIsInstance(value[0], nodes.Const)
self.assertEqual(value[0].value, 42)
inferred = next(func.igetattr("value"))
self.assertIsInstance(inferred, nodes.Const)
self.assertEqual(inferred.value, 42)
@test_utils.require_version("3.8")
def test_posonlyargs_default_value():
ast_node = builder.extract_node(
"""
def func(a, b=1, /, c=2): pass
"""
)
last_param = ast_node.args.default_value("c")
assert isinstance(last_param, nodes.Const)
assert last_param.value == 2
first_param = ast_node.args.default_value("b")
assert isinstance(first_param, nodes.Const)
assert first_param.value == 1
# function('aeozrijz\
# earzer', hop)
discard = stmts[0]
self.assertIsInstance(discard, nodes.Expr)
self.assertEqual(discard.fromlineno, 4)
self.assertEqual(discard.tolineno, 5)
callfunc = discard.value
self.assertIsInstance(callfunc, nodes.Call)
self.assertEqual(callfunc.fromlineno, 4)
self.assertEqual(callfunc.tolineno, 5)
name = callfunc.func
self.assertIsInstance(name, nodes.Name)
self.assertEqual(name.fromlineno, 4)
self.assertEqual(name.tolineno, 4)
strarg = callfunc.args[0]
self.assertIsInstance(strarg, nodes.Const)
if hasattr(sys, "pypy_version_info"):
lineno = 4
else:
lineno = 5 if not PY38 else 4
self.assertEqual(strarg.fromlineno, lineno)
self.assertEqual(strarg.tolineno, lineno)
namearg = callfunc.args[1]
self.assertIsInstance(namearg, nodes.Name)
self.assertEqual(namearg.fromlineno, 5)
self.assertEqual(namearg.tolineno, 5)
# on line 10:
# fonction(1,
# 2,
# 3,
# 4)
discard = stmts[2]
def visit_str(self, node, parent, assign_ctx=None):
"""visit a Str node by returning a fresh instance of Const"""
newnode = new.Const(node.s)
_set_infos(node, newnode, parent)
return newnode
proxy.parent = astroid_builtin
else:
proxy = astroid_builtin.getattr(cls.__name__)[0]
if cls in (dict, list, set, tuple):
node_cls._proxied = proxy
else:
_CONST_PROXY[cls] = proxy
_astroid_bootstrapping()
# TODO : find a nicer way to handle this situation;
# However __proxied introduced an
# infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870)
def _set_proxied(const):
return _CONST_PROXY[const.value.__class__]
Const._proxied = property(_set_proxied)
from types import GeneratorType
Generator._proxied = Class(GeneratorType.__name__, GeneratorType.__doc__)
Astroid_BUILDER.object_build(Generator._proxied, GeneratorType)
def visit_const(self, node, parent, assign_ctx=None):
"""visit a Const node by returning a fresh instance of it"""
newnode = new.Const(node.value)
_set_infos(node, newnode, parent)
return newnode
break
else:
# It doesn't interest us.
raise exceptions.InferenceError(node=func)
# Get the first yield point. If it has multiple yields,
# then a RuntimeError will be raised.
possible_yield_points = func.nodes_of_class(nodes.Yield)
# Ignore yields in nested functions
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)
def infer_str(node, context=None):
"""Infer str() calls
:param nodes.Call node: str() call to infer
:param context.InferenceContext: node context
:rtype nodes.Const: a Const containing an empty string
"""
call = arguments.CallSite.from_call(node)
if call.keyword_arguments:
raise UseInferenceDefault("TypeError: str() must take no keyword arguments")
try:
return nodes.Const("")
except (AstroidTypeError, InferenceError) as exc:
raise UseInferenceDefault(str(exc)) from exc
# obj is to be check is an instance of
try:
class_container = _class_or_tuple_to_container(
class_or_tuple_node, context=context
)
except InferenceError:
raise UseInferenceDefault
try:
isinstance_bool = helpers.object_isinstance(obj_node, class_container, context)
except AstroidTypeError as exc:
raise UseInferenceDefault("TypeError: " + str(exc))
except MroError as exc:
raise UseInferenceDefault from exc
if isinstance_bool is util.Uninferable:
raise UseInferenceDefault
return nodes.Const(isinstance_bool)