2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-11-26 08:17:47 +08:00
|
|
|
import ast
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2019-02-12 11:56:15 +08:00
|
|
|
from pre_commit_hooks.check_builtin_literals import Call
|
2017-11-26 08:17:47 +08:00
|
|
|
from pre_commit_hooks.check_builtin_literals import main
|
2019-02-12 11:56:15 +08:00
|
|
|
from pre_commit_hooks.check_builtin_literals import Visitor
|
2019-02-01 11:19:10 +08:00
|
|
|
|
|
|
|
BUILTIN_CONSTRUCTORS = '''\
|
2020-02-06 03:10:42 +08:00
|
|
|
import builtins
|
2019-02-01 11:19:10 +08:00
|
|
|
|
|
|
|
c1 = complex()
|
|
|
|
d1 = dict()
|
|
|
|
f1 = float()
|
|
|
|
i1 = int()
|
|
|
|
l1 = list()
|
|
|
|
s1 = str()
|
|
|
|
t1 = tuple()
|
|
|
|
|
|
|
|
c2 = builtins.complex()
|
|
|
|
d2 = builtins.dict()
|
|
|
|
f2 = builtins.float()
|
|
|
|
i2 = builtins.int()
|
|
|
|
l2 = builtins.list()
|
|
|
|
s2 = builtins.str()
|
|
|
|
t2 = builtins.tuple()
|
|
|
|
'''
|
|
|
|
BUILTIN_LITERALS = '''\
|
|
|
|
c1 = 0j
|
|
|
|
d1 = {}
|
|
|
|
f1 = 0.0
|
|
|
|
i1 = 0
|
|
|
|
l1 = []
|
|
|
|
s1 = ''
|
|
|
|
t1 = ()
|
|
|
|
'''
|
2017-11-26 08:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def visitor():
|
2019-02-12 11:56:15 +08:00
|
|
|
return Visitor()
|
2017-11-26 08:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('expression', 'calls'),
|
|
|
|
[
|
2018-05-18 08:14:25 +08:00
|
|
|
# see #285
|
|
|
|
('x[0]()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
# complex
|
2019-02-12 11:57:37 +08:00
|
|
|
('0j', []),
|
|
|
|
('complex()', [Call('complex', 1, 0)]),
|
|
|
|
('complex(0, 0)', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
("complex('0+0j')", []),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.complex()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
# float
|
2019-02-12 11:57:37 +08:00
|
|
|
('0.0', []),
|
|
|
|
('float()', [Call('float', 1, 0)]),
|
2017-11-26 08:17:47 +08:00
|
|
|
("float('0.0')", []),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.float()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
# int
|
2019-02-12 11:57:37 +08:00
|
|
|
('0', []),
|
|
|
|
('int()', [Call('int', 1, 0)]),
|
2017-11-26 08:17:47 +08:00
|
|
|
("int('0')", []),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.int()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
# list
|
2019-02-12 11:57:37 +08:00
|
|
|
('[]', []),
|
|
|
|
('list()', [Call('list', 1, 0)]),
|
2017-11-26 08:17:47 +08:00
|
|
|
("list('abc')", []),
|
|
|
|
("list([c for c in 'abc'])", []),
|
|
|
|
("list(c for c in 'abc')", []),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.list()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
# str
|
|
|
|
("''", []),
|
2019-02-12 11:57:37 +08:00
|
|
|
('str()', [Call('str', 1, 0)]),
|
2017-11-26 08:17:47 +08:00
|
|
|
("str('0')", []),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.str()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
# tuple
|
2019-02-12 11:57:37 +08:00
|
|
|
('()', []),
|
|
|
|
('tuple()', [Call('tuple', 1, 0)]),
|
2017-11-26 08:17:47 +08:00
|
|
|
("tuple('abc')", []),
|
|
|
|
("tuple([c for c in 'abc'])", []),
|
|
|
|
("tuple(c for c in 'abc')", []),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.tuple()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_non_dict_exprs(visitor, expression, calls):
|
|
|
|
visitor.visit(ast.parse(expression))
|
|
|
|
assert visitor.builtin_type_calls == calls
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('expression', 'calls'),
|
|
|
|
[
|
2019-02-12 11:57:37 +08:00
|
|
|
('{}', []),
|
|
|
|
('dict()', [Call('dict', 1, 0)]),
|
|
|
|
('dict(a=1, b=2, c=3)', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
("dict(**{'a': 1, 'b': 2, 'c': 3})", []),
|
|
|
|
("dict([(k, v) for k, v in [('a', 1), ('b', 2), ('c', 3)]])", []),
|
|
|
|
("dict((k, v) for k, v in [('a', 1), ('b', 2), ('c', 3)])", []),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.dict()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_dict_allow_kwargs_exprs(visitor, expression, calls):
|
|
|
|
visitor.visit(ast.parse(expression))
|
|
|
|
assert visitor.builtin_type_calls == calls
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('expression', 'calls'),
|
|
|
|
[
|
2019-02-12 11:57:37 +08:00
|
|
|
('dict()', [Call('dict', 1, 0)]),
|
|
|
|
('dict(a=1, b=2, c=3)', [Call('dict', 1, 0)]),
|
2019-02-12 11:56:15 +08:00
|
|
|
("dict(**{'a': 1, 'b': 2, 'c': 3})", [Call('dict', 1, 0)]),
|
2017-12-01 02:27:16 +08:00
|
|
|
('builtins.dict()', []),
|
2017-11-26 08:17:47 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_dict_no_allow_kwargs_exprs(expression, calls):
|
2019-02-12 11:56:15 +08:00
|
|
|
visitor = Visitor(allow_dict_kwargs=False)
|
2017-11-26 08:17:47 +08:00
|
|
|
visitor.visit(ast.parse(expression))
|
|
|
|
assert visitor.builtin_type_calls == calls
|
|
|
|
|
|
|
|
|
|
|
|
def test_ignore_constructors():
|
2020-02-04 00:41:48 +08:00
|
|
|
visitor = Visitor(
|
|
|
|
ignore=('complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'),
|
|
|
|
)
|
2019-02-01 11:19:10 +08:00
|
|
|
visitor.visit(ast.parse(BUILTIN_CONSTRUCTORS))
|
2017-11-26 08:17:47 +08:00
|
|
|
assert visitor.builtin_type_calls == []
|
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_failing_file(tmpdir):
|
|
|
|
f = tmpdir.join('f.py')
|
|
|
|
f.write(BUILTIN_CONSTRUCTORS)
|
2020-05-21 00:07:45 +08:00
|
|
|
rc = main([str(f)])
|
2017-11-26 08:17:47 +08:00
|
|
|
assert rc == 1
|
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_passing_file(tmpdir):
|
|
|
|
f = tmpdir.join('f.py')
|
|
|
|
f.write(BUILTIN_LITERALS)
|
2020-05-21 00:07:45 +08:00
|
|
|
rc = main([str(f)])
|
2017-11-26 08:17:47 +08:00
|
|
|
assert rc == 0
|
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_failing_file_ignore_all(tmpdir):
|
|
|
|
f = tmpdir.join('f.py')
|
|
|
|
f.write(BUILTIN_CONSTRUCTORS)
|
2020-05-21 00:07:45 +08:00
|
|
|
rc = main(['--ignore=complex,dict,float,int,list,str,tuple', str(f)])
|
2017-11-26 08:17:47 +08:00
|
|
|
assert rc == 0
|