globus / globus-cli

A command line interface to Globus
https://docs.globus.org/cli
Apache License 2.0
74 stars 21 forks source link

Fix type-annotations in excepthook registry module #1013

Closed sirosen closed 1 month ago

sirosen commented 1 month ago

This module was listed with an override for disallow_any_generics=false due to the complexities around the generic type aliases it defines.

In short, the problem this module faces is that it converts between various types while preserving a type var (the type of exception being handled). However, attempting to explicitly declare that type var leads to mypy flagging runtime-safe usages which aren't provably type-safe.

To resolve these issues, a few steps are taken:

  1. Tuples of hook functions with their relevant tests in the registry are replaced by a small dataclass

    Where we once had (handleErrorX, errorIsOfTypeX), we now have _RegisteredHook[X]. This keeps the harmonious types declared in a way which type-checkers will find more palatable.

  2. Overloads are declared for an internal helper which resolves class names to classes. Because the type of this function cannotbe deduced correctly by a type checker, several overloads are declared which "explain the type".

  3. Drop support for a condition (test) function from the non-SDK error handler decorator. This is additional complexity, requiring overloads or similar "explanation" to make type-checking pass. But it is never actually used, and can therefore be safely dropped.