Interceptor and InterceptorContext typing rework #426
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Back when interceptors were first created I didn't use dataclasses because they interacted strangely with generics. Now though, that's not the case. So this updates them to be dataclasses.
Another thing I did at the time was to leave the responsibility of declaring nullability for the properties of InterceptorContext to the parametric types. So if you were at the point of the request pipeline where the request was not yet serialized, you'd have this type:
That would mean that accessing
context.transport_requestwould always be typed asNone. But after serialization it would be:And at that point
context.transport_responsewill never beNone.But the drawback is that you have to do casts like this all over the request pipeline:
An alternative would be to have
expect_transport_responsethat asserts it isn't null and then returns it. The drawbacks are that type system isn't telling you when it is and isn't null anymore, and there's now another function call. But now you don't have to cast the context all over the place in the request pipeline.I'm opening this as a draft to discuss whether we should or shouldn't make the typing change (dataclass change is happening regardless).
(Note that protocol tests won't pass because the code generated request pipeline isn't updated)
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.