-
Notifications
You must be signed in to change notification settings - Fork 111
Move B042 to B913 to not be enabled by deefault due to false positives #528
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -288,7 +288,7 @@ second usage. Save the result to a list if the result is needed multiple times. | |||||
|
|
||||||
| **B041**: Repeated key-value pair in dictionary literal. Only emits errors when the key's value is *also* the same, being the opposite of the pyflakes like check. | ||||||
|
|
||||||
| **B042**: Remember to call super().__init__() in custom exceptions initalizer. | ||||||
| **B042**: Reserved for future use. This check has been moved to B913. | ||||||
|
|
||||||
| Opinionated warnings | ||||||
| ~~~~~~~~~~~~~~~~~~~~ | ||||||
|
|
@@ -372,6 +372,11 @@ The ``strict=`` argument was added in Python 3.13, so don't enable this flag for | |||||
| **B912**: ``map()`` without an explicit `strict=` parameter set. ``strict=True`` causes the resulting iterator | ||||||
| to raise a ``ValueError`` if the arguments are exhausted at differing lengths. | ||||||
|
|
||||||
| .. _B913: | ||||||
|
|
||||||
| **B913**: Exception class with ``__init__`` should pass all args to ``super().__init__()`` in order to work | ||||||
| with ``copy.copy()``. It should also not take any kwargs. | ||||||
|
|
||||||
| .. _B950: | ||||||
|
|
||||||
| **B950**: Line too long. This is a pragmatic equivalent of | ||||||
|
|
@@ -479,10 +484,15 @@ MIT | |||||
| Change Log | ||||||
| ---------- | ||||||
|
|
||||||
| UNRELEASED | ||||||
| ~~~~~~~~~~ | ||||||
|
|
||||||
| * B913: Move B042 to be optional by default. It checks for reminding to call super().__init__ in custom exceptions | ||||||
|
||||||
| * B913: Move B042 to be optional by default. It checks for reminding to call super().__init__ in custom exceptions | |
| * B913: Move B042 to be optional by default. It checks for reminding to call super().__init__ in custom exceptions. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -614,7 +614,7 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None: | |
| self.check_for_b903(node) | ||
| self.check_for_b021(node) | ||
| self.check_for_b024_and_b027(node) | ||
| self.check_for_b042(node) | ||
| self.check_for_b913(node) | ||
| self.generic_visit(node) | ||
|
|
||
| def visit_Try(self, node) -> None: | ||
|
|
@@ -1733,7 +1733,7 @@ def check(num_args: int, param_name: str) -> None: | |
| elif func.attr == "split": | ||
| check(2, "maxsplit") | ||
|
|
||
| def check_for_b042(self, node: ast.ClassDef) -> None: # noqa: C901 # too-complex | ||
| def check_for_b913(self, node: ast.ClassDef) -> None: # noqa: C901 # too-complex | ||
| def is_exception(s: str): | ||
| for ending in "Exception", "Error", "Warning", "ExceptionGroup": | ||
| if s.endswith(ending): | ||
|
|
@@ -1755,7 +1755,7 @@ def is_exception(s: str): | |
| continue | ||
| if fun.args.kwonlyargs or fun.args.kwarg: | ||
| # kwargs cannot be passed to super().__init__() | ||
| self.add_error("B042", fun) | ||
| self.add_error("B913", fun) | ||
| return | ||
| # -1 to exclude the `self` argument | ||
| expected_arg_count = ( | ||
|
|
@@ -1782,18 +1782,18 @@ def is_exception(s: str): | |
| and b.value.func.attr == "__init__" | ||
| ): | ||
| if len(b.value.args) != expected_arg_count: | ||
| self.add_error("B042", fun) | ||
| self.add_error("B913", fun) | ||
| elif fun.args.vararg: | ||
| for arg in b.value.args: | ||
| if isinstance(arg, ast.Starred): | ||
| return | ||
| else: | ||
| # no Starred argument despite vararg | ||
| self.add_error("B042", fun) | ||
| self.add_error("B913", fun) | ||
| return | ||
| else: | ||
| # no super().__init__() found | ||
| self.add_error("B042", fun) | ||
| self.add_error("B913", fun) | ||
| return | ||
| # no `def __init__` found, which is fine | ||
|
|
||
|
|
@@ -2408,13 +2408,6 @@ def __call__(self, lineno: int, col: int, vars: tuple[object, ...] = ()) -> erro | |
| message="B040 Exception with added note not used. Did you forget to raise it?" | ||
| ), | ||
| "B041": Error(message=("B041 Repeated key-value pair in dictionary literal.")), | ||
| "B042": Error( | ||
| message=( | ||
| "B042 Exception class with `__init__` should pass all args to " | ||
| "`super().__init__()` in order to work with `copy.copy()`. " | ||
| "It should also not take any kwargs." | ||
| ) | ||
| ), | ||
| # Warnings disabled by default. | ||
| "B901": Error( | ||
| message=( | ||
|
|
@@ -2477,6 +2470,13 @@ def __call__(self, lineno: int, col: int, vars: tuple[object, ...] = ()) -> erro | |
| message="B911 `itertools.batched()` without an explicit `strict=` parameter." | ||
| ), | ||
| "B912": Error(message="B912 `map()` without an explicit `strict=` parameter."), | ||
| "B913": Error( | ||
| message=( | ||
| "B913 Exception class with `__init__` should pass all args to " | ||
| "`super().__init__()` in order to work with `copy.copy()`. " | ||
| "It should also not take any kwargs." | ||
|
Comment on lines
+2475
to
+2477
Collaborator
Author
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. Happy to improve here too |
||
| ) | ||
| ), | ||
| "B950": Error(message="B950 line too long ({} > {} characters)"), | ||
| } | ||
|
|
||
|
|
@@ -2493,5 +2493,6 @@ def __call__(self, lineno: int, col: int, vars: tuple[object, ...] = ()) -> erro | |
| "B910", | ||
| "B911", | ||
| "B912", | ||
| "B913", | ||
| "B950", | ||
| ] | ||
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.
Does this make more sense on how to use it? Welcome all improvements here ...