fix: preserve user function type through http and cloud_event decorators#426
Open
64johnlee wants to merge 1 commit into
Open
fix: preserve user function type through http and cloud_event decorators#42664johnlee wants to merge 1 commit into
64johnlee wants to merge 1 commit into
Conversation
The http() and cloud_event() decorators previously had fixed signatures
that erased the user function's specific type, replacing it with the
generic HTTPFunction / CloudEventFunction alias. This caused type
checkers (mypy, pyright) to lose the decorated function's type, breaking
call-site validation for functions with type annotations.
Fix: introduce _HTTPFunctionT and _CloudEventFunctionT TypeVars bound to
the respective aliases. The decorators now return the same TypeVar they
receive, so the user's original callable type flows through unmodified.
The # type: ignore[return-value] suppresses the unavoidable mismatch
between the inner wrapper(*args, **kwargs) and the TypeVar -- this is
the standard pattern for typed decorator implementations.
Before:
@functions_framework.http
def handle(request: flask.Request) -> str:
...
# type of `handle` was HTTPFunction, losing return type annotation
After:
# type of `handle` is (request: flask.Request) -> str -- preserved
Fixes GoogleCloudPlatform#361
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Problem
The
@functions_framework.httpand@functions_framework.cloud_eventdecorators had fixed signatures that erased the user function's type:After decoration, mypy and pyright saw the function as the generic
HTTPFunction = Callable[[flask.Request], ResponseReturnValue]alias, losing all specific type information (custom return types, subclassed request types, etc.).Example that failed type checking before this fix:
Fix
Introduce bounded TypeVars for each decorator so the user's exact callable type flows through unchanged:
The
# type: ignore[return-value]on the return is the standard pattern for typed decorator implementations — mypy cannot verify that the innerwrapper(*args, **kwargs)satisfies the TypeVar bound, butfunctools.wrapsguarantees the runtime behaviour is correct.Changes
src/functions_framework/__init__.pyTypeVarto typing imports_CloudEventFunctionTand_HTTPFunctionTTypeVarscloud_event()andhttp()signatures to use TypeVarsBackward Compatibility
Purely additive from a runtime perspective — no behaviour changes. Type checkers now get more precise information rather than less.
Fixes #361
🤖 Generated with Claude Code