Fix UnboundLocalError: local variable 'results' referenced before assignment

#16
by albertvillanova HF staff - opened
Demo leaderboard with an integrated backend org
edited 14 days ago

Fix UnboundLocalError: local variable 'results' referenced before assignment.

If there is a exception within the try block, the variable results is never defined.

This PR raises the exception (so we have information to fix it) after having cleaned up.

CC: @clefourrier

Demo leaderboard with an integrated backend org
except Exception as e:
...
raise e
Demo leaderboard with an integrated backend org

@clefourrier the modification you proposed makes only sense if e is used in the except clause, which is not the case here.

Demo leaderboard with an integrated backend org

See: https://docs.python.org/3/tutorial/errors.html#raising-exceptions

If you need to determine whether an exception was raised but don’t intend to handle it, a simpler form of the raise statement allows you to re-raise the exception:

try:
    raise NameError('HiThere')
except NameError:
    print('An exception flew by!')
    raise
Demo leaderboard with an integrated backend org

See also: https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement

raise_stmt ::=  "raise" [expression ["from" expression]]

If no expressions are present, raise re-raises the exception that is currently being handled, which is also known as the active exception.

Demo leaderboard with an integrated backend org

Ha, got what you wanted to do, works for me then :)

clefourrier changed pull request status to merged
Demo leaderboard with an integrated backend org

Thanks for the details!

Sign up or log in to comment