Skip to content
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

Fix bugs in the "with suppress" check #55

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion refurb/checks/contextlib/with_suppress.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def check(node: TryStmt, errors: list[Error]) -> None:
except_inner = f" ({inner})"

case None:
inner = except_inner = ""
inner = "BaseException"
except_inner = ""

case _:
return
Expand Down
17 changes: 15 additions & 2 deletions refurb/checks/flow/no_with_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from mypy.nodes import (
AssignmentStmt,
Block,
CallExpr,
MypyFile,
NameExpr,
Statement,
Expand Down Expand Up @@ -55,13 +56,25 @@ def check_stmts(body: list[Statement], errors: list[Error]) -> None:
case WithStmt(
body=Block(
body=[AssignmentStmt(lvalues=[NameExpr() as name])]
)
),
expr=resources,
) if (
name.fullname
and name.fullname
== assign.lvalues[0].fullname # type: ignore
):
errors.append(ErrorInfo(assign.line, assign.column))
# Skip if suppress() is one of the resources

# see https://github.com/dosisod/refurb/issues/47
for resource in resources:
match resource:
case CallExpr(
callee=NameExpr(fullname="contextlib.suppress")
):
break

else:
errors.append(ErrorInfo(assign.line, assign.column))

assign = None

Expand Down
2 changes: 1 addition & 1 deletion test/data/err_107.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test/data/err_107.py:3:1 [FURB107]: Replace `try: ... except: pass` with `with suppress(): ...`
test/data/err_107.py:3:1 [FURB107]: Replace `try: ... except: pass` with `with suppress(BaseException): ...`
test/data/err_107.py:8:1 [FURB107]: Replace `try: ... except Exception: pass` with `with suppress(Exception): ...`
test/data/err_107.py:14:1 [FURB107]: Replace `try: ... except Exception: pass` with `with suppress(Exception): ...`
test/data/err_107.py:19:1 [FURB107]: Replace `try: ... except (ValueError, FileNotFoundError): pass` with `with suppress(ValueError, FileNotFoundError): ...`
Expand Down
8 changes: 7 additions & 1 deletion test/data/err_127.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from contextlib import nullcontext
from contextlib import nullcontext, suppress

# these will match

Expand All @@ -25,3 +25,9 @@ def func():

with nullcontext():
y = 2


# see https://github.com/dosisod/refurb/issues/47
x = 1
with suppress(Exception):
x = 2