diff --git a/src/docformatter/classify.py b/src/docformatter/classify.py index 9b8b553..f0db043 100644 --- a/src/docformatter/classify.py +++ b/src/docformatter/classify.py @@ -446,7 +446,7 @@ def is_nested_definition_line(token: tokenize.TokenInfo) -> bool: bool True if the token is a nested definition line, False otherwise. """ - return re.match(r"^ {4,}(async|class|def) ", token.line) is not None + return re.match(r"^ {4,}(?:async def|class|def) ", token.line) is not None def is_newline_continuation( diff --git a/tests/_data/string_files/classify_functions.toml b/tests/_data/string_files/classify_functions.toml index fc40460..6ef9726 100644 --- a/tests/_data/string_files/classify_functions.toml +++ b/tests/_data/string_files/classify_functions.toml @@ -122,6 +122,14 @@ end = "(3,25)" line = ' async def foo():\n' expected = true +[is_not_nested_definition_line_async_with] +type = 1 +string = ' async with lock:\n' +start = "(3,10)" +end = "(3,30)" +line = ' async with lock:\n' +expected = false + [is_not_nested_definition_line_function] type = 1 string = ' definitely\n' diff --git a/tests/_data/string_files/do_format_code.toml b/tests/_data/string_files/do_format_code.toml index d652a15..64c9291 100644 --- a/tests/_data/string_files/do_format_code.toml +++ b/tests/_data/string_files/do_format_code.toml @@ -1245,6 +1245,18 @@ expected='''"""A.""" pass ''' +[issue_351] +source='''async def example() -> None: + """Example.""" + async with lock: + pass +''' +expected='''async def example() -> None: + """Example.""" + async with lock: + pass +''' + # A blank line with trailing whitespace must not crash the tokenizer round-trip. # The "\n" escapes are used so the four trailing spaces on the blank line are # explicit and cannot be stripped by editors or formatters. diff --git a/tests/formatter/test_do_format_code.py b/tests/formatter/test_do_format_code.py index 037ace1..4cd7a71 100644 --- a/tests/formatter/test_do_format_code.py +++ b/tests/formatter/test_do_format_code.py @@ -141,6 +141,7 @@ ("do_not_break_f_string_double_quotes", NO_ARGS), ("do_not_break_f_string_single_quotes", NO_ARGS), ("issue_331_black_module_docstring", ["--black", ""]), + ("issue_351", NO_ARGS), ("issue_355", NO_ARGS), ("issue_360_no_trailing_newline", NO_ARGS), ], diff --git a/tests/test_classify_functions.py b/tests/test_classify_functions.py index d959bc2..55f8e6e 100644 --- a/tests/test_classify_functions.py +++ b/tests/test_classify_functions.py @@ -144,6 +144,7 @@ def test_is_not_attribute_docstring_inside_brackets(source): ("is_nested_definition_line_class", is_nested_definition_line), ("is_nested_definition_line_function", is_nested_definition_line), ("is_nested_definition_line_async_function", is_nested_definition_line), + ("is_not_nested_definition_line_async_with", is_nested_definition_line), ("is_not_nested_definition_line_function", is_nested_definition_line), ("is_newline_continuation", is_newline_continuation), ("is_string_variable", is_string_variable),