Open MinekPo1 opened 2 years ago
Oh, another variation I though off, that's essentially the same thing:
if cond:
raise AssertionError("msg")
# ==>
assert not cond, msg
Here is an example (in my code >-<) I found. Not the original one, but I'll have tp fix that one too.
Hi @MinekPo1, thanks for the suggestion! I'll add it to our refactoring pipeline.
By the way, check out our documentation for Custom Rules. Although I think your suggestion is a good one to include in core Sourcery, until then you can enforce this on your own code base using custom rules..
you can enforce this on your own code base using custom rules
Oh I assumed custom rules were a teams only feature. Good to know!
Edit: It is stated as a teams only feature on your pricing page
Also for anyone who wants to use this before this goes through the pipeline, here is the custom rule:
- id: move-condition-into-assertion
description: Move condition into assertion.
pattern: |
if ${condition}:
assert False, ${message?}
replacement: assert not ${condition}, ${message}
The applicable code snippet is as follows:
I have encountered this in one of my codebases and while I found it on my own, I feel like it would feel at home in the library of refactorings, removing some branching and, in general, make the code more readable.
Some reasons this can occur: