Skip to content

Commit

Permalink
EHH: simplify logic in versioned_branches.py
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Sep 11, 2021
1 parent 9cca9f7 commit cf01e9f
Showing 1 changed file with 21 additions and 63 deletions.
84 changes: 21 additions & 63 deletions pyupgrade/_plugins/versioned_branches.py
Expand Up @@ -106,13 +106,16 @@ def visit_If(
node: ast.If,
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if state.settings.min_version >= (3,) and (
len(state.settings.min_version) >= 2
):
py3_minor = state.settings.min_version[1]

min_version: Tuple[int, ...]
if state.settings.min_version == (3,):
min_version = (3, 0)
else:
min_version = state.settings.min_version
assert len(min_version) >= 2

if (
state.settings.min_version >= (3,) and (
min_version >= (3,) and (
# if six.PY2:
is_name_attr(node.test, state.from_imports, 'six', ('PY2',)) or
# if not six.PY3:
Expand All @@ -127,6 +130,7 @@ def visit_If(
)
) or
# sys.version_info == 2 or < (3,)
# or < (3, n) or <= (3, n) (with n<m)
(
isinstance(node.test, ast.Compare) and
is_name_attr(
Expand All @@ -137,33 +141,10 @@ def visit_If(
) and
len(node.test.ops) == 1 and (
_eq(node.test, 2) or
_compare_to_3(node.test, ast.Lt)
)
) or
# sys.version_info < (3, n) (with n<=m)
# or sys.version_info <= (3, n) (with n<m)
(
len(state.settings.min_version) >= 2 and (
isinstance(node.test, ast.Compare) and
is_name_attr(
node.test.left,
state.from_imports,
'sys',
('version_info',),
) and
len(node.test.ops) == 1 and (
_compare_to_3(
node.test,
ast.Lt,
minor=py3_minor,
) or any(
_compare_to_3(
node.test,
(ast.Lt, ast.LtE),
minor=minor,
)
for minor in range(py3_minor)
)
_compare_to_3(node.test, ast.Lt, min_version[1]) or
any(
_compare_to_3(node.test, (ast.Lt, ast.LtE), minor)
for minor in range(min_version[1])
)
)
)
Expand All @@ -172,7 +153,7 @@ def visit_If(
if node.orelse and not isinstance(node.orelse[0], ast.If):
yield ast_to_offset(node), _fix_py2_block
elif (
state.settings.min_version >= (3,) and (
min_version >= (3,) and (
# if six.PY3:
is_name_attr(node.test, state.from_imports, 'six', ('PY3',)) or
# if not six.PY2:
Expand All @@ -187,6 +168,8 @@ def visit_If(
)
) or
# sys.version_info == 3 or >= (3,) or > (3,)
# sys.version_info >= (3, n) (with n<=m)
# or sys.version_info > (3, n) (with n<m)
(
isinstance(node.test, ast.Compare) and
is_name_attr(
Expand All @@ -197,37 +180,12 @@ def visit_If(
) and
len(node.test.ops) == 1 and (
_eq(node.test, 3) or
_compare_to_3(node.test, (ast.Gt, ast.GtE))
)
) or
# sys.version_info >= (3, n) (with n<=m)
# or sys.version_info > (3, n) (with n<m)
(
len(state.settings.min_version) >= 2 and (

isinstance(node.test, ast.Compare) and
is_name_attr(
node.test.left,
state.from_imports,
'sys',
('version_info',),
) and
len(node.test.ops) == 1 and
(
_compare_to_3(
node.test,
ast.GtE,
minor=py3_minor,
) or any(
_compare_to_3(
node.test,
(ast.Gt, ast.GtE),
minor=minor,
)
for minor in range(py3_minor)
)
_compare_to_3(node.test, (ast.Gt, ast.GtE)) or
_compare_to_3(node.test, ast.GtE, min_version[1]) or
any(
_compare_to_3(node.test, (ast.Gt, ast.GtE), minor)
for minor in range(min_version[1])
)

)
)
)
Expand Down

0 comments on commit cf01e9f

Please sign in to comment.