Add typing and correct errors in the authentication backends.

Review Request #13143 — Created July 13, 2023 and submitted — Latest diff uploaded


Review Board


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:

  1. 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.

  2. We were very inconsistent on whether HttpRequest objects were
    optional or defaulted to None. We now follow Django's lead on most
    of these.

  3. 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.