2018-07-15 09:36:36 +00:00
|
|
|
# Copyright (c) 2015-2018 Claudiu Popa <pcmanticore@gmail.com>
|
2017-12-15 11:24:15 +00:00
|
|
|
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.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
|
2015-04-01 10:47:51 +00:00
|
|
|
|
2016-06-29 16:32:16 +00:00
|
|
|
import astroid
|
2015-04-01 10:47:51 +00:00
|
|
|
|
|
|
|
from pylint.checkers import strings
|
2018-09-01 18:58:11 +00:00
|
|
|
from pylint.testutils import CheckerTestCase, Message
|
2015-04-01 10:47:51 +00:00
|
|
|
|
|
|
|
|
2016-12-06 15:42:53 +00:00
|
|
|
class TestStringChecker(CheckerTestCase):
|
2016-01-13 14:21:47 +00:00
|
|
|
CHECKER_CLASS = strings.StringFormatChecker
|
2015-04-01 10:47:51 +00:00
|
|
|
|
|
|
|
def test_format_bytes(self):
|
|
|
|
code = "b'test'.format(1, 2)"
|
2016-06-29 16:32:16 +00:00
|
|
|
node = astroid.extract_node(code)
|
2015-04-01 10:47:51 +00:00
|
|
|
with self.assertNoMessages():
|
2015-08-26 19:09:27 +00:00
|
|
|
self.checker.visit_call(node)
|
2018-09-01 18:58:11 +00:00
|
|
|
|
|
|
|
def test_format_types(self):
|
|
|
|
for code in ("'%s' % 1", "'%d' % 1", "'%f' % 1"):
|
|
|
|
with self.assertNoMessages():
|
|
|
|
node = astroid.extract_node(code)
|
|
|
|
self.checker.visit_binop(node)
|
|
|
|
|
2018-09-16 15:33:50 +00:00
|
|
|
for code in (
|
|
|
|
"'%s' % 1",
|
|
|
|
"'%(key)s' % {'key' : 1}",
|
|
|
|
"'%d' % 1",
|
|
|
|
"'%(key)d' % {'key' : 1}",
|
|
|
|
"'%f' % 1",
|
|
|
|
"'%(key)f' % {'key' : 1}",
|
|
|
|
"'%d' % 1.1",
|
|
|
|
"'%(key)d' % {'key' : 1.1}",
|
|
|
|
"'%s' % []",
|
|
|
|
"'%(key)s' % {'key' : []}",
|
|
|
|
"'%s' % None",
|
|
|
|
"'%(key)s' % {'key' : None}",
|
|
|
|
):
|
2018-09-01 18:58:11 +00:00
|
|
|
with self.assertNoMessages():
|
|
|
|
node = astroid.extract_node(code)
|
|
|
|
self.checker.visit_binop(node)
|
|
|
|
|
2018-09-16 15:33:50 +00:00
|
|
|
for code, arg_type, format_type in [
|
|
|
|
("'%d' % '1'", "builtins.str", "d"),
|
|
|
|
("'%(key)d' % {'key' : '1'}", "builtins.str", "d"),
|
|
|
|
("'%x' % 1.1", "builtins.float", "x"),
|
|
|
|
("'%(key)x' % {'key' : 1.1}", "builtins.float", "x"),
|
|
|
|
("'%d' % []", "builtins.list", "d"),
|
|
|
|
("'%(key)d' % {'key' : []}", "builtins.list", "d"),
|
|
|
|
]:
|
2018-09-01 18:58:11 +00:00
|
|
|
node = astroid.extract_node(code)
|
|
|
|
with self.assertAddsMessages(
|
2018-09-16 15:33:50 +00:00
|
|
|
Message(
|
|
|
|
"bad-string-format-type", node=node, args=(arg_type, format_type)
|
|
|
|
)
|
|
|
|
):
|
2018-09-01 18:58:11 +00:00
|
|
|
self.checker.visit_binop(node)
|