-
-
Notifications
You must be signed in to change notification settings - Fork 3
initial implementation of ASYNC123 #303
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba56421
initial implementation of ASYNC123
jakkdl 06874cc
add docs, version
jakkdl 2e807d2
fix code coverage
jakkdl 10c78ee
split out py311+-specific async123 test code to not crash CI runs on …
jakkdl 1a35dae
Update docs/rules.rst
jakkdl 1dbae0d
Update docs/rules.rst
jakkdl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
visitor105, | ||
visitor111, | ||
visitor118, | ||
visitor123, | ||
visitor_utility, | ||
visitors, | ||
) |
This file contains hidden or 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,111 @@ | ||
"""foo.""" | ||
|
||
from __future__ import annotations | ||
|
||
import ast | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from .flake8asyncvisitor import Flake8AsyncVisitor | ||
from .helpers import error_class | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Mapping | ||
|
||
|
||
@error_class | ||
class Visitor123(Flake8AsyncVisitor): | ||
error_codes: Mapping[str, str] = { | ||
"ASYNC123": ( | ||
"Raising a child exception of an exception group loses" | ||
" context, cause, and/or traceback of the exception inside the group." | ||
) | ||
} | ||
|
||
def __init__(self, *args: Any, **kwargs: Any): | ||
super().__init__(*args, **kwargs) | ||
self.try_star = False | ||
self.exception_group_names: set[str] = set() | ||
self.child_exception_list_names: set[str] = set() | ||
self.child_exception_names: set[str] = set() | ||
|
||
def _is_exception_group(self, node: ast.expr) -> bool: | ||
return ( | ||
(isinstance(node, ast.Name) and node.id in self.exception_group_names) | ||
or ( | ||
# a child exception might be an ExceptionGroup | ||
self._is_child_exception(node) | ||
) | ||
or ( | ||
isinstance(node, ast.Call) | ||
and isinstance(node.func, ast.Attribute) | ||
and self._is_exception_group(node.func.value) | ||
and node.func.attr in ("subgroup", "split") | ||
) | ||
) | ||
|
||
def _is_exception_list(self, node: ast.expr | None) -> bool: | ||
return ( | ||
isinstance(node, ast.Name) and node.id in self.child_exception_list_names | ||
) or ( | ||
isinstance(node, ast.Attribute) | ||
and node.attr == "exceptions" | ||
and self._is_exception_group(node.value) | ||
) | ||
|
||
def _is_child_exception(self, node: ast.expr | None) -> bool: | ||
return ( | ||
isinstance(node, ast.Name) and node.id in self.child_exception_names | ||
) or (isinstance(node, ast.Subscript) and self._is_exception_list(node.value)) | ||
|
||
def visit_Raise(self, node: ast.Raise): | ||
if self._is_child_exception(node.exc): | ||
self.error(node) | ||
|
||
def visit_ExceptHandler(self, node: ast.ExceptHandler): | ||
self.save_state( | ||
node, | ||
"exception_group_names", | ||
"child_exception_list_names", | ||
"child_exception_names", | ||
copy=True, | ||
) | ||
if node.name is None or ( | ||
not self.try_star | ||
and (node.type is None or "ExceptionGroup" not in ast.unparse(node.type)) | ||
): | ||
self.novisit = True | ||
return | ||
self.exception_group_names = {node.name} | ||
|
||
# ast.TryStar added in py311 | ||
# we run strict codecov on all python versions, this one doesn't run on <py311 | ||
def visit_TryStar(self, node: ast.TryStar): # type: ignore[name-defined] # pragma: no cover | ||
self.save_state(node, "try_star", copy=False) | ||
self.try_star = True | ||
|
||
def visit_Assign(self, node: ast.Assign | ast.AnnAssign): | ||
if node.value is None or not self.exception_group_names: | ||
return | ||
targets = (node.target,) if isinstance(node, ast.AnnAssign) else node.targets | ||
if self._is_child_exception(node.value): | ||
# not normally possible to assign single exception to multiple targets | ||
if len(targets) == 1 and isinstance(targets[0], ast.Name): | ||
self.child_exception_names.add(targets[0].id) | ||
elif self._is_exception_list(node.value): | ||
if len(targets) == 1 and isinstance(targets[0], ast.Name): | ||
self.child_exception_list_names.add(targets[0].id) | ||
# unpacking tuples and Starred and shit. Not implemented | ||
elif self._is_exception_group(node.value): | ||
for target in targets: | ||
if isinstance(target, ast.Name): | ||
self.exception_group_names.add(target.id) | ||
elif isinstance(target, ast.Tuple): | ||
for t in target.elts: | ||
if isinstance(t, ast.Name): | ||
self.exception_group_names.add(t.id) | ||
|
||
visit_AnnAssign = visit_Assign | ||
|
||
def visit_For(self, node: ast.For): | ||
if self._is_exception_list(node.iter) and isinstance(node.target, ast.Name): | ||
self.child_exception_names.add(node.target.id) |
This file contains hidden or 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,149 @@ | ||
import copy | ||
import sys | ||
from typing import Any | ||
|
||
if sys.version_info < (3, 11): | ||
from exceptiongroup import BaseExceptionGroup, ExceptionGroup | ||
|
||
|
||
def condition() -> bool: | ||
return True | ||
|
||
|
||
def any_fun(arg: Exception) -> Exception: | ||
return arg | ||
|
||
|
||
try: | ||
... | ||
except ExceptionGroup as e: | ||
if condition(): | ||
raise e.exceptions[0] # error: 8 | ||
elif condition(): | ||
raise copy.copy(e.exceptions[0]) # safe | ||
elif condition(): | ||
raise copy.deepcopy(e.exceptions[0]) # safe | ||
else: | ||
raise any_fun(e.exceptions[0]) # safe | ||
try: | ||
... | ||
except BaseExceptionGroup as e: | ||
raise e.exceptions[0] # error: 4 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
my_e = e.exceptions[0] | ||
raise my_e # error: 4 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
excs = e.exceptions | ||
my_e = excs[0] | ||
raise my_e # error: 4 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
excs_2 = e.subgroup(bool) | ||
if excs_2: | ||
raise excs_2.exceptions[0] # error: 8 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
excs_1, excs_2 = e.split(bool) | ||
if excs_1: | ||
raise excs_1.exceptions[0] # error: 8 | ||
if excs_2: | ||
raise excs_2.exceptions[0] # error: 8 | ||
|
||
try: | ||
... | ||
except ExceptionGroup as e: | ||
f = e | ||
raise f.exceptions[0] # error: 4 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
excs = e.exceptions | ||
excs2 = excs | ||
raise excs2[0] # error: 4 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
my_exc = e.exceptions[0] | ||
my_exc2 = my_exc | ||
raise my_exc2 # error: 4 | ||
|
||
try: | ||
... | ||
except ExceptionGroup as e: | ||
raise e.exceptions[0].exceptions[0] # e 47D1 rror: 4 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
excs = e.exceptions | ||
for exc in excs: | ||
if ...: | ||
raise exc # error: 12 | ||
raise | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
ff: ExceptionGroup[Exception] = e | ||
raise ff.exceptions[0] # error: 4 | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
raise e.subgroup(bool).exceptions[0] # type: ignore # error: 4 | ||
|
||
# not implemented | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
a, *b = e.exceptions | ||
raise a | ||
|
||
# not implemented | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
x: Any = object() | ||
x.y = e | ||
raise x.y.exceptions[0] | ||
|
||
# coverage | ||
try: | ||
... | ||
except ExceptionGroup: | ||
... | ||
|
||
# not implemented | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
(a, *b), (c, *d) = e.split(bool) | ||
if condition(): | ||
raise a | ||
if condition(): | ||
raise b[0] | ||
if condition(): | ||
raise c | ||
if condition(): | ||
raise d[0] | ||
|
||
# coverage (skip irrelevant assignments) | ||
x = 0 | ||
|
||
# coverage (ignore multiple targets when assign target is child exception) | ||
try: | ||
... | ||
except ExceptionGroup as e: | ||
exc = e.exceptions[0] | ||
b, c = exc | ||
if condition(): | ||
raise b # not handled, and probably shouldn't raise | ||
else: | ||
raise c # same | ||
|
||
# coverage (skip irrelevant loop) | ||
for x in range(5): | ||
... |
This file contains hidden or 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,4 @@ | ||
try: | ||
... | ||
except* Exception as e: | ||
raise e.exceptions[0] # error: 4 |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 much better, thanks! Just one small nit on it