-
-
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
base: main
Are you sure you want to change the base?
Changes from all commits
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though Note: Mypy will always infer it as |
||
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 | ||
Comment on lines
-312
to
+318
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about this one. It works 🤷🏻♂️ There are a few things to note though. Only variable binding - no body Performance if node.orelse match [nodes.If() as next_if_node]:
pass
elif isinstance(next_sibling, nodes.If):
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I'm a bit on the fence on this one as well. The original code isn't much better, but the explicit matching on a tuple only to then disregard 50% of the tuple in both cases feels a bit "meh"... But I can't come up with a better suggestion, so fine to merge for me. |
||
|
||
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 | ||
) | ||
Comment on lines
+202
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me it would! |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,6 +207,11 @@ scripts_are_modules = true | |
warn_unused_ignores = true | ||
show_error_codes = true | ||
enable_error_code = "ignore-without-code" | ||
disable_error_code = [ | ||
# TODO: remove once match false-positives are fixed, probably in v1.18 | ||
# https://github.com/python/mypy/pull/19708 | ||
"has-type", | ||
Comment on lines
+211
to
+213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mypy errors if we don't disable pylint/extensions/private_import.py:199: error: Cannot determine type of "n" [has-type]
pylint/extensions/private_import.py:200: error: Cannot determine type of "n" [has-type]
pylint/extensions/private_import.py:201: error: Cannot determine type of "n" [has-type]
pylint/extensions/private_import.py:201: note: Error code "has-type" not covered by "type: ignore" comment
pylint/extensions/private_import.py:204: error: Cannot determine type of "s" [has-type]
pylint/extensions/private_import.py:207: error: Cannot determine type of "v" [has-type]
pylint/extensions/private_import.py:213: error: Cannot determine type of "e" [has-type]
pylint/checkers/match_statements_checker.py:46: error: Cannot determine type of "n" [has-type] Basically every time we do a variable binding inside a match case and use set variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels dangerous to disable. Normally it means you're doing something very estoric that you shouldn't be doing anyway. In this case that is obviously not true but we shouldn't forget to remove it once we can! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix is already merged python/mypy#19708. Just not sure when the next release will be though. |
||
] | ||
strict = true | ||
# TODO: Remove this once pytest has annotations | ||
disallow_untyped_decorators = false | ||
|
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.