fix: type the middleware factory to FastStream's construction contract - #39
Merged
Conversation
ty 0.0.69 added `unbound-type-variable` and the weekly dependency check went red: `_DIMiddlewareFactory.__call__` declared `*args: P.args, **kwargs: P.kwargs` with the ParamSpec `P` bound to nothing. It was never solvable, which is why the code carried two `# ty: ignore[invalid-argument-type]` comments conceding it did not match `BrokerMiddleware`. Declare FastStream's real contract instead -- `__call__(msg, /, *, context: ContextRepo) -> _DiMiddleware`, mirrored on `_DiMiddleware.__init__` -- and drop `P` plus `typing.Generic[P]`. Both ignore comments come out and `add_middleware(...)` now type-checks on its own, so a future upstream contract change is a ty error at the call site rather than a first-message runtime failure. `msg: object` rather than `Any`: the protocol's message parameter is contravariant and the value is only forwarded to `BaseMiddleware.__init__`. This fired the revisit trigger on the two-class-split decision. Collapsing to `functools.partial` was built and measured -- it is ~10 LOC shorter and equally clean -- but rejected: `partial` types as `(*args: Any, **kwargs: Any)` and satisfies any protocol, so breaking the construction contract under it leaves ty silent. The decision file records the outcome and a new trigger. Closes #38 Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #38.
ty0.0.69 addedunbound-type-variableand the weekly dependency check went red:_DIMiddlewareFactory.__call__declared*args: P.args, **kwargs: P.kwargswithPbound to nothing — never solvable, which is why the code carried two# ty: ignore[invalid-argument-type]comments conceding it did not matchBrokerMiddleware. This declares FastStream's real contract instead, and both ignores come out.Rationale, alternatives, and verification:
planning/changes/2026-08-10.01-middleware-contract-typed.md.Collapsing to
functools.partial(the revisit trigger on the two-class-split decision) was built and measured, then rejected —partialtypes as(*args: Any, **kwargs: Any)and satisfies any protocol, so a contract break under it leavestysilent. That file records the outcome and a new trigger.Verified locally on the upgraded lock:
just lint-ciclean, 6 passed, 100% coverage.