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.

B001 misses some bare excepts

See original GitHub issue

B001 and Flake8/pycodestyle E722 both check for bare_except, but B001 misses a lot of cases that E722 finds.

We noticed this when we tried running an internal tool that looks for overlaps in checks – we are writing some of our own and don’t want to overlap – and found that every time B001 fires E722 also fires; but the reverse is not true.

Tool output: B001 (6786) <-> E722 (34093): 13572 occurred at same line+path B001 implies E722: 100% E722 implies B001: 19%

I took a look at the implementations for B001 and E722 and found that bugbear uses the AST and E722 uses a regex:

Bugbear implementation uses AST.

def visit_ExceptHandler(self, node):
    if node.type is None:
        self.errors.append(B001(node.lineno, node.col_offset))
    self.generic_visit(node)

Flake8 implementation uses regex.

def bare_except(logical_line, noqa):
    if noqa:
        return

    regex = re.compile(r"except\s*:")
    match = regex.match(logical_line)
    if match:
        yield match.start(), "E722 do not use bare 'except'"

From the implementation, it looks like B001 and E722 should hit the same locations every time.

We have a platform that lets us run static analysis over a bunch of open source repositories at the same time, so I ran vanilla flake8 and bugbear at the same time to see if there was a pattern, but one wasn’t immediately obvious.

I thought this might be related to forgetting to call visit or something like that (I’ve been bitten by that before!) but the reason for this disparity wasn’t clear to me… so I’m making this issue. Feel free to reach out to me if you have any other questions!

Here are some examples:

image

image

image

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
ambvcommented, Jul 6, 2020

+1

2reactions
cooperleescommented, Jul 6, 2020

Maybe it’s time to just drop our “limited” Python 2 support all together. It’s past midpoint 2020.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do not use bare except, specify exception instead (E722)
A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other ......
Read more >
flake8-bugbear - PyPI
B001: Do not use bare except:, it also catches unexpected events like memory errors, interrupts, system exit, and so on. Prefer except Exception:....
Read more >
What is wrong with using a bare 'except'? - Stack Overflow
Bare except will catch exceptions you almost certainly don't want to catch, including KeyboardInterrupt (the user hitting Ctrl+C) and ...
Read more >
Fix flake8 errors and disable ignore case · da53f143ee - ha
... hanging indent E122 continuation line missing indentation or outdented E123 ... of print operator B001 Do not use bare `except:` B004 Using...
Read more >
Inspection interpretation of "PEP 8: E722 do not use bare ...
A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other ......
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