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

Automatically remove try-except blocks. #915

Open
EwoutH opened this issue Nov 9, 2023 · 2 comments
Open

Automatically remove try-except blocks. #915

EwoutH opened this issue Nov 9, 2023 · 2 comments

Comments

@EwoutH
Copy link

EwoutH commented Nov 9, 2023

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
@asottile
Copy link
Owner

asottile commented Nov 9, 2023

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

@EwoutH
Copy link
Author

EwoutH commented Nov 9, 2023

Fully agreed it’s not a good pattern. Would still be nice to be able to fix it automatically though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants