Skip to content

Commit 815b102

Browse files
authored
Merge pull request #776 from hhatto/fix/e721-with-indent
Fix E721 type comparison pattern handling
2 parents a70fa24 + 3b9d399 commit 815b102

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

autopep8.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,7 @@ def fix_e721(self, result):
13001300
# NOTE: match objects
13011301
# * type(a) == type(b) -> (None, None, 'a', '==')
13021302
# * str == type(b) -> ('==', 'b', None, None)
1303+
# * type(b) == str -> (None, None, 'b', '==')
13031304
# * type("") != type(b) -> (None, None, '""', '!=')
13041305
start = match.start()
13051306
end = match.end()
@@ -1320,6 +1321,10 @@ def fix_e721(self, result):
13201321
isinstance_stmt = " not isinstance"
13211322

13221323
_type_comp = f"{_target_obj}, {target[:start]}"
1324+
indent_match = re.match(r'^\s+', target)
1325+
indent = ""
1326+
if indent_match:
1327+
indent = indent_match.group()
13231328

13241329
_prefix_tmp = target[:start].split()
13251330
if len(_prefix_tmp) >= 1:
@@ -1332,15 +1337,28 @@ def fix_e721(self, result):
13321337

13331338
_suffix_tmp = target[end:]
13341339
_suffix_type_match = TYPE_REGEX.search(_suffix_tmp)
1335-
if len(_suffix_tmp.split()) >= 1 and _suffix_type_match:
1336-
if _suffix_type_match:
1340+
if _suffix_type_match:
1341+
if len(_suffix_tmp.split()) >= 1:
13371342
type_match_end = _suffix_type_match.end()
13381343
_suffix = _suffix_tmp[type_match_end:]
1339-
if _suffix_type_match:
13401344
cmp_b = _suffix_type_match.groups()[0]
13411345
_type_comp = f"{_target_obj}, {cmp_b}"
1342-
1343-
fix_line = f"{_prefix}{isinstance_stmt}({_type_comp}){_suffix}"
1346+
else:
1347+
_else_suffix_match = re.match(
1348+
r"^\s*([^\s:]+)(.*)$",
1349+
_suffix_tmp,
1350+
)
1351+
if _else_suffix_match:
1352+
_else_suffix = _else_suffix_match.group(1)
1353+
_else_suffix_other = _else_suffix_match.group(2)
1354+
_type_comp = f"{_target_obj}, {_else_suffix}"
1355+
_else_suffix_end = _suffix_tmp[_else_suffix_match.end():]
1356+
_suffix = f"{_else_suffix_other}{_else_suffix_end}"
1357+
# `else` route is not care
1358+
1359+
fix_line = (
1360+
f"{indent}{_prefix}{isinstance_stmt}({_type_comp}){_suffix}"
1361+
)
13441362
self.source[line_index] = fix_line
13451363

13461364
def fix_e722(self, result):

test/test_autopep8.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4419,6 +4419,18 @@ def test_e721_in_conditional_pat2(self):
44194419
with autopep8_context(line, options=['--aggressive']) as result:
44204420
self.assertEqual(fixed, result)
44214421

4422+
def test_e721_in_conditional_pat3(self):
4423+
line = "if type(res) == str:\n pass\n"
4424+
fixed = "if isinstance(res, str):\n pass\n"
4425+
with autopep8_context(line, options=['--aggressive']) as result:
4426+
self.assertEqual(fixed, result)
4427+
4428+
def test_e721_in_conditional_with_indent(self):
4429+
line = "if True:\n if str == type(''):\n pass\n"
4430+
fixed = "if True:\n if isinstance('', str):\n pass\n"
4431+
with autopep8_context(line, options=['--aggressive']) as result:
4432+
self.assertEqual(fixed, result)
4433+
44224434
def test_e721_in_not_conditional(self):
44234435
line = "if type(res) != type(''):\n pass\n"
44244436
fixed = "if not isinstance(res, type('')):\n pass\n"

0 commit comments

Comments
 (0)