Add typing and new features to registries.
Review Request #12701 — Created Oct. 28, 2022 and submitted — Latest diff uploaded
Registries are a core part of the extensibility in Djblets and in
Review Board, and perform registration management and fetching for
specific types of items.To aid in type safety, this change adds typing to
djblets.registries
.
Registry
itself is now aGeneric[]
, allowing a subclass to specify
exactly what type of item is being stored. This then enables type safety
within the registry and for callers registering, fetching, or iterating
through items.This unveiled an issue where
ExceptionFreeGetterMixin
doesn't play
especially well with Python typing, as it overrides theRegistry.get()
method to useOptional[T]
for the result. Type checkers don't like it
when the function signature gets replaced when mixing in or subclassing,
since it's perceived to violate the contract of the function and produce
a type-unsafe result.We've worked around that here, but we have had situations in the past
where we've incorrectly calledget()
expectingNone
results for
invalid items whenItemLookupError
would be raised instead, and
vice-versa.To address these issues, a new
Registry.get_or_none()
method has been
added, which works like the mixin'sget()
method. Callers can move
over to that, andget()
can be made deprecated by subclasses. In time,
we'll probably want to deprecateExceptionFreeGetterMixin
altogether,
but that probably won't happen until Djblets 4 at the earliest.Some unit tests have also been fixed. We had some bad asserts
(assertTrue()
instead ofassertEqual()
), some type-unsafe checks,
and just some very old assertions that needed updating. The tests also
now largely test typed registries.
All unit tests passed.
Verified there are no typing warnings/errors within
djblets.registries
(usingmypy
andpyright
), aside from untyped modules being imported.Built the docs and checked for errors.