-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use match statement in checkers (1) #10514
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
Changes from 1 commit
ffed4b5
ed93da5
3b460ee
b7938b1
e734457
b3d1654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,13 +37,13 @@ def visit_match(self, node: nodes.Match) -> None: | |
""" | ||
for idx, case in enumerate(node.cases): | ||
match case.pattern: | ||
case nodes.MatchAs(pattern=None, name=nodes.AssignName()) if ( | ||
case nodes.MatchAs(pattern=None, name=nodes.AssignName(name=n)) if ( | ||
idx < len(node.cases) - 1 | ||
): | ||
self.add_message( | ||
"bare-name-capture-pattern", | ||
node=case.pattern, | ||
args=case.pattern.name.name, | ||
args=n, | ||
|
||
confidence=HIGH, | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,12 +309,13 @@ def _check_ignore_assignment_expr_suggestion( | |
if isinstance(node.test, nodes.Compare): | ||
next_if_node: nodes.If | None = None | ||
next_sibling = node.next_sibling() | ||
if len(node.orelse) == 1 and isinstance(node.orelse[0], nodes.If): | ||
# elif block | ||
next_if_node = node.orelse[0] | ||
elif isinstance(next_sibling, nodes.If): | ||
# separate if block | ||
next_if_node = next_sibling | ||
match (node.orelse, next_sibling): | ||
case [[nodes.If() as next_if_node], _]: | ||
# elif block | ||
pass | ||
case [_, nodes.If() as next_if_node]: | ||
# separate if block | ||
pass | ||
|
||
|
||
if ( # pylint: disable=too-many-boolean-expressions | ||
next_if_node is not None | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,14 +147,15 @@ def _populate_type_annotations( | |
if isinstance(usage_node, nodes.AssignName) and isinstance( | ||
usage_node.parent, (nodes.AnnAssign, nodes.Assign) | ||
): | ||
assign_parent = usage_node.parent | ||
if isinstance(assign_parent, nodes.AnnAssign): | ||
name_assignments.append(assign_parent) | ||
private_name = self._populate_type_annotations_annotation( | ||
usage_node.parent.annotation, all_used_type_annotations | ||
) | ||
elif isinstance(assign_parent, nodes.Assign): | ||
name_assignments.append(assign_parent) | ||
match assign_parent := usage_node.parent: | ||
case nodes.AnnAssign(): | ||
name_assignments.append(assign_parent) | ||
private_name = self._populate_type_annotations_annotation( | ||
assign_parent.annotation, | ||
all_used_type_annotations, | ||
) | ||
case nodes.Assign(): | ||
name_assignments.append(assign_parent) | ||
|
||
if isinstance(usage_node, nodes.FunctionDef): | ||
self._populate_type_annotations_function( | ||
|
@@ -194,24 +195,23 @@ def _populate_type_annotations_annotation( | |
"""Handles the possibility of an annotation either being a Name, i.e. just type, | ||
or a Subscript e.g. `Optional[type]` or an Attribute, e.g. `pylint.lint.linter`. | ||
""" | ||
if isinstance(node, nodes.Name) and node.name not in all_used_type_annotations: | ||
all_used_type_annotations[node.name] = True | ||
return node.name # type: ignore[no-any-return] | ||
if isinstance(node, nodes.Subscript): # e.g. Optional[List[str]] | ||
# slice is the next nested type | ||
self._populate_type_annotations_annotation( | ||
node.slice, all_used_type_annotations | ||
) | ||
# value is the current type name: could be a Name or Attribute | ||
return self._populate_type_annotations_annotation( | ||
node.value, all_used_type_annotations | ||
) | ||
if isinstance(node, nodes.Attribute): | ||
# An attribute is a type like `pylint.lint.pylinter`. node.expr is the next level | ||
# up, could be another attribute | ||
return self._populate_type_annotations_annotation( | ||
node.expr, all_used_type_annotations | ||
) | ||
match node: | ||
case nodes.Name(name=n) if n not in all_used_type_annotations: | ||
all_used_type_annotations[n] = True | ||
return n # type: ignore[no-any-return] | ||
case nodes.Subscript(slice=s, value=v): | ||
# slice is the next nested type | ||
self._populate_type_annotations_annotation(s, all_used_type_annotations) | ||
# value is the current type name: could be a Name or Attribute | ||
return self._populate_type_annotations_annotation( | ||
v, all_used_type_annotations | ||
) | ||
|
||
case nodes.Attribute(expr=e): | ||
# An attribute is a type like `pylint.lint.pylinter`. node.expr is the next level | ||
# up, could be another attribute | ||
return self._populate_type_annotations_annotation( | ||
e, all_used_type_annotations | ||
) | ||
return None | ||
|
||
@staticmethod | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we bind variables, what should the variable name be? A short-form like
n
here or bettername
? Maybe both is also an option?Not sure where I picked this up from but using short-forms has somewhat grown on me. It's especially useful if we bind multiple variables in one
case
. Saynodes.Subscript(slice=s, value=v)
. Furthermore it might help reduce the ambiguity if the keyword name is used for the variable name as well, e.g.name=name
. It also reminds me a bit of indices in loops with also just usei
,j
, so it might feel natural to look for the name in the match case.Then again just
name
might still be better in the case body.--
Another factor could be if it's just a generic name or a descriptive one. As example, one of the other example in this PR uses
next_if_node
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer more descriptive names, especially since
match ... case
can become quite long and I quickly lose context in the Github review view.But if others prefer shorthand, that is also fine. I don't feel strongly.