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.

Assertions unsafe in production code

See original GitHub issue

I’ve noticed while reading graphene and graphene-django that assertions are used frequently to protect from operations with undefined results or things that should be impossible (but might still happen if used incorrectly).

In Python, assertions are not safe to use for this in general. This is because they can be disabled, and often are in production.

Instead of:

assert something is not None, "Something was unexpectedly None"

We should do this:

if something is not None:
    raise AssertionError("Something was unexpectedly None")

This is the recommended best practice in Python development where assertions are being used.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jkimbocommented, Jul 1, 2018

@danpalmer totally agree would this and would love to see a PR which takes us in this direction

0reactions
stale[bot]commented, Jul 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When should assertions stay in production code?
Assertions are comments that do not become outdated. They document which theoretical states are intended, and which states should not occur.
Read more >
The dangers of assert in Python - Snyk
Using assert unsafely. Assert should only be used for testing and debugging — not in production environments. Because assert statements only ...
Read more >
Assertions should not be used in production code
This rule raises an issue when any assertion intended to be used in test is used in production code. Supported frameworks: JUnit; FestAssert;...
Read more >
Assertions in Production Code? : r/programming - Reddit
Professionally written production code is bug free, so there is no need for asserts. Hilarious. High reliability is not achieved by making ...
Read more >
Assertions in Production Code? (2008) - Hacker News
Soft assertions. They crash under test and in development, but log in production. Monitor the occurrences in production and alert if they go ......
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