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.

Raise exceptions instead of using assertions for control flow

See original GitHub issue

https://github.com/huggingface/transformers/blob/546dc24e0883e5e9f5eb06ec8060e3e6ccc5f6d7/src/transformers/models/gpt2/modeling_gpt2.py#L698

Assertions 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:open
  • Created 2 years ago
  • Reactions:4
  • Comments:22 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
sguggercommented, Oct 25, 2021

The second approach is more informative indeed!

1reaction
m5l14i11commented, Oct 5, 2021

I’ve created PR #13894 . Please take a look.

Read more comments on GitHub >

github_iconTop 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 >

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