-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use match statement in checkers (3) #10529
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?
Conversation
Codecov Report❌ Patch coverage is ❌ Your patch check has failed because the patch coverage (98.43%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #10529 +/- ##
==========================================
+ Coverage 95.84% 95.86% +0.02%
==========================================
Files 177 177
Lines 19284 19308 +24
==========================================
+ Hits 18483 18510 +27
+ Misses 801 798 -3
🚀 New features to boost your workflow:
|
This comment has been minimized.
This comment has been minimized.
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.
Yeah, this looks amazing.
# Check *a, *b = ... | ||
assign_target = node.targets[0] | ||
# Check *a = b |
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.
Those comments were stangely placed, good job fixing them.
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.
That's one of the things I noticed with the match statement. It forces you to think about conditions in a more linear way compared to if statements which often improves the code itself.
if isinstance(decorator, nodes.Attribute): | ||
if self._check_functools_or_not(decorator): | ||
match decorator: | ||
case nodes.Attribute(attrname="getter" | "setter" | "deleter"): |
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.
So elegant !
elif isinstance(node, nodes.ClassDef): | ||
metaclass = node.metaclass() | ||
if metaclass and isinstance(metaclass, nodes.ClassDef): | ||
case bases.Instance(): | ||
try: | ||
metaclass.local_attr(NEXT_METHOD) | ||
node.local_attr(NEXT_METHOD) | ||
return True | ||
except astroid.NotFoundError: | ||
pass | ||
case nodes.ClassDef(): | ||
metaclass = node.metaclass() | ||
if metaclass and isinstance(metaclass, nodes.ClassDef): | ||
try: | ||
metaclass.local_attr(NEXT_METHOD) | ||
return True | ||
except astroid.NotFoundError: | ||
pass |
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.
Not sure if this one is better as we had to duplicate some code. Maybe. (+0)
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.
The diff itself looks worse than it really is. The partial duplication is there already. I just replaced the if statements with case ...
and indented the individual blocks.
return any( | ||
self._is_node_return_ended(_child) for _child in all_but_handler | ||
) and all(self._is_node_return_ended(_child) for _child in handlers) | ||
case nodes.Assert(test=nodes.Const(value=value)) if not value: |
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.
Nice.
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.
We could even simplify this a bit further since we only really care about assert False
. This should work
-case nodes.Assert(test=nodes.Const(value=value)):
+case nodes.Assert(test=nodes.Const(value=False)):
...
Will do that in a followup as it's technically a (minor) behavior change and not just refactoring.
This comment has been minimized.
This comment has been minimized.
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.
We might want to add the missing coverage to be safe.
I know that the code was not covered by automated test before the refactor, but the old code was tested by time since release and billions of use on very diverse codebases. The refactor with the new match statement isn't, so I don't think we can trust it as much as the old code without adding automated tests. We might realize that the default match case is impossible to reach and remove it or pragma no-cover it because it's very hard to automatically test but still need to be there. |
Might be my own bias speaking here. After converting a lot of if statements, I think the transformations in this PR are quite safe. I've actually excluded the unsafe / questionable ones for latter.
After looking at the code again, the uncovered else case shouldn't ever be reached as we exclude all nodes which parent isn't |
This comment has been minimized.
This comment has been minimized.
Yeah, you know match better than me and it's probably safe, but (in my youth... 😛) I've been absolutely certain that a refactor was safe and didn't need new tests and then destroyed the productions of less impactful projects :) |
True, happened to all of us at some point 😅 The primer runs do help to feel better about it though. Yes, it could be that there are one or two regressions hiding in the match PRs. However those could easily be fixed later, similar to the |
This comment has been minimized.
This comment has been minimized.
12e9111
to
afa59eb
Compare
This comment has been minimized.
This comment has been minimized.
🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉 This comment was generated for commit 8ee42d1 |
To review I'd suggest to use
Hide whitespace
.