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
HttpRequest
objects were
optional or defaulted toNone
. We now follow Django's lead on most
of these.Some code assumed the existence of cetain modules or state that could
have theoretically beenNone
. We now assert these, helping the type
checkers.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.