Add typing and new features to registries.

Review Request #12701 — Created Oct. 28, 2022 and submitted — Latest diff uploaded

Information

Djblets
release-3.x

Reviewers

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 a Generic[], 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 the Registry.get()
method to use Optional[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 called get() expecting None results for
invalid items when ItemLookupError 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's get() method. Callers can move
over to that, and get() can be made deprecated by subclasses. In time,
we'll probably want to deprecate ExceptionFreeGetterMixin 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 of assertEqual()), 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
(using mypy and pyright), aside from untyped modules being imported.

Built the docs and checked for errors.

Commits

Files

    Loading...