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.

B011 is misleading, and the advice doesn't sound good

See original GitHub issue

B011 Do not call assert False since python -O removes these calls. Instead callers should raise AssertionError().

There are two problems with this:

  1. Removal of assertions with -O is not limited to assert False cases, it removes all assertions. So the message is misleading.
  2. The suggestion to replace this assert with raise AssertionError() is not a good one, because such a construct cannot be disabled by disabling assertions when one explicitly wants to do that.

Given these considerations, I cannot think of a good use case or a fix for B011 in the first place, so I suggest removing it altogether.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
maxfischer2781commented, Apr 26, 2019

assert definitely has its place, so warning for every occurrence seems misleading. The entire point of assertions is that they can be switched off. So one actually should use them similar to how @gaul describes it: Have some costly checks that can be disabled in production.

However, -O implies the assertion machinery is switched off. Entirely. That means two things:

  1. Explicitly raising AssertionError defeats the purpose of assert being optional. That means the suggestion of B011 encourages misuse of assertions.
  2. Explicitly catching AssertionError leads to different -O-behaviour at a distance. It is a better indication of misusing assertions.

A better replacement for assert False is raise NotImplementedError, since it usually indicates a dead code path. However, I would rather suggest to flag except AssertionError, perhaps flag raise AssertionError as well. They indicate that assertions are misused for control flow, which they are not suitable for.

4reactions
neutrinoceroscommented, Aug 3, 2020

Came here to report basically the same problem. I see this is more controversial than I expected. Would it be acceptable to have an opinionated version of this warning (B911?) that would flag all assertions outside of tests functions and classes, assuming those are all named with test as a case-insensitive prefix ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Minikube start virtualbox hang on MacOS Big Sur · Issue #10011
I am trying to start minikube on MacOS. I have installed all needed software via homebrew. When I type minikube start I get...
Read more >
LEIVI Electric Bidet Smart Toilet Seat with Dual Control Mode ...
It makes no noise and will not wake your family at night. You don't have to stoop to close the seat, which is...
Read more >
Order: CFTC v. Barclays PLC, et al
Instead, Barclays conveyed false, misleading or knowingly inaccurate reports that its submitted rates for LIBOR and Euribor were based on and.
Read more >
'Never Look Away' review: This German drama is one of the ...
The German drama, nominated for a cinematography Oscar, is based on the life of painter Gerhard Richter.
Read more >
Comparing methods of category learning: Classification ...
In contrast, classification learning does not result in such a bias due to ... Analysis included standard parametric statistics as well as ...
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