Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with self.assertRaises(utils.NoSuchArgumentError):
node = test_utils.extract_node('foo(3)')
utils.get_argument_from_call(node, keyword='bar')
with self.assertRaises(utils.NoSuchArgumentError):
node = test_utils.extract_node('foo(one=a, two=b, three=c)')
utils.get_argument_from_call(node, position=1)
node = test_utils.extract_node('foo(a, b, c)')
self.assertIsNotNone(utils.get_argument_from_call(node, position=1))
node = test_utils.extract_node('foo(a, not_this_one=1, this_one=2)')
arg = utils.get_argument_from_call(node, position=1, keyword='this_one')
self.assertEqual(2, arg.value)
node = test_utils.extract_node('foo(a)')
with self.assertRaises(utils.NoSuchArgumentError):
utils.get_argument_from_call(node, position=1)
with self.assertRaises(ValueError):
utils.get_argument_from_call(node, None, None)
name = utils.get_argument_from_call(node, position=0)
self.assertEqual(name.name, 'a')
def testGetArgumentFromCall():
node = astroid.extract_node("foo(a, not_this_one=1, this_one=2)")
arg = utils.get_argument_from_call(node, position=2, keyword="this_one")
assert 2 == arg.value
node = astroid.extract_node("foo(a)")
with pytest.raises(utils.NoSuchArgumentError):
utils.get_argument_from_call(node, position=1)
with pytest.raises(ValueError):
utils.get_argument_from_call(node, None, None)
name = utils.get_argument_from_call(node, position=0)
assert name.name == "a"
def testGetArgumentFromCall(self):
node = test_utils.extract_node('foo(bar=3)')
self.assertIsNotNone(utils.get_argument_from_call(node, keyword='bar'))
with self.assertRaises(utils.NoSuchArgumentError):
node = test_utils.extract_node('foo(3)')
utils.get_argument_from_call(node, keyword='bar')
with self.assertRaises(utils.NoSuchArgumentError):
node = test_utils.extract_node('foo(one=a, two=b, three=c)')
utils.get_argument_from_call(node, position=1)
node = test_utils.extract_node('foo(a, b, c)')
self.assertIsNotNone(utils.get_argument_from_call(node, position=1))
node = test_utils.extract_node('foo(a, not_this_one=1, this_one=2)')
arg = utils.get_argument_from_call(node, position=1, keyword='this_one')
self.assertEqual(2, arg.value)
node = test_utils.extract_node('foo(a)')
with self.assertRaises(utils.NoSuchArgumentError):
utils.get_argument_from_call(node, position=1)
with self.assertRaises(ValueError):
utils.get_argument_from_call(node, None, None)
name = utils.get_argument_from_call(node, position=0)
self.assertEqual(name.name, 'a')
def _check_new_format_specifiers(self, node, fields, named):
"""
Check attribute and index access in the format
string ("{0.a}" and "{0[a]}").
"""
for key, specifiers in fields:
# Obtain the argument. If it can't be obtained
# or inferred, skip this check.
if key == "":
# {[0]} will have an unnamed argument, defaulting
# to 0. It will not be present in `named`, so use the value
# 0 for it.
key = 0
if isinstance(key, numbers.Number):
try:
argname = utils.get_argument_from_call(node, key)
except utils.NoSuchArgumentError:
continue
else:
if key not in named:
continue
argname = named[key]
if argname in (astroid.Uninferable, None):
continue
try:
argument = utils.safe_infer(argname)
except astroid.InferenceError:
continue
if not specifiers or not argument:
# No need to check this key if it doesn't
# use attribute / item access
continue
def _check_reversed(self, node):
""" check that the argument to `reversed` is a sequence """
try:
argument = safe_infer(get_argument_from_call(node, position=0))
except NoSuchArgumentError:
self.add_message('missing-reversed-argument', node=node)
else:
if argument is astroid.YES:
return
if argument is None:
# Nothing was infered.
# Try to see if we have iter().
if isinstance(node.args[0], astroid.CallFunc):
try:
func = next(node.args[0].func.infer())
except InferenceError:
return
if (getattr(func, 'name', None) == 'iter' and
is_builtin_object(func)):
self.add_message('bad-reversed-sequence', node=node)
def _check_open_mode(self, node):
"""Check that the mode argument of an open or file call is valid."""
try:
mode_arg = utils.get_argument_from_call(node, position=1, keyword="mode")
except utils.NoSuchArgumentError:
return
if mode_arg:
mode_arg = utils.safe_infer(mode_arg)
if isinstance(mode_arg, astroid.Const) and not _check_mode_str(
mode_arg.value
):
self.add_message("bad-open-mode", node=node, args=mode_arg.value)
def _check_open_encoding(self, node):
"""Check that an open() call always has an encoding set."""
try:
mode_arg = utils.get_argument_from_call(node, position=1,
keyword='mode')
except utils.NoSuchArgumentError:
mode_arg = None
_encoding = None
try:
_encoding = utils.get_argument_from_call(node, position=2)
except utils.NoSuchArgumentError:
try:
_encoding = utils.get_argument_from_call(node,
keyword='encoding')
except utils.NoSuchArgumentError:
pass
if _encoding is None:
if mode_arg is None:
mode = None
else:
mode = utils.safe_infer(mode_arg)
if mode is not None and not isinstance(mode, astroid.Const):
# We can't say what mode is exactly.
return
if mode is None:
self.add_message('open-without-encoding', node=node)
elif 'b' in getattr(mode, 'value', ''):
# Files opened as binary don't need an encoding.
return
def _check_reversed(self, node):
""" check that the argument to `reversed` is a sequence """
try:
argument = utils.safe_infer(utils.get_argument_from_call(node, position=0))
except utils.NoSuchArgumentError:
pass
else:
if argument is astroid.Uninferable:
return
if argument is None:
# Nothing was inferred.
# Try to see if we have iter().
if isinstance(node.args[0], astroid.Call):
try:
func = next(node.args[0].func.infer())
except astroid.InferenceError:
return
if getattr(
func, "name", None
) == "iter" and utils.is_builtin_object(func):
def _check_open_mode(self, node):
"""Check that the mode argument of an open or file call is valid."""
try:
mode_arg = utils.get_argument_from_call(node, position=1, keyword='mode')
if mode_arg:
mode_arg = utils.safe_infer(mode_arg)
if (isinstance(mode_arg, astroid.Const)
and not _VALID_OPEN_MODE_REGEX.match(mode_arg.value)):
self.add_message('bad-open-mode', node=node,
args=(mode_arg.value))
except (utils.NoSuchArgumentError, TypeError):
pass
def _check_reversed(self, node):
""" check that the argument to `reversed` is a sequence """
try:
argument = safe_infer(get_argument_from_call(node, position=0))
except NoSuchArgumentError:
self.add_message('missing-reversed-argument', node=node)
else:
if argument is astroid.YES:
return
if argument is None:
# Nothing was infered.
# Try to see if we have iter().
if isinstance(node.args[0], astroid.CallFunc):
try:
func = next(node.args[0].func.infer())
except InferenceError:
return
if (getattr(func, 'name', None) == 'iter' and
is_builtin_object(func)):
self.add_message('bad-reversed-sequence', node=node)