Raise exceptions instead of using assertions for control flow
See original GitHub issueAssertions can’t be relied upon for control flow because they can be disabled, as per the following:
$ python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
...
-O : remove assert and __debug__-dependent statements; add .opt-1 before
.pyc extension; also PYTHONOPTIMIZE=x
...
From my understanding, this is why mypy has no qualms about using them to narrow types because you can turn them off at runtime and so they incur zero cost.
Would you be open to me changing these assertions to other appropriate exceptions as I encounter them?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:22 (18 by maintainers)
Top Results From Across the Web
When to use assertions and when to use exceptions?
A simple rule that I tend to follow is verifying private functions' arguments with asserts, and using exceptions for public/protected functions' arguments.
Read more >Assertions and Exceptions are not the same - Inspired Python
The assert statement is not a stand-in replacement for raising exceptions. Wrongful application of assertions is a common mistake beginners make, and it...
Read more >c# - Debug.Assert vs Exception Throwing - Stack Overflow
If you use exceptions instead of asserts, you lose some value: The code is more verbose, since testing and throwing an exception is...
Read more >5.10 Assertions :: Chapter 5. Control Flow, Exception Handling ...
This makes AssertionError exceptions unchecked. They could be explicitly caught and handled using the try-catch construct. The execution would then continue ...
Read more >Lecture 15 Assertions & Exceptions - Washington
General concepts about dealing with errors and failures ... How to throw, catch, and declare exceptions in Java ... Exceptions as non-local control...
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
The second approach is more informative indeed!
I’ve created PR #13894 . Please take a look.