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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
from __future__ import annotations
import pytest
from pyupgrade._data import Settings
from pyupgrade._main import _fix_plugins
@pytest.mark.parametrize(
('s',),
(
pytest.param(
'from collections import defaultdict as dd\n\n'
'dd(lambda: set())\n',
id='not following as imports',
),
pytest.param(
'from collections2 import defaultdict\n\n'
'dd(lambda: dict())\n',
id='not following unknown import',
),
pytest.param(
'from .collections import defaultdict\n'
'defaultdict(lambda: list())\n',
id='relative imports',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: {1}))\n',
id='non empty set',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: [1]))\n'
'defaultdict(lambda: list([1])))\n',
id='non empty list',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: {1: 2})\n',
id='non empty dict, literal',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: dict([(1,2),])))\n',
id='non empty dict, call with args',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: dict(a=[1]))\n',
id='non empty dict, call with kwargs',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: (1,))\n',
id='non empty tuple, literal',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: tuple([1]))\n',
id='non empty tuple, calls with arg',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: "AAA")\n'
'defaultdict(lambda: \'BBB\')\n',
id='non empty string',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: 10)\n'
'defaultdict(lambda: -2)\n',
id='non zero integer',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: 0.2)\n'
'defaultdict(lambda: 0.00000001)\n'
'defaultdict(lambda: -2.3)\n',
id='non zero float',
),
pytest.param(
'import collections\n'
'collections.defaultdict(lambda: None)\n',
id='lambda: None is not equivalent to defaultdict()',
),
),
)
def test_fix_noop(s):
assert _fix_plugins(s, settings=Settings()) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: set())\n',
'from collections import defaultdict\n\n'
'defaultdict(set)\n',
id='call with attr, set()',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: list())\n',
'from collections import defaultdict\n\n'
'defaultdict(list)\n',
id='call with attr, list()',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: dict())\n',
'from collections import defaultdict\n\n'
'defaultdict(dict)\n',
id='call with attr, dict()',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: tuple())\n',
'from collections import defaultdict\n\n'
'defaultdict(tuple)\n',
id='call with attr, tuple()',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: [])\n',
'from collections import defaultdict\n\n'
'defaultdict(list)\n',
id='call with attr, []',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: {})\n',
'from collections import defaultdict\n\n'
'defaultdict(dict)\n',
id='call with attr, {}',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: ())\n',
'from collections import defaultdict\n\n'
'defaultdict(tuple)\n',
id='call with attr, ()',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: "")\n',
'from collections import defaultdict\n\n'
'defaultdict(str)\n',
id='call with attr, empty string (double quote)',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: \'\')\n',
'from collections import defaultdict\n\n'
'defaultdict(str)\n',
id='call with attr, empty string (single quote)',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: 0)\n',
'from collections import defaultdict\n\n'
'defaultdict(int)\n',
id='call with attr, int',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: 0.0)\n',
'from collections import defaultdict\n\n'
'defaultdict(float)\n',
id='call with attr, float',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: 0.0000)\n',
'from collections import defaultdict\n\n'
'defaultdict(float)\n',
id='call with attr, long float',
),
pytest.param(
'from collections import defaultdict\n\n'
'defaultdict(lambda: [], {1: []})\n',
'from collections import defaultdict\n\n'
'defaultdict(list, {1: []})\n',
id='defauldict with kwargs',
),
pytest.param(
'import collections\n\n'
'collections.defaultdict(lambda: set())\n'
'collections.defaultdict(lambda: list())\n'
'collections.defaultdict(lambda: dict())\n'
'collections.defaultdict(lambda: tuple())\n'
'collections.defaultdict(lambda: [])\n'
'collections.defaultdict(lambda: {})\n'
'collections.defaultdict(lambda: "")\n'
'collections.defaultdict(lambda: \'\')\n'
'collections.defaultdict(lambda: 0)\n'
'collections.defaultdict(lambda: 0.0)\n'
'collections.defaultdict(lambda: 0.00000)\n'
'collections.defaultdict(lambda: 0j)\n',
'import collections\n\n'
'collections.defaultdict(set)\n'
'collections.defaultdict(list)\n'
'collections.defaultdict(dict)\n'
'collections.defaultdict(tuple)\n'
'collections.defaultdict(list)\n'
'collections.defaultdict(dict)\n'
'collections.defaultdict(str)\n'
'collections.defaultdict(str)\n'
'collections.defaultdict(int)\n'
'collections.defaultdict(float)\n'
'collections.defaultdict(float)\n'
'collections.defaultdict(complex)\n',
id='call with attr',
),
),
)
def test_fix_defaultdict(s, expected):
ret = _fix_plugins(s, settings=Settings())
assert ret == expected
|