Add typing and correct errors in the authentication backends.
Review Request #13143 — Created July 13, 2023 and submitted — Latest diff uploaded
This change adds typing information for the authentication backends, in
preparation for upcoming work. It codifies the types and arguments
required for any overridden functions, helping to ensure consistent type
signatures for any subclasses.
In the process, some issues were found with the implementations:
BaseAuthBackend.authenticate()doesn't require any specific
credentials, but subclasses do. Those parameters were not made
optional in the subclasses, and that could lead to crashes calling
authenticate()with wrong credentials. Now they're optional, and
error information is logged instead.
We were very inconsistent on whether
optional or defaulted to
None. We now follow Django's lead on most
Some code assumed the existence of cetain modules or state that could
have theoretically been
None. We now assert these, helping the type
Some minor code was rewritten to be more type-safe in the process, and
some legacy code was modernized for Python 3.
Unit tests pass.