Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: harupy <hkawamura0130@gmail.com>
- Loading branch information
Showing
84 changed files
with
847 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .pytest_raises_without_match import PytestRaisesWithoutMatch | ||
|
||
|
||
def register(linter): | ||
linter.register_checker(PytestRaisesWithoutMatch(linter)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# `pytest-raises-without-match` | ||
|
||
This custom pylint rule disallows calling `pytest.raises` without a `match` argument | ||
to avoid capturing unintended exceptions and eliminate false-positive tests. | ||
|
||
## Example | ||
|
||
Suppose we want to test this function throws `Exception("bar")` when `condition2` is satisfied. | ||
|
||
```python | ||
def func(): | ||
if condition1: | ||
raise Exception("foo") | ||
|
||
if condition2: | ||
raise Exception("bar") | ||
``` | ||
|
||
### Bad | ||
|
||
```python | ||
def test_func(): | ||
with pytest.raises(Exception): | ||
func() | ||
``` | ||
|
||
- This test passes when `condition1` is unintentionally satisfied. | ||
- Future code readers will struggle to identify which exception `pytest.raises` should match. | ||
|
||
### Good | ||
|
||
```python | ||
def test_func(): | ||
with pytest.raises(Exception, match="bar"): | ||
func() | ||
``` | ||
|
||
- This test fails when `condition1` is unintentionally satisfied. | ||
- Future code readers can quickly identify which exception `pytest.raises` should match by searching `bar`. | ||
|
||
## How to disable this rule | ||
|
||
```python | ||
def test_func(): | ||
with pytest.raises(Exception): # pylint: disable=pytest-raises-without-match | ||
func() | ||
``` | ||
|
||
## References | ||
|
||
- https://docs.pytest.org/en/latest/how-to/assert.html#assertions-about-expected-exceptions | ||
- https://docs.pytest.org/en/latest/reference/reference.html#pytest.raises |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import astroid | ||
from pylint.interfaces import IAstroidChecker | ||
from pylint.checkers import BaseChecker | ||
|
||
|
||
class PytestRaisesWithoutMatch(BaseChecker): | ||
__implements__ = IAstroidChecker | ||
|
||
name = "pytest-raises-without-match" | ||
msgs = { | ||
"W0001": ( | ||
"`pytest.raises` must be called with `match` argument` ", | ||
name, | ||
"Use `pytest.raises(<exception>, match=...)`", | ||
), | ||
} | ||
priority = -1 | ||
|
||
@staticmethod | ||
def _is_pytest_raises_call(node: astroid.Call): | ||
if not isinstance(node.func, astroid.Attribute) or not isinstance( | ||
node.func.expr, astroid.Name | ||
): | ||
return False | ||
return node.func.expr.name == "pytest" and node.func.attrname == "raises" | ||
|
||
@staticmethod | ||
def _called_with_match(node: astroid.Call): | ||
# Note `match` is a keyword-only argument: | ||
# https://docs.pytest.org/en/latest/reference/reference.html#pytest.raises | ||
return any(k.arg == "match" for k in node.keywords) | ||
|
||
def visit_call(self, node: astroid.Call): | ||
if not PytestRaisesWithoutMatch._is_pytest_raises_call(node): | ||
return | ||
|
||
if not PytestRaisesWithoutMatch._called_with_match(node): | ||
self.add_message(self.name, node=node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.