Skip to content

Commit 7758676

Browse files
committed
check-builtin-literals: Ignore function attribute calls
1 parent 3ab8c71 commit 7758676

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Add this to your `.pre-commit-config.yaml`
3232
- `check-ast` - Simply check whether files parse as valid python.
3333
- `check-builtin-literals` - Require literal syntax when initializing empty or zero Python builtin types.
3434
- Allows calling constructors with positional arguments (e.g., `list('abc')`).
35+
- Allows calling constructors from the `builtins` (`__builtin__`) namespace (`builtins.list()`).
3536
- Ignore this requirement for specific builtin types with `--ignore=type1,type2,…`.
3637
- Forbid `dict` keyword syntax with `--no-allow-dict-kwargs`.
3738
- `check-byte-order-marker` - Forbid files which have a UTF-8 byte-order marker

pre_commit_hooks/check_builtin_literals.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def _check_dict_call(self, node):
3030
return self.allow_dict_kwargs and (getattr(node, 'kwargs', None) or getattr(node, 'keywords', None))
3131

3232
def visit_Call(self, node):
33+
if isinstance(node.func, ast.Attribute):
34+
# Ignore functions that are object attributes (`foo.bar()`).
35+
# Assume that if the user calls `builtins.list()`, they know what
36+
# they're doing.
37+
return
3338
if node.func.id not in set(BUILTIN_TYPES).difference(self.ignore):
3439
return
3540
if node.func.id == 'dict' and self._check_dict_call(node):
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
# flake8 checks imports; `builtins` does not exist in Python 2.
2+
# flake8: noqa
3+
14
c1 = complex()
25
d1 = dict()
36
f1 = float()
47
i1 = int()
58
l1 = list()
69
s1 = str()
710
t1 = tuple()
11+
12+
c2 = builtins.complex()
13+
d2 = builtins.dict()
14+
f2 = builtins.float()
15+
i2 = builtins.int()
16+
l2 = builtins.list()
17+
s2 = builtins.str()
18+
t2 = builtins.tuple()

tests/check_builtin_literals_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,36 @@ def visitor():
2121
("complex()", [BuiltinTypeCall('complex', 1, 0)]),
2222
("complex(0, 0)", []),
2323
("complex('0+0j')", []),
24+
('builtins.complex()', []),
2425
# float
2526
("0.0", []),
2627
("float()", [BuiltinTypeCall('float', 1, 0)]),
2728
("float('0.0')", []),
29+
('builtins.float()', []),
2830
# int
2931
("0", []),
3032
("int()", [BuiltinTypeCall('int', 1, 0)]),
3133
("int('0')", []),
34+
('builtins.int()', []),
3235
# list
3336
("[]", []),
3437
("list()", [BuiltinTypeCall('list', 1, 0)]),
3538
("list('abc')", []),
3639
("list([c for c in 'abc'])", []),
3740
("list(c for c in 'abc')", []),
41+
('builtins.list()', []),
3842
# str
3943
("''", []),
4044
("str()", [BuiltinTypeCall('str', 1, 0)]),
4145
("str('0')", []),
42-
("[]", []),
46+
('builtins.str()', []),
4347
# tuple
4448
("()", []),
4549
("tuple()", [BuiltinTypeCall('tuple', 1, 0)]),
4650
("tuple('abc')", []),
4751
("tuple([c for c in 'abc'])", []),
4852
("tuple(c for c in 'abc')", []),
53+
('builtins.tuple()', []),
4954
],
5055
)
5156
def test_non_dict_exprs(visitor, expression, calls):
@@ -62,6 +67,7 @@ def test_non_dict_exprs(visitor, expression, calls):
6267
("dict(**{'a': 1, 'b': 2, 'c': 3})", []),
6368
("dict([(k, v) for k, v in [('a', 1), ('b', 2), ('c', 3)]])", []),
6469
("dict((k, v) for k, v in [('a', 1), ('b', 2), ('c', 3)])", []),
70+
('builtins.dict()', []),
6571
],
6672
)
6773
def test_dict_allow_kwargs_exprs(visitor, expression, calls):
@@ -75,6 +81,7 @@ def test_dict_allow_kwargs_exprs(visitor, expression, calls):
7581
("dict()", [BuiltinTypeCall('dict', 1, 0)]),
7682
("dict(a=1, b=2, c=3)", [BuiltinTypeCall('dict', 1, 0)]),
7783
("dict(**{'a': 1, 'b': 2, 'c': 3})", [BuiltinTypeCall('dict', 1, 0)]),
84+
('builtins.dict()', []),
7885
],
7986
)
8087
def test_dict_no_allow_kwargs_exprs(expression, calls):

0 commit comments

Comments
 (0)