From 77c29ef742b880d0c67e7d6a0797ca60f435f60a Mon Sep 17 00:00:00 2001 From: Kyle Gottfried Date: Mon, 4 Sep 2023 06:09:40 -0400 Subject: [PATCH] Do not match keywords named 'match' (#1111) Closes #1110 , do not match variable names named 'match'. --------- Signed-off-by: Kyle Gottfried Co-authored-by: Charles Co-authored-by: Bill Wendling --- CHANGELOG.md | 1 + yapf/yapflib/format_decision_state.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb5df20f3..73911fb48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ by removing the "verify" parameter. - Changed FORCE_MULTILINE_DICT to override SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES. - Adopt pyproject.toml (PEP 517) for build system +- Do not treat variables named `match` as the match keyword ## [0.40.1] 2023-06-20 ### Fixed diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py index a0d7033a3..bc7f977a7 100644 --- a/yapf/yapflib/format_decision_state.py +++ b/yapf/yapflib/format_decision_state.py @@ -27,6 +27,7 @@ """ from yapf.pytree import split_penalty +from yapf.pytree.pytree_utils import NodeName from yapf.yapflib import logical_line from yapf.yapflib import object_state from yapf.yapflib import style @@ -1101,15 +1102,26 @@ def _ContainerFitsOnStartLine(self, opening): _COMPOUND_STMTS = frozenset({ - 'for', 'while', 'if', 'elif', 'with', 'except', 'def', 'class', 'match', - 'case' + 'for', + 'while', + 'if', + 'elif', + 'with', + 'except', + 'def', + 'class', }) def _IsCompoundStatement(token): - if token.value == 'async': + value = token.value + if value == 'async': token = token.next_token - return token.value in _COMPOUND_STMTS + if token.value in _COMPOUND_STMTS: + return True + parent_name = NodeName(token.node.parent) + return value == 'match' and parent_name == 'match_stmt' or \ + value == 'case' and parent_name == 'case_stmt' def _IsFunctionDef(token):