Assertions unsafe in production code
See original GitHub issueI’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:
- Created 5 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@danpalmer totally agree would this and would love to see a PR which takes us in this direction
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.