-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64f7a9d
commit 9635950
Showing
14 changed files
with
194 additions
and
42 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,34 @@ | ||
# `TRY006` - Inheriting from non defined base exception | ||
|
||
## Why is it bad | ||
|
||
It's a good idea to define a base exception for your project, you can define specific base types that should be used instead of the regular `Exception`. | ||
|
||
## How to enable it | ||
|
||
You need to define which base exceptions are intended to be used through `allowed_base_exceptions`. | ||
|
||
```toml | ||
[tool.tryceratops] | ||
allowed_base_exceptions = ["MyAppException", "CriticalAppException"] | ||
``` | ||
|
||
## How it looks like | ||
|
||
```py | ||
class MyAppException(Exception): | ||
... | ||
|
||
class SpecificException(Exception): | ||
... | ||
``` | ||
|
||
## How it should be | ||
|
||
```py | ||
class MyAppException(Exception): | ||
... | ||
|
||
class SpecificException(MyAppException): # 👈 Assuming `MyAppException` is defined as allowed | ||
... | ||
``` |
This file was deleted.
Oops, something went wrong.
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,47 @@ | ||
from functools import partial | ||
|
||
from tryceratops import analyzers | ||
from tryceratops.settings import GlobalSettings | ||
from tryceratops.violations import codes | ||
|
||
from .analyzer_helpers import assert_violation, read_sample | ||
|
||
|
||
def test_non_pickable_error(): | ||
tree = read_sample("class_non_pickable") | ||
analyzer = analyzers.classdefs.NonPickableAnalyzer() | ||
|
||
assert_non_pickable = partial( | ||
assert_violation, codes.NON_PICKABLE_CLASS[0], codes.NON_PICKABLE_CLASS[1] | ||
) | ||
|
||
violations = analyzer.check(tree, "filename") | ||
|
||
assert len(violations) == 2 | ||
|
||
assert_non_pickable(24, 0, violations[0]) | ||
assert_non_pickable(29, 0, violations[1]) | ||
|
||
|
||
def test_inherit_from_allowed_exceptions(): | ||
tree = read_sample("class_base_allowed") | ||
allowed_base_exceptions = {"AllowedExc", "AlsoAllowed"} | ||
analyzer = analyzers.classdefs.InheritFromBaseAnalyzer( | ||
GlobalSettings( | ||
include_experimental=False, | ||
exclude_dirs=[], | ||
ignore_violations=[], | ||
allowed_base_exceptions=allowed_base_exceptions, | ||
) | ||
) | ||
|
||
assert_non_pickable = partial(assert_violation, codes.ALLOWED_BASE_EXCEPTION[0]) | ||
msg_base = codes.ALLOWED_BASE_EXCEPTION[1] | ||
allowed_msg = ", ".join(allowed_base_exceptions) | ||
|
||
violations = analyzer.check(tree, "filename") | ||
|
||
assert len(violations) == 2 | ||
|
||
assert_non_pickable(msg_base.format("InvalidBase", allowed_msg), 19, 0, violations[0]) | ||
assert_non_pickable(msg_base.format("MultiInvalidBase", allowed_msg), 23, 0, violations[1]) |
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,32 @@ | ||
""" | ||
Violation: | ||
Forbid inherit from Exception if needed | ||
""" | ||
|
||
|
||
from typing import Any | ||
|
||
|
||
class AllowedExc(Exception): | ||
pass | ||
|
||
|
||
class AlsoAllowed(Exception): | ||
pass | ||
|
||
|
||
class InvalidBase(Exception): # Err | ||
pass | ||
|
||
|
||
class MultiInvalidBase(ValueError, Exception): # Err | ||
pass | ||
|
||
|
||
class ThisIsFine(AllowedExc): | ||
pass | ||
|
||
|
||
class ThisIsAlsoFine(AlsoAllowed): | ||
pass |
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