-
-
Notifications
You must be signed in to change notification settings - Fork 2
add async400 exceptiongroup-invalid-access #379
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 2 commits
Commits
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
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,96 @@ | ||
"""4XX error classes, which handle exception groups. | ||
|
||
ASYNC400 except-star-invalid-attribute checks for invalid attribute access on except* | ||
""" | ||
|
||
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 | ||
|
||
EXCGROUP_ATTRS = ( | ||
# from ExceptionGroup | ||
"message", | ||
"exceptions", | ||
"subgroup", | ||
"split", | ||
"derive", | ||
# from BaseException | ||
"args", | ||
"with_traceback", | ||
"add_notes", | ||
) | ||
|
||
|
||
@error_class | ||
class Visitor4xx(Flake8AsyncVisitor): | ||
|
||
error_codes: Mapping[str, str] = { | ||
"ASYNC400": ( | ||
"Accessing attribute {} on ExceptionGroup as if it was a bare Exception." | ||
) | ||
} | ||
|
||
def __init__(self, *args: Any, **kwargs: Any): | ||
super().__init__(*args, **kwargs) | ||
self.exception_groups: list[str] = [] | ||
self.trystar = False | ||
|
||
def visit_TryStar(self, node: ast.TryStar): # type: ignore[name-defined] | ||
self.save_state(node, "trystar") | ||
self.trystar = True | ||
|
||
def visit_Try(self, node: ast.Try): | ||
self.save_state(node, "trystar") | ||
self.trystar = False | ||
|
||
def visit_ExceptHandler(self, node: ast.ExceptHandler): | ||
if not self.trystar or node.name is None: | ||
return | ||
self.save_state(node, "exception_groups", copy=True) | ||
self.exception_groups.append(node.name) | ||
self.visit_nodes(node.body) | ||
|
||
def visit_Attribute(self, node: ast.Attribute): | ||
if ( | ||
isinstance(node.value, ast.Name) | ||
and node.value.id in self.exception_groups | ||
and node.attr not in EXCGROUP_ATTRS | ||
and not (node.attr.startswith("__") and node.attr.endswith("__")) | ||
): | ||
self.error(node, node.attr) | ||
|
||
def _clear_if_name(self, node: ast.AST | None): | ||
if isinstance(node, ast.Name) and node.id in self.exception_groups: | ||
self.exception_groups.remove(node.id) | ||
|
||
def _walk_and_clear(self, node: ast.AST | None): | ||
if node is None: | ||
return | ||
for n in ast.walk(node): | ||
self._clear_if_name(n) | ||
|
||
def visit_Assign(self, node: ast.Assign): | ||
for t in node.targets: | ||
self._walk_and_clear(t) | ||
|
||
def visit_AnnAssign(self, node: ast.AnnAssign): | ||
self._clear_if_name(node.target) | ||
|
||
def visit_withitem(self, node: ast.withitem): | ||
self._walk_and_clear(node.optional_vars) | ||
|
||
def visit_FunctionDef( | ||
self, node: ast.FunctionDef | ast.AsyncFunctionDef | ast.Lambda | ||
): | ||
self.save_state(node, "exception_groups", "trystar", copy=False) | ||
self.exception_groups = [] | ||
|
||
visit_AsyncFunctionDef = visit_FunctionDef | ||
visit_Lambda = visit_FunctionDef |
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,84 @@ | ||
try: | ||
... | ||
except* ValueError as e: | ||
e.anything # error: 4, "anything" | ||
e.foo() # error: 4, "foo" | ||
e.bar.zee # error: 4, "bar" | ||
|
||
# from ExceptionGroup | ||
e.message | ||
e.exceptions | ||
e.subgroup | ||
e.split | ||
e.derive | ||
|
||
# from BaseException | ||
e.args | ||
e.with_traceback | ||
e.add_notes | ||
|
||
# ignore anything that looks like a dunder | ||
e.__foo__ | ||
e.__bar__ | ||
|
||
e.anything # safe | ||
|
||
# assigning to the variable clears it | ||
try: | ||
... | ||
except* ValueError as e: | ||
e = e.exceptions[0] | ||
e.ignore # safe | ||
except* ValueError as e: | ||
e, f = 1, 2 | ||
e.anything # safe | ||
except* TypeError as e: | ||
(e, f) = (1, 2) | ||
e.anything # safe | ||
except* ValueError as e: | ||
with blah as e: | ||
e.anything | ||
e.anything | ||
except* ValueError as e: | ||
e: int = 1 | ||
e.real | ||
except* ValueError as e: | ||
with blah as (e, f): | ||
e.anything | ||
|
||
# check state saving | ||
try: | ||
... | ||
except* ValueError as e: | ||
... | ||
except* BaseException: | ||
e.error # safe | ||
|
||
try: | ||
... | ||
except* ValueError as e: | ||
try: | ||
... | ||
except* TypeError as e: | ||
... | ||
e.anything # error: 4, "anything" | ||
|
||
try: | ||
... | ||
except* ValueError as e: | ||
|
||
def foo(): | ||
# possibly problematic, but we minimize false alarms | ||
e.anything | ||
|
||
e.anything # error: 4, "anything" | ||
|
||
def foo(e): | ||
# this one is more clear it should be treated as safe | ||
e.anything | ||
|
||
e.anything # error: 4, "anything" | ||
|
||
lambda e: e.anything | ||
|
||
e.anything # error: 4, "anything" |
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 |
---|---|---|
|
@@ -512,6 +512,7 @@ def _parse_eval_file( | |
"ASYNC123", | ||
"ASYNC125", | ||
"ASYNC300", | ||
"ASYNC400", | ||
"ASYNC912", | ||
} | ||
|
||
|
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.
Let's add a test that
set(dir(ExceptionGroup)).issubset(EXCGROUP_ATTRS)
, so that if a new method or attribute is added (e.g. by PEP-785 😅) we'll notice as soon as we test on that version of Python, rather than when users complain.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.
oopie, that caught a typo. Turns out it's
add_note
and notadd_notes
^^;