Add type hints and keyword-only arguments for cache_memoize and friends.

Review Request #13987 — Created June 19, 2024 and submitted

Information

Djblets
release-5.x

Reviewers

cache_memoize() and cache_memoize_iter() didn't work well with type
hints before. Type checkers would infer a list of possible types, none
of which were likely to match the true type that would be returned, and
this forced all callers to either cast() or add a type: ignore
comment.

These now infer the return types based on the callback function
provided. This helps ensure the right type in the majority of cases.
There's a caveat in that the actual cached data may always be of some
other type, but that's already something the caller was responsible for
handling.

To keep things maintainable, these methods, along with
make_cache_key(), now require keyword-only arguments for all optional
flags and settings. For a simple usage of cache_memoize() or
cache_memozie_iter(), nothing changes, as the key and
lookup_callable arguments can still be provided as positional
arguments, but all others are now optional.

We're also taking the time to properly deprecate some old arguments,
which will be removed once keyword-only arguments become enforced.

Unit tests pass in Djblets and Review Board.

Summary ID
Add type hints and keyword-only arguments for cache_memoize and friends.
`cache_memoize()` and `cache_memoize_iter()` didn't work well with type hints before. Type checkers would infer a list of possible types, none of which were likely to match the true type that would be returned, and this forced all callers to either `cast()` or add a `type: ignore` comment. These now infer the return types based on the callback function provided. This helps ensure the right type in the majority of cases. There's a caveat in that the actual cached data may always be of some other type, but that's already something the caller was responsible for handling. To keep things maintainable, these methods, along with `make_cache_key()`, now require keyword-only arguments for all optional flags and settings. For a simple usage of `cache_memoize()` or `cache_memozie_iter()`, nothing changes, as the `key` and `lookup_callable` arguments can still be provided as positional arguments, but all others are now optional. We're also taking the time to properly deprecate some old arguments, which will be removed once keyword-only arguments become enforced.
ca32ee5f33e84f99df0b6b341593bb0af3195c2f
david
  1. Ship It!
  2. 
      
maubin
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-5.x (de168c4)