-
Notifications
You must be signed in to change notification settings - Fork 892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[0.40.0] Mistakes variable "match" for a match statement? #1110
Comments
.. to stop Yapf 0.40.0 from mis-classifying it as a match statement. Related: google/yapf#1110
@char101 are you interested in submitting a patch for this? |
Either there is a bug with 3.11.4 (I still use 3.11.3) or there is a problem with pre-commit or with the workflow. Mistaking match as a variable and a keyword is pratically impossible. Either it is treated as a regular variable (<0.40) or it is treated as a context sensitive keyword (0.40) EDIT: Ok I can replicate it. Apparently the variable names has been renamed to |
The problem is that diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 0c8643f..e06da09 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -31,6 +31,7 @@ from yapf.yapflib import logical_line
from yapf.yapflib import object_state
from yapf.yapflib import style
from yapf.yapflib import subtypes
+from yapf_third_party._ylib2to3.pytree import type_repr
class FormatDecisionState(object):
@@ -1103,8 +1104,13 @@ _COMPOUND_STMTS = frozenset({
def _IsCompoundStatement(token):
- if token.value == 'async':
+ value = token.value
+ if value == 'async':
token = token.next_token
+ if value == 'match':
+ return type_repr(token.node.parent.type) == 'match_stmt'
+ if value == 'case':
+ return type_repr(token.node.parent.type) == 'case_stmt'
return token.value in _COMPOUND_STMTS @Spitfire1900 If you have the time please do create the pull request. |
Slightly more efficient diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 0c8643f..aada2ae 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -31,6 +31,7 @@ from yapf.yapflib import logical_line
from yapf.yapflib import object_state
from yapf.yapflib import style
from yapf.yapflib import subtypes
+from yapf_third_party._ylib2to3.pytree import type_repr
class FormatDecisionState(object):
@@ -1097,15 +1098,21 @@ class FormatDecisionState(object):
_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
+ if value == 'match':
+ return type_repr(token.node.parent.type) == 'match_stmt'
+ if value == 'case':
+ return type_repr(token.node.parent.type) == 'case_stmt'
+ return False |
I've been trying to keep pytree-specific stuff out of the main YAPF library in |
Like this? diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 0c8643f..c04a663 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -27,6 +27,7 @@ through the code to commit the whitespace formatting.
"""
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
@@ -1097,15 +1098,19 @@ class FormatDecisionState(object):
_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): |
Yes, thanks. :-) |
Closes #1110 , do not match variable names named 'match'. --------- Signed-off-by: Kyle Gottfried <[email protected]> Co-authored-by: Charles <[email protected]> Co-authored-by: Bill Wendling <[email protected]>
Hi! 👋
Yapf 0.40.0 suggests this change in formatting at https://github.com/hartwork/wnpp.debian.net/actions/runs/5299147736/jobs/9591931423?pr=707#step:6:30 …
… which now indents by one level more than expected. I'm reading that as the variable "match" name being misinterpreted as a match statement. I could imagine this will be hard to fix. What do you think?
Best, Sebastian
The text was updated successfully, but these errors were encountered: