guilatrova / tryceratops

A linter to prevent exception handling antipatterns in Python (limited only for those who like dinosaurs).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggest else block only if except block does not raise

MaxG87 opened this issue · comments

This is a feature request.

I run tryceratops version 0.2.3, installed via pipx.

Consider the following snippet:

# located in try.py


def helper():
    ...


def example():
    try:
        helper()
        return
    except ValueError:
        raise

Running tryceratops try.py produces:

[TC300] Consider moving this statement to an 'else' block - /tmp/try.py:7:8
Done processing! 🦖✨
Processed 1 files
Found 1 violations

I find that unfortunate because it suggests to introduce another block, thus more complications. Since the except block must raise, it would suffice to move the return statement after the try-except.

I understand that in general it cannot be decided if the except block must raise, but I guess in simple cases like the above its possible. And for cases where it is not possible, maybe they are too complicated and deserve a new warning ;-).

I don't know if it would be valid or if I'm taking your example too seriously.
What do you think about ignoring bare returns? e.g. return instead of return X

In the original snippet, the return is not bare. I just made up a very brief example.

Got you, thanks for the suggestion, let me think more about it and I'll post back here what I think

I've been a bit busy recently, so I apologize for the late reply.

I'd rather have people who dislike this suggestion disable the violation either for the project or inline.

If anyone else thinks it's a problem, let me know so I can reopen the issue and we can discuss it more.