-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #898 from asottile/constant-fold-types
constant fold isinstance / issubclass / except
- Loading branch information
Showing
7 changed files
with
206 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
import ast | ||
from typing import Iterable | ||
|
||
from tokenize_rt import Offset | ||
|
||
from pyupgrade._ast_helpers import ast_to_offset | ||
from pyupgrade._ast_helpers import is_type_check | ||
from pyupgrade._data import register | ||
from pyupgrade._data import State | ||
from pyupgrade._data import TokenFunc | ||
from pyupgrade._token_helpers import constant_fold_tuple | ||
|
||
|
||
def _to_name(node: ast.AST) -> str | None: | ||
if isinstance(node, ast.Name): | ||
return node.id | ||
elif isinstance(node, ast.Attribute): | ||
base = _to_name(node.value) | ||
if base is None: | ||
return None | ||
else: | ||
return f'{base}.{node.attr}' | ||
else: | ||
return None | ||
|
||
|
||
def _can_constant_fold(node: ast.Tuple) -> bool: | ||
seen = set() | ||
for el in node.elts: | ||
name = _to_name(el) | ||
if name is not None: | ||
if name in seen: | ||
return True | ||
else: | ||
seen.add(name) | ||
else: | ||
return False | ||
|
||
|
||
def _cbs(node: ast.AST | None) -> Iterable[tuple[Offset, TokenFunc]]: | ||
if isinstance(node, ast.Tuple) and _can_constant_fold(node): | ||
yield ast_to_offset(node), constant_fold_tuple | ||
|
||
|
||
@register(ast.Call) | ||
def visit_Call( | ||
state: State, | ||
node: ast.Call, | ||
parent: ast.AST, | ||
) -> Iterable[tuple[Offset, TokenFunc]]: | ||
if is_type_check(node): | ||
yield from _cbs(node.args[1]) | ||
|
||
|
||
@register(ast.Try) | ||
def visit_Try( | ||
state: State, | ||
node: ast.Try, | ||
parent: ast.AST, | ||
) -> Iterable[tuple[Offset, TokenFunc]]: | ||
for handler in node.handlers: | ||
yield from _cbs(handler.type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from pyupgrade._data import Settings | ||
from pyupgrade._main import _fix_plugins | ||
|
||
|
||
@pytest.mark.parametrize( | ||
's', | ||
( | ||
pytest.param( | ||
'isinstance(x, str)', | ||
id='isinstance nothing duplicated', | ||
), | ||
pytest.param( | ||
'issubclass(x, str)', | ||
id='issubclass nothing duplicated', | ||
), | ||
pytest.param( | ||
'try: ...\n' | ||
'except Exception: ...\n', | ||
id='try-except nothing duplicated', | ||
), | ||
pytest.param( | ||
'isinstance(x, (str, (str,)))', | ||
id='only consider flat tuples', | ||
), | ||
pytest.param( | ||
'isinstance(x, (f(), a().g))', | ||
id='only consider names and dotted names', | ||
), | ||
), | ||
) | ||
def test_constant_fold_noop(s): | ||
assert _fix_plugins(s, settings=Settings()) == s | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('s', 'expected'), | ||
( | ||
pytest.param( | ||
'isinstance(x, (str, str, int))', | ||
'isinstance(x, (str, int))', | ||
id='isinstance', | ||
), | ||
pytest.param( | ||
'issubclass(x, (str, str, int))', | ||
'issubclass(x, (str, int))', | ||
id='issubclass', | ||
), | ||
pytest.param( | ||
'try: ...\n' | ||
'except (Exception, Exception, TypeError): ...\n', | ||
'try: ...\n' | ||
'except (Exception, TypeError): ...\n', | ||
id='except', | ||
), | ||
pytest.param( | ||
'isinstance(x, (str, str))', | ||
'isinstance(x, str)', | ||
id='folds to 1', | ||
), | ||
pytest.param( | ||
'isinstance(x, (a.b, a.b, a.c))', | ||
'isinstance(x, (a.b, a.c))', | ||
id='folds dotted names', | ||
), | ||
pytest.param( | ||
'try: ...\n' | ||
'except(a, a): ...\n', | ||
'try: ...\n' | ||
'except a: ...\n', | ||
id='deduplication to 1 does not cause syntax error with except', | ||
), | ||
), | ||
) | ||
def test_constant_fold(s, expected): | ||
assert _fix_plugins(s, settings=Settings()) == expected |