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
|
from __future__ import annotations
import pytest
from pyupgrade._data import Settings
from pyupgrade._main import _fix_plugins
@pytest.mark.parametrize(
's',
(
# renaming things for weird reasons
'from six import MAXSIZE as text_type\n'
'isinstance(s, text_type)\n',
# parenthesized part of attribute
'(\n'
' six\n'
').text_type(u)\n',
pytest.param(
'from .six import text_type\n'
'isinstance("foo", text_type)\n',
id='relative import might not be six',
),
pytest.param(
'foo.range(3)',
id='Range, but not from six.moves',
),
),
)
def test_six_simple_noop(s):
assert _fix_plugins(s, settings=Settings()) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
(
'isinstance(s, six.text_type)',
'isinstance(s, str)',
),
pytest.param(
'isinstance(s, six . string_types)',
'isinstance(s, str)',
id='weird spacing on six.attr',
),
(
'isinstance(s, six.string_types)',
'isinstance(s, str)',
),
(
'issubclass(tp, six.string_types)',
'issubclass(tp, str)',
),
(
'STRING_TYPES = six.string_types',
'STRING_TYPES = (str,)',
),
(
'from six import string_types\n'
'isinstance(s, string_types)\n',
'from six import string_types\n'
'isinstance(s, str)\n',
),
(
'from six import string_types\n'
'STRING_TYPES = string_types\n',
'from six import string_types\n'
'STRING_TYPES = (str,)\n',
),
pytest.param(
'six.moves.range(3)\n',
'range(3)\n',
id='six.moves.range',
),
pytest.param(
'six.moves.xrange(3)\n',
'range(3)\n',
id='six.moves.xrange',
),
pytest.param(
'from six.moves import xrange\n'
'xrange(3)\n',
'from six.moves import xrange\n'
'range(3)\n',
id='six.moves.xrange, from import',
),
),
)
def test_fix_six_simple(s, expected):
ret = _fix_plugins(s, settings=Settings())
assert ret == expected
|