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

Add method for messagepack messages to assert type #45

Merged
merged 2 commits into from
Jun 13, 2022

Conversation

sirosen
Copy link
Member

@sirosen sirosen commented Jun 13, 2022

This is a simple method for letting messages assert that they are one
of a known set of types and raise a nicely formatted error if the
assertion fails.

Unfortunately, this cannot be expressed in the type system in a clean
way at present. A TypeGuard cannot be defined as a union over a
sequence-type argument to a method.

We don't want to build fragile code which is based on stringized
annotations being ealuated at runtime, so for now this just focuses on
giving us the runtime safety which we'll care about in our
applications. Additional assert calls, cast calls, or
type:ignore comments can still be used to narrow types under type
checking.

This is a simple method for letting messages assert that they are one
of a known set of types and raise a nicely formatted error if the
assertion fails.

Unfortunately, this cannot be expressed in the type system in a clean
way at present. A TypeGuard cannot be defined as a union over a
sequence-type argument to a method.

We don't want to build fragile code which is based on stringized
annotations being ealuated at runtime, so for now this just focuses on
giving us the runtime safety which we'll care about in our
applications. Additional `assert` calls, `cast` calls, or
`type:ignore` comments can still be used to narrow types under type
checking.
@chris-janidlo chris-janidlo self-requested a review June 13, 2022 19:08
Copy link
Contributor

@chris-janidlo chris-janidlo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change a lot for guarding our endpoint/web-service communication. I have one small item as a comment below, but everything else looks good.

src/funcx_common/messagepack/exceptions.py Outdated Show resolved Hide resolved
@chris-janidlo chris-janidlo merged commit d54b2c3 into main Jun 13, 2022
@chris-janidlo chris-janidlo deleted the messagepack-wrong-messagetype branch June 13, 2022 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants