-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add except* support to B012&B025 (#500)
* add except* support to B012&B025, add tests for any except-handling rules * now prints except* in error messages
- Loading branch information
Showing
11 changed files
with
535 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def a(): | ||
try: | ||
pass | ||
except* Exception: | ||
pass | ||
finally: | ||
return # warning | ||
|
||
|
||
def b(): | ||
try: | ||
pass | ||
except* Exception: | ||
pass | ||
finally: | ||
if 1 + 0 == 2 - 1: | ||
return # warning | ||
|
||
|
||
def c(): | ||
try: | ||
pass | ||
except* Exception: | ||
pass | ||
finally: | ||
try: | ||
return # warning | ||
except* Exception: | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Should emit: | ||
B013 - on lines 10 and 28 | ||
""" | ||
|
||
import re | ||
|
||
try: | ||
pass | ||
except* (ValueError,): | ||
# pointless use of tuple | ||
pass | ||
|
||
# fmt: off | ||
# Turn off black to keep brackets around | ||
# single except*ion for testing purposes. | ||
try: | ||
pass | ||
except* (ValueError): | ||
# not using a tuple means it's OK (if odd) | ||
pass | ||
# fmt: on | ||
|
||
try: | ||
pass | ||
except* ValueError: | ||
# no warning here, all good | ||
pass | ||
|
||
try: | ||
pass | ||
except* (re.error,): | ||
# pointless use of tuple with dotted attribute | ||
pass | ||
|
||
try: | ||
pass | ||
except* (a.b.c.d, b.c.d): | ||
# attribute of attribute, etc. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
This is a copy of b014 but with except* instead. Should emit: | ||
B014 - on lines 11, 17, 28, 42, 49, 56, and 74. | ||
""" | ||
|
||
import binascii | ||
import re | ||
|
||
try: | ||
pass | ||
except* (Exception, TypeError): | ||
# TypeError is a subclass of Exception, so it doesn't add anything | ||
pass | ||
|
||
try: | ||
pass | ||
except* (OSError, OSError) as err: | ||
# Duplicate exception types are useless | ||
pass | ||
|
||
|
||
class MyError(Exception): | ||
pass | ||
|
||
|
||
try: | ||
pass | ||
except* (MyError, MyError): | ||
# Detect duplicate non-builtin errors | ||
pass | ||
|
||
|
||
try: | ||
pass | ||
except* (MyError, Exception) as e: | ||
# Don't assume that we're all subclasses of Exception | ||
pass | ||
|
||
|
||
try: | ||
pass | ||
except* (MyError, BaseException) as e: | ||
# But we *can* assume that everything is a subclass of BaseException | ||
raise e | ||
|
||
|
||
try: | ||
pass | ||
except* (re.error, re.error): | ||
# Duplicate exception types as attributes | ||
pass | ||
|
||
|
||
try: | ||
pass | ||
except* (IOError, EnvironmentError, OSError): | ||
# Detect if a primary exception and any its aliases are present. | ||
# | ||
# Since Python 3.3, IOError, EnvironmentError, WindowsError, mmap.error, | ||
# socket.error and select.error are aliases of OSError. See PEP 3151 for | ||
# more info. | ||
pass | ||
|
||
|
||
try: | ||
pass | ||
except* (MyException, NotImplemented): | ||
# NotImplemented is not an exception, let's not crash on it. | ||
pass | ||
|
||
|
||
try: | ||
pass | ||
except* (ValueError, binascii.Error): | ||
# binascii.Error is a subclass of ValueError. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Should emit: | ||
B025 - on lines 15, 22, 31 | ||
""" | ||
|
||
import pickle | ||
|
||
try: | ||
a = 1 | ||
except* ValueError: | ||
a = 2 | ||
finally: | ||
a = 3 | ||
|
||
try: | ||
a = 1 | ||
except* ValueError: | ||
a = 2 | ||
except* ValueError: | ||
a = 2 | ||
|
||
try: | ||
a = 1 | ||
except* pickle.PickleError: | ||
a = 2 | ||
except* ValueError: | ||
a = 2 | ||
except* pickle.PickleError: | ||
a = 2 | ||
|
||
try: | ||
a = 1 | ||
except* (ValueError, TypeError): | ||
a = 2 | ||
except* ValueError: | ||
a = 2 | ||
except* (OSError, TypeError): | ||
a = 2 |
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,14 @@ | ||
""" | ||
Should emit: | ||
B029 - on lines 8 and 13 | ||
""" | ||
|
||
try: | ||
pass | ||
except* (): | ||
pass | ||
|
||
try: | ||
pass | ||
except* () as e: | ||
pass |
Oops, something went wrong.