Add typing and new features to registries.

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

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.

Summary ID
Add typing and new features to registries.
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.
4653db1349e7222efa648bc6b2ff89179a7ce799
Description From Last Updated

Typo: "used" -> "using"

maubinmaubin
maubin
  1. 
      
  2. djblets/registries/mixins.py (Diff revision 1)
     
     

    Typo: "used" -> "using"

  3. 
      
chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-3.x (660b429)
Loading...