Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mypy and strengthen typing for exc hooks #899

Merged
merged 1 commit into from
Nov 30, 2023

Conversation

sirosen
Copy link
Member

@sirosen sirosen commented Nov 29, 2023

In order to have stronger and clearer type checking enforcement on the
exception hook registry, it has been refactored. Instead of all hooks
being declared in terms of an exception class and/or a condition
function, the hook registry stores all hooks with relevant condition
functions. Exception classes are represented (internally) by building
condition functions as necessary.

Therefore, the hook registry is now an ordered list of the form:

[(condition, hook)]

which is evaluated via a core loop:

for condition, hook in registry:
    if condition(error):
        return hook

This is effectively what the logic was before, but it is now declared
and stated as such, in a more readable manner.

One of the improved inferences was achieved by declaring a separate
interface method for registering an SDK-error-class hook. This
indicates to mypy that any condition function will be passed an SDK
error class, which allows mypy to know what attributes will
(minimally) be present.


A prereq for some of these changes to work was a mypy update (to v1.7.1).
This resulted in one minor, unrelated change.

@sirosen sirosen added the no-news-is-good-news This change does not require a news file label Nov 29, 2023
In order to have stronger and clearer type checking enforcement on the
exception hook registry, it has been refactored. Instead of all hooks
being declared in terms of an exception class and/or a condition
function, the hook registry stores all hooks with relevant condition
functions. Exception classes are represented (internally) by building
condition functions as necessary.

Therefore, the hook registry is now an ordered list of the form:

    [(condition, hook)]

which is evaluated via a core loop:

    for condition, hook in registry:
        if condition(error):
            return hook

This is effectively what the logic was before, but it is now declared
and stated as such, in a more readable manner.

One of the improved inferences was achieved by declaring a separate
interface method for registering an SDK-error-class hook. This
indicates to mypy that any condition function will be passed an SDK
error class, which allows mypy to know what attributes will
(minimally) be present.

---

A prereq for some of these changes to work was a mypy update (to v1.7.1).
This resulted in one minor, unrelated change.
@sirosen sirosen merged commit 21ac47c into globus:main Nov 30, 2023
17 checks passed
@sirosen sirosen deleted the mypy-exc-handling branch November 30, 2023 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-news-is-good-news This change does not require a news file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants