diff --git a/autopep8.py b/autopep8.py index 8b44a1bb..f906383c 100755 --- a/autopep8.py +++ b/autopep8.py @@ -1943,11 +1943,20 @@ def _shorten_line(tokens, source, indentation, indent_word, Multiple candidates will be yielded. """ + in_string = False for (token_type, token_string, start_offset, end_offset) in token_offsets(tokens): + if IS_SUPPORT_TOKEN_FSTRING: + if token_type == tokenize.FSTRING_START: + in_string = True + elif token_type == tokenize.FSTRING_END: + in_string = False + if in_string: + continue + if ( token_type == tokenize.COMMENT and not is_probably_part_of_multiline(previous_line) and @@ -4091,7 +4100,7 @@ def read_pyproject_toml(args, parser): if config.get("tool", {}).get("autopep8") is None: return None - config = config.get("tool").get("autopep8") + config = config.get("tool", {}).get("autopep8") defaults = {} option_list = {o.dest: o.type or type(o.default) diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 05c396df..19ff2b15 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -3941,6 +3941,19 @@ def test_e501_with_pep572_assignment_expressions(self): with autopep8_context(line, options=['-aa']) as result: self.assertEqual(line, result) + def test_e501_effected_with_fstring(self): + line = """\ +def foo(): + logger.info(f"some string padding some string padding some string padd {somedict['key1']}, more padding more paddin #: {somedict['dictkey2']}") +""" + fixed = """\ +def foo(): + logger.info( + f"some string padding some string padding some string padd {somedict['key1']}, more padding more paddin #: {somedict['dictkey2']}") +""" + with autopep8_context(line) as result: + self.assertEqual(fixed, result) + def test_e501_not_effected_with_fstring(self): line = """\ connector = f"socks5://{user}:{password}:{url}:{port}"