Add typing for the extra methods on User.
Review Request #14286 — Created Jan. 3, 2025 and submitted — Latest diff uploaded
We patch
Userwith custom methods useful within Review Board. Since
this is just monkey-patching, we don't have a good way to convey the
presence of the methods for any code importing
django.contrib.auth.models.User.To address this, we're now changing the way we do the patching. All the
methods now live on an object that gets mixed in to a newUserclass
when type-checking, but is otherwise assigned as normal at runtime.It's still the same
Userobject at runtime. The only impact is for
type checking. To benefit from this, going forward we'll want to import
reviewboard.accounts.models.User(AnonymousUseris also provided for
convenience). This will ensure our typed class is used for type hints.
This can be done incrementally as needed, and won't introduce any kind
of compatibility challenges.It's worth noting that Django does have an ability to swap out the
Usermodel with a custom one, but this doesn't get us what we need.
Anything importing the oldUsermodel will continue to get that model
and not ours, and would introduce significant compatibility challenges,
so it's not an option.
All unit tests pass.
Tested updating a few modules making use of these methods and seeing that
the typing was correct.