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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
from __future__ import annotations
import pytest
from pyupgrade._data import Settings
from pyupgrade._main import _fix_plugins
@pytest.mark.parametrize(
's',
(
pytest.param(
'try: ...\n'
'except Exception:\n'
' raise',
id='empty raise',
),
pytest.param(
'try: ...\n'
'except: ...\n',
id='empty try-except',
),
pytest.param(
'try: ...\n'
'except AssertionError: ...\n',
id='unrelated exception type as name',
),
pytest.param(
'try: ...\n'
'except (AssertionError,): ...\n',
id='unrelated exception type as tuple',
),
pytest.param(
'try: ...\n'
'except OSError: ...\n',
id='already rewritten name',
),
pytest.param(
'try: ...\n'
'except (TypeError, OSError): ...\n',
id='already rewritten tuple',
),
pytest.param(
'from .os import error\n'
'raise error(1)\n',
id='same name as rewrite but relative import',
),
pytest.param(
'from os import error\n'
'def f():\n'
' error = 3\n'
' return error\n',
id='not rewriting outside of raise or except',
),
pytest.param(
'from os import error as the_roof\n'
'raise the_roof()\n',
id='ignoring imports with aliases',
),
# TODO: could probably rewrite these but leaving for now
pytest.param(
'import os\n'
'try: ...\n'
'except (os).error: ...\n',
id='weird parens',
),
),
)
def test_fix_exceptions_noop(s):
assert _fix_plugins(s, settings=Settings()) == s
@pytest.mark.parametrize(
('s', 'version'),
(
pytest.param(
'raise socket.timeout()',
(3, 9),
id='raise socket.timeout is noop <3.10',
),
pytest.param(
'try: ...\n'
'except socket.timeout: ...\n',
(3, 9),
id='except socket.timeout is noop <3.10',
),
pytest.param(
'raise asyncio.TimeoutError()',
(3, 10),
id='raise asyncio.TimeoutError() is noop <3.11',
),
pytest.param(
'try: ...\n'
'except asyncio.TimeoutError: ...\n',
(3, 10),
id='except asyncio.TimeoutError() is noop <3.11',
),
),
)
def test_fix_exceptions_version_specific_noop(s, version):
assert _fix_plugins(s, settings=Settings(min_version=version)) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param(
'raise mmap.error(1)\n',
'raise OSError(1)\n',
id='mmap.error',
),
pytest.param(
'raise os.error(1)\n',
'raise OSError(1)\n',
id='os.error',
),
pytest.param(
'raise select.error(1)\n',
'raise OSError(1)\n',
id='select.error',
),
pytest.param(
'raise socket.error(1)\n',
'raise OSError(1)\n',
id='socket.error',
),
pytest.param(
'raise IOError(1)\n',
'raise OSError(1)\n',
id='IOError',
),
pytest.param(
'raise EnvironmentError(1)\n',
'raise OSError(1)\n',
id='EnvironmentError',
),
pytest.param(
'raise WindowsError(1)\n',
'raise OSError(1)\n',
id='WindowsError',
),
pytest.param(
'raise os.error\n',
'raise OSError\n',
id='raise exception type without call',
),
pytest.param(
'from os import error\n'
'raise error(1)\n',
'from os import error\n'
'raise OSError(1)\n',
id='raise via from import',
),
pytest.param(
'try: ...\n'
'except WindowsError: ...\n',
'try: ...\n'
'except OSError: ...\n',
id='except of name',
),
pytest.param(
'try: ...\n'
'except os.error: ...\n',
'try: ...\n'
'except OSError: ...\n',
id='except of dotted name',
),
pytest.param(
'try: ...\n'
'except (WindowsError,): ...\n',
'try: ...\n'
'except OSError: ...\n',
id='except of name in tuple',
),
pytest.param(
'try: ...\n'
'except (os.error,): ...\n',
'try: ...\n'
'except OSError: ...\n',
id='except of dotted name in tuple',
),
pytest.param(
'try: ...\n'
'except (WindowsError, KeyError, OSError): ...\n',
'try: ...\n'
'except (OSError, KeyError): ...\n',
id='deduplicates exception types',
),
pytest.param(
'try: ...\n'
'except (os.error, WindowsError, OSError): ...\n',
'try: ...\n'
'except OSError: ...\n',
id='deduplicates to a single type',
),
pytest.param(
'try: ...\n'
'except(os.error, WindowsError, OSError): ...\n',
'try: ...\n'
'except OSError: ...\n',
id='deduplicates to a single type without whitespace',
),
pytest.param(
'from wat import error\n'
'try: ...\n'
'except (WindowsError, error): ...\n',
'from wat import error\n'
'try: ...\n'
'except (OSError, error): ...\n',
id='leave unrelated error names alone',
),
pytest.param(
'try: ...\n'
'except (\n'
' BaseException,\n'
' BaseException # b\n'
'): ...\n',
'try: ...\n'
'except BaseException: ...\n',
id='dedupe with comment. see #932',
),
pytest.param(
'try: ...\n'
'except (\n'
' A, A,\n'
' B # b\n'
'): ...\n',
'try: ...\n'
'except (A, B): ...\n',
id='dedupe other exception, one contains comment. see #932',
),
),
)
def test_fix_exceptions(s, expected):
assert _fix_plugins(s, settings=Settings()) == expected
@pytest.mark.parametrize(
('s', 'expected', 'version'),
(
pytest.param(
'raise socket.timeout(1)\n',
'raise TimeoutError(1)\n',
(3, 10),
id='socket.timeout',
),
pytest.param(
'raise asyncio.TimeoutError(1)\n',
'raise TimeoutError(1)\n',
(3, 11),
id='asyncio.TimeoutError',
),
),
)
def test_fix_exceptions_versioned(s, expected, version):
assert _fix_plugins(s, settings=Settings(min_version=version)) == expected
def test_can_rewrite_disparate_names():
s = '''\
try: ...
except (asyncio.TimeoutError, WindowsError): ...
'''
expected = '''\
try: ...
except (TimeoutError, OSError): ...
'''
assert _fix_plugins(s, settings=Settings(min_version=(3, 11))) == expected
|