2017-12-15 11:24:15 +00:00
|
|
|
# Copyright (c) 2010 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
|
2018-07-15 09:36:36 +00:00
|
|
|
# Copyright (c) 2013-2017 Claudiu Popa <pcmanticore@gmail.com>
|
2017-12-15 11:24:15 +00:00
|
|
|
# Copyright (c) 2013-2014 Google, Inc.
|
|
|
|
# Copyright (c) 2014 Arun Persaud <arun@nubati.net>
|
|
|
|
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
|
|
|
|
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
|
2018-08-21 16:06:17 +00:00
|
|
|
# Copyright (c) 2018 Caio Carrara <ccarrara@redhat.com>
|
2016-07-22 21:22:28 +00:00
|
|
|
|
2016-06-01 15:11:29 +00:00
|
|
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
|
|
|
|
|
2014-07-26 15:11:14 +00:00
|
|
|
"""Tests for the pylint.checkers.utils module."""
|
2006-04-26 10:48:09 +00:00
|
|
|
|
2016-06-29 16:32:16 +00:00
|
|
|
import astroid
|
2013-06-19 09:46:37 +00:00
|
|
|
|
2006-04-26 10:48:09 +00:00
|
|
|
from pylint.checkers import utils
|
2016-12-02 17:09:18 +00:00
|
|
|
import pytest
|
2006-04-26 10:48:09 +00:00
|
|
|
|
|
|
|
|
2018-09-16 15:33:50 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"name,expected",
|
|
|
|
[
|
|
|
|
("min", True),
|
|
|
|
("__builtins__", True),
|
|
|
|
("__path__", False),
|
|
|
|
("__file__", False),
|
|
|
|
("whatever", False),
|
|
|
|
("mybuiltin", False),
|
|
|
|
],
|
|
|
|
)
|
2016-12-14 13:50:39 +00:00
|
|
|
def testIsBuiltin(name, expected):
|
2016-12-08 20:34:58 +00:00
|
|
|
assert utils.is_builtin(name) == expected
|
|
|
|
|
|
|
|
|
2018-09-16 15:33:50 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"fn,kw",
|
|
|
|
[("foo(3)", {"keyword": "bar"}), ("foo(one=a, two=b, three=c)", {"position": 1})],
|
|
|
|
)
|
2016-12-08 20:34:58 +00:00
|
|
|
def testGetArgumentFromCallError(fn, kw):
|
2016-12-06 15:42:53 +00:00
|
|
|
with pytest.raises(utils.NoSuchArgumentError):
|
2016-12-08 20:34:58 +00:00
|
|
|
node = astroid.extract_node(fn)
|
|
|
|
utils.get_argument_from_call(node, **kw)
|
|
|
|
|
|
|
|
|
2018-09-16 15:33:50 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"fn,kw", [("foo(bar=3)", {"keyword": "bar"}), ("foo(a, b, c)", {"position": 1})]
|
|
|
|
)
|
2016-12-08 20:34:58 +00:00
|
|
|
def testGetArgumentFromCallExists(fn, kw):
|
|
|
|
node = astroid.extract_node(fn)
|
|
|
|
assert utils.get_argument_from_call(node, **kw) is not None
|
|
|
|
|
|
|
|
|
|
|
|
def testGetArgumentFromCall():
|
2018-09-16 15:33:50 +00:00
|
|
|
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")
|
2016-12-06 15:42:53 +00:00
|
|
|
assert 2 == arg.value
|
2016-12-08 20:34:58 +00:00
|
|
|
|
2018-09-16 15:33:50 +00:00
|
|
|
node = astroid.extract_node("foo(a)")
|
2016-12-06 15:42:53 +00:00
|
|
|
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)
|
2018-09-16 15:33:50 +00:00
|
|
|
assert name.name == "a"
|
2013-06-19 09:46:37 +00:00
|
|
|
|
2016-12-08 20:34:58 +00:00
|
|
|
|
2016-12-06 15:42:53 +00:00
|
|
|
def test_error_of_type():
|
2018-09-16 15:33:50 +00:00
|
|
|
nodes = astroid.extract_node(
|
|
|
|
"""
|
2016-12-06 15:42:53 +00:00
|
|
|
try: pass
|
|
|
|
except AttributeError: #@
|
|
|
|
pass
|
|
|
|
try: pass
|
|
|
|
except Exception: #@
|
|
|
|
pass
|
|
|
|
except: #@
|
|
|
|
pass
|
2018-09-16 15:33:50 +00:00
|
|
|
"""
|
|
|
|
)
|
2016-12-06 15:42:53 +00:00
|
|
|
assert utils.error_of_type(nodes[0], AttributeError)
|
2018-09-16 15:33:50 +00:00
|
|
|
assert utils.error_of_type(nodes[0], (AttributeError,))
|
2016-12-06 15:42:53 +00:00
|
|
|
assert not utils.error_of_type(nodes[0], Exception)
|
|
|
|
assert utils.error_of_type(nodes[1], Exception)
|
2018-08-01 08:33:49 +00:00
|
|
|
assert utils.error_of_type(nodes[2], ImportError)
|
2015-05-17 13:40:19 +00:00
|
|
|
|
2016-12-08 20:34:58 +00:00
|
|
|
|
2016-12-06 15:42:53 +00:00
|
|
|
def test_node_ignores_exception():
|
2018-09-16 15:33:50 +00:00
|
|
|
nodes = astroid.extract_node(
|
|
|
|
"""
|
2016-12-06 15:42:53 +00:00
|
|
|
try:
|
|
|
|
1/0 #@
|
|
|
|
except ZeroDivisionError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
1/0 #@
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
2/0 #@
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
1/0 #@
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2018-09-16 15:33:50 +00:00
|
|
|
"""
|
|
|
|
)
|
2016-12-06 15:42:53 +00:00
|
|
|
assert utils.node_ignores_exception(nodes[0], ZeroDivisionError)
|
|
|
|
assert not utils.node_ignores_exception(nodes[1], ZeroDivisionError)
|
2018-08-01 08:33:13 +00:00
|
|
|
assert utils.node_ignores_exception(nodes[2], ZeroDivisionError)
|
2016-12-06 15:42:53 +00:00
|
|
|
assert not utils.node_ignores_exception(nodes[3], ZeroDivisionError)
|
2018-08-21 16:06:17 +00:00
|
|
|
|
|
|
|
|
2018-08-22 06:57:16 +00:00
|
|
|
def test_is_subclass_of_node_b_derived_from_node_a():
|
2018-09-16 15:33:50 +00:00
|
|
|
nodes = astroid.extract_node(
|
|
|
|
"""
|
2018-08-21 16:06:17 +00:00
|
|
|
class Superclass: #@
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Subclass(Superclass): #@
|
|
|
|
pass
|
2018-09-16 15:33:50 +00:00
|
|
|
"""
|
|
|
|
)
|
2018-08-21 16:06:17 +00:00
|
|
|
assert utils.is_subclass_of(nodes[1], nodes[0])
|
|
|
|
|
|
|
|
|
2018-08-22 06:57:16 +00:00
|
|
|
def test_is_subclass_of_node_b_not_derived_from_node_a():
|
2018-09-16 15:33:50 +00:00
|
|
|
nodes = astroid.extract_node(
|
|
|
|
"""
|
2018-08-21 16:06:17 +00:00
|
|
|
class OneClass: #@
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AnotherClass: #@
|
|
|
|
pass
|
2018-09-16 15:33:50 +00:00
|
|
|
"""
|
|
|
|
)
|
2018-08-21 16:06:17 +00:00
|
|
|
assert not utils.is_subclass_of(nodes[1], nodes[0])
|
|
|
|
|
|
|
|
|
2018-08-22 06:57:16 +00:00
|
|
|
def test_is_subclass_of_not_classdefs():
|
2018-09-16 15:33:50 +00:00
|
|
|
node = astroid.extract_node(
|
|
|
|
"""
|
2018-08-21 16:06:17 +00:00
|
|
|
class OneClass: #@
|
|
|
|
pass
|
2018-09-16 15:33:50 +00:00
|
|
|
"""
|
|
|
|
)
|
2018-08-21 16:06:17 +00:00
|
|
|
assert not utils.is_subclass_of(None, node)
|
|
|
|
assert not utils.is_subclass_of(node, None)
|
|
|
|
assert not utils.is_subclass_of(None, None)
|
2018-10-04 07:28:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_format_method_string():
|
|
|
|
samples = [
|
|
|
|
("{}", 1),
|
|
|
|
("{}:{}", 2),
|
|
|
|
("{field}", 1),
|
|
|
|
("{:5}", 1),
|
|
|
|
("{:10}", 1),
|
|
|
|
("{field:10}", 1),
|
|
|
|
("{field:10}{{}}", 1),
|
|
|
|
("{:5}{!r:10}", 2),
|
|
|
|
("{:5}{}{{}}{}", 3),
|
|
|
|
("{0}{1}{0}", 2),
|
|
|
|
("Coordinates: {latitude}, {longitude}", 2),
|
|
|
|
("X: {0[0]}; Y: {0[1]}", 1),
|
|
|
|
("{:*^30}", 1),
|
|
|
|
("{!r:}", 1),
|
|
|
|
]
|
|
|
|
for fmt, count in samples:
|
|
|
|
keys, num_args, pos_args = utils.parse_format_method_string(fmt)
|
|
|
|
keyword_args = len(set(k for k, l in keys if not isinstance(k, int)))
|
|
|
|
assert keyword_args + num_args + pos_args == count
|