Do not match keywords named 'match' (#1111)
Closes #1110 , do not match variable names named 'match'.
---------
Signed-off-by: Kyle Gottfried <Kyle.Gottfried@outlook.com>
Co-authored-by: Charles <char101@ui.ac.id>
Co-authored-by: Bill Wendling <morbo@google.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eb5df20..73911fb 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 a0d7033..bc7f977 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 @@
_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):