Automatically remove try-except blocks.
EwoutH opened this issue · comments
Ewout ter Hoeven commented
When currently applying pycharm on a Python 2.7-compatible codebase, a few of such cases pop up:
try:
from collections.abc import Iterable
except ImportError:
- from collections import Iterable
+ from collections.abc import Iterable
It would be useful if pyupgrade could detect if the code in both the try
and except
end up identical, and if so, remove the block.
In that case, the diff should become:
-try:
- from collections.abc import Iterable
-except ImportError:
- from collections import Iterable
+from collections.abc import Iterable
Anthony Sottile commented
the correct way to write such a concept is with if sys.version_info < (...):
which would be auto-rewritten. the try
except
pattern is error-prone
while this is fixable -- it's a bit of a garbage-in-garbage-out situation
Ewout ter Hoeven commented
Fully agreed it’s not a good pattern. Would still be nice to be able to fix it automatically though!