File: mutable_defaults_test.py

package info (click to toggle)
flake8-mutable 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 104 kB
  • sloc: python: 163; makefile: 2
file content (101 lines) | stat: -rw-r--r-- 2,408 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import ast
import pytest

from mutable_defaults import MutableDefaultChecker


@pytest.mark.parametrize('code,error_count', [
    ('def foo(bar={}): pass', 1),
    ('def foo(bar=[]): pass', 1),
    ('def foo(): pass', 0),
    ('def foo(a, b, c=False): pass', 0),
    (
        '\n'.join([
            'def foo():',
            '    def bar(a={}):',
            '        pass',
            '    pass',
        ]),
        1
    ),
    ('def foo(bar=[], baz={}): pass', 2),
    ('def foo(bar=range(10)): pass', 1),
    ('def foo(bar=set()): pass', 1),
    (
        '\n'.join([
            'def foo():',
            '    return True',
            'def bar(a=foo()):',
            '    pass',
        ]),
        1
    ),
    ('', 0),
], ids=(
    'dict',
    'list',
    'empty_arg_list',
    'valid_args_and_kwargs',
    'nested_function',
    'multiple_mutable_defaults',
    'builtin_callable_with_value',
    'builtin_callable_without_value',
    'user_defined_callable',
    'empty_tree',
))
def test_mutable_defaults(code, error_count):
    tree = ast.parse(code)
    assert (
        len(list(MutableDefaultChecker(tree, 'filename').run())) ==
        error_count
    )


@pytest.mark.parametrize('code,location_list', [
    ('def foo(bar={}): pass', [(1, 12)]),
    ('def foo(bar=[]): pass', [(1, 12)]),
    (
        '\n'.join([
            'def foo():',
            '    def bar(a={}):',
            '        pass',
            '    pass',
        ]),
        [(2, 14)]
    ),
    ('def foo(bar=[], baz={}): pass', [(1, 12), (1, 20)]),
    ('def foo(bar=range(10)): pass', [(1, 12)]),
    ('def foo(bar=set()): pass', [(1, 12)]),
    (
        '\n'.join([
            'def foo():',
            '    return True',
            'def bar(a=foo()):',
            '    pass',
        ]),
        [(3, 10)]
    ),
    (
        '\n'.join([
            'def foo(bar=[],',
            '        baz={}):',
            '    pass',
        ]),
        [(1, 12), (2, 12)]
    ),
], ids=(
    'dict',
    'list',
    'nested_function',
    'multiple_mutable_defaults',
    'builtin_callable_with_value',
    'builtin_callable_without_value',
    'user_defined_callable',
    'multiple_line_function_def',
))
def test_mutable_defaults_location(code, location_list):
    tree = ast.parse(code)
    assert (
        location_list ==
        [(e[0], e[1]) for e in MutableDefaultChecker(tree, 'filename').run()]
    )