transformers: Raise exceptions instead of using assertions for control flow

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?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 23 (14 by maintainers)

Commits related to this issue

Most upvoted comments

The second approach is more informative indeed!

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

@sgugger I am also going to pick several files to kill my time.

Just adding my two cents but I wouldn’t raise bare Exceptions but instead raise the appropriate type of exception given the error.

On Aug 19, 2021, at 1:44 AM, Ambesh Shekhar @.***> wrote:

Hi @sgugger, I’d be happy to do this. So I need to change assert to ‘Exception’ type error for only one file? (Not allowed to do this for every file?)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.