From 4ceed0b95803abd999da7d89df2821346a94571a Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 1 Jan 2024 17:36:49 -0800 Subject: [PATCH] Remove is_function_or_class helper footgun (#4133) This is a no-op change. That function was not a good way to tell if something is a function or a class, since it basically only worked for async functions by accident (the parent of a suite / simple_stmt of async function body is a funcdef). --- src/black/linegen.py | 4 ++-- src/black/nodes.py | 9 +++++++- .../cases/preview_dummy_implementations.py | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 0972cf4..574c89b 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -42,12 +42,12 @@ is_atom_with_invisible_parens, is_docstring, is_empty_tuple, - is_function_or_class, is_lpar_token, is_multiline_string, is_name_token, is_one_sequence_between, is_one_tuple, + is_parent_function_or_class, is_rpar_token, is_stub_body, is_stub_suite, @@ -302,7 +302,7 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]: if node.parent and node.parent.type in STATEMENT: if Preview.dummy_implementations in self.mode: - condition = is_function_or_class(node.parent) + condition = is_parent_function_or_class(node) else: condition = self.mode.is_pyi if condition and is_stub_body(node): diff --git a/src/black/nodes.py b/src/black/nodes.py index d45d40c..7ee2df2 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -742,6 +742,13 @@ def is_multiline_string(leaf: Leaf) -> bool: return has_triple_quotes(leaf.value) and "\n" in leaf.value +def is_parent_function_or_class(node: Node) -> bool: + assert node.type in {syms.suite, syms.simple_stmt} + assert node.parent is not None + # Note this works for suites / simple_stmts in async def as well + return node.parent.type in {syms.funcdef, syms.classdef} + + def is_function_or_class(node: Node) -> bool: return node.type in {syms.funcdef, syms.classdef, syms.async_funcdef} @@ -751,7 +758,7 @@ def is_stub_suite(node: Node, mode: Mode) -> bool: if ( node.parent is not None and Preview.dummy_implementations in mode - and not is_function_or_class(node.parent) + and not is_parent_function_or_class(node) ): return False diff --git a/tests/data/cases/preview_dummy_implementations.py b/tests/data/cases/preview_dummy_implementations.py index 28b23bb..3cd392c 100644 --- a/tests/data/cases/preview_dummy_implementations.py +++ b/tests/data/cases/preview_dummy_implementations.py @@ -58,6 +58,17 @@ def has_comment(): if already_dummy: ... +class AsyncCls: + async def async_method(self): + ... + +async def async_function(self): + ... + +@decorated +async def async_function(self): + ... + # output from typing import NoReturn, Protocol, Union, overload @@ -121,3 +132,14 @@ def has_comment(): ... # still a dummy if already_dummy: ... + + +class AsyncCls: + async def async_method(self): ... + + +async def async_function(self): ... + + +@decorated +async def async_function(self): ...