Raise exceptions instead of using assertions for control flow #12789
Labels
Comments
|
That is an excellent point, and we welcome any PR that changes assert's to proper exception. We have been doing that for long error messages anyway, and I agree with you it's a better choice (as long as we raise the appropriate exception of course). |
|
I'm adding the good first issue label, this way if someone wants to take care of one file to remove all asserts and replace them with proper exceptions, they can make a PR with it! (Don't try to do all files of the library at one ;-) ) |
|
Hi @sgugger, I'd like to take up one of the files to start. Should I pick up modelling_gpt2.py and go ahead? |
|
Any of the files with assert statements is fair game :-) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
transformers/src/transformers/models/gpt2/modeling_gpt2.py
Line 698 in 546dc24
Assertions can't be relied upon for control flow because they can be disabled, as per the following:
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?
The text was updated successfully, but these errors were encountered: