question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Adjust Rule] SIM106: Error cases first only if it's not "NotImplementedError"

See original GitHub issue

Desired change

  • Rule(s): SIM106
  • Adjustment: Make an exception for the NotImplementedError

Explanation

Throwing an exception for potentially forgotten implementation is better than potentially returning None or making the last one catch all.

Example

    if merge_method in ["take_right_shallow", "take_right_deep"]:
        ...
    elif merge_method == "take_left_shallow":
        ...
    elif merge_method == "take_left_deep":
        ...
    elif merge_method == "sum":
        ...
    else:
        raise NotImplementedError

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
MinmoTechcommented, Aug 5, 2021

Since the blocking issue is resolved, is there any progress here? 😃

3reactions
ROpdebeecommented, Nov 4, 2020

My take on this is that it should be allowed to raise an exception in an else block when there’s a chain of ifs, regardless of the type of exception raised. Take for instance:

if path.is_dir():
   ...
elif path.is_file():
   ...
else:
   raise ValueError('File or directory expected')

Fail-fast in this case would be to do

if not (path.is_file() or path.is_dir()):
   raise ValueError('File or directory expected')

if path.is_file():
   ...
elif path.is_dir():
   ...

which makes it more complicated, IMO.

In case there’s no elifs, e.g.

if path.is_file():
   ...
else:
   raise NotImplementedError

IMHO it’s clearer to just do

if not path.is_file():
   raise NotImplementedError

# We're sure it's a file
...

although the former would be allowed by the proposed adjustment.

A final note (perhaps a bit outside the scope of this issue) is the following:

if path.is_file():
    ...
    return something

raise NotImplementedError

That’s also not detected at the moment, but detecting such things might be a bit more complicated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Why return NotImplemented instead of raising ...
When Python interpreter checks whether a.__eq__(b) returned NotImplemented, couldn't it just as easily catch NotImplementedError instead (and ...
Read more >
raise NotImplemented should be raise NotImplementedError ...
Raise NotImplementedError to indicate that a super-class method is not implemented and that child classes should implement it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found