-
Notifications
You must be signed in to change notification settings - Fork 403
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
Event Handler: API Gateway current_event doesn't show all properties #761
Comments
Forgot the instance variable had an explicit type of |
@heitorlessa any suggestions on how to implement this on Python? I am not sure how generics would work on Python. Another option would be to add properties that cast to the preferred type |
Because we have a parameter that indicates which Proxy Type to use, we can use function overloading from typing to indicate the return type will be different based on the params received. I'm considering to have an entire release just focused on MyPy stuff as more and more customers are raising issues on typing that could've been prevented, then enable MyPy at CI level -- it's a good thing, we just need a focused release or else other higher priorities will take place. After the next release I'll write a plan for this on all areas we need. MyPy and Integ test for runtime botocore are my top concerns now |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Dumping it here for posterity. None of the techniques I tried worked due to being late-bound: Dynamically inject properties, Create BaseProxyEvent at runtime, Use generic to propagate, etc. The closest I've got working is using an Union of all Proxy Event types -- auto-completion works as expected, but it shows attributes that might not be there leading to an Late bound issueAs the class variable defines the type upfront, static analysis cannot evaluate what is the correct event as it's only resolved when a request is received: class BaseRouter(ABC):
current_event: ProxyEventTypes # Union[APIGatewayProxyEvent, APIGatewayProxyEventV2, ALBEvent]
lambda_context: LambdaContext
... from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
from aws_lambda_powertools.utilities.data_classes.api_gateway_proxy_event import APIGatewayProxyEvent
app = ApiGatewayResolver()
@app.get
def hello():
request: APIGatewayProxyEvent = app.current_event # explicit type annotation works
request = app.current_event.route_key # leads to an AttributeError despite auto-completion as it's a API GW V1 event
return request.get_header_value(name="X-Id", default_value="Blah") Next stepsThree potential solutions in my head now:
cc @cakepietoast @mploski as I need some brainstorming. |
…ties Add new specialized resolvers APIGatewayProxyEventResolver, APIGatewayProxyEventV2Resolver and ALBEventResolver closes aws-powertools#761
DX from #978 Example of an Application Load Balancer handler (ALBResolver) where current_event can only be an ALBEvent from aws_lambda_powertools.event_handler import ALBResolver
app = ALBResolver()
@app.get("/lambda")
def foo():
# Now you can access ALBEvent specific attributes
assert app.current_event.request_context.elb_target_group_arn is not None
return Response(200, content_types.TEXT_HTML, "foo") Example of an REST API handler (APIGatewayRestResolver) where current_event can only be an APIGatewayProxyEvent from aws_lambda_powertools.event_handler import APIGatewayRestResolver
app = APIGatewayRestResolver()
@app.get("/my/path")
def get_lambda() -> Response:
# Now you can access APIGatewayProxyEvent specific attributes
assert app.current_event.request_context.domain_name == "id.execute-api.us-east-1.amazonaws.com"
return Response(200, content_types.APPLICATION_JSON, json.dumps({"foo": "value"})) Example of an Http API handler (APIGatewayHttpResolver) where current_event can only be an APIGatewayProxyEventV2 from aws_lambda_powertools.event_handler import APIGatewayHttpResolver
app = APIGatewayHttpResolver()
@app.post("/my/path")
def my_path() -> Response:
# Now you can access APIGatewayProxyEventV2 specific attributes
assert app.current_event.cookies[0] == "cookie1"
return Response(200, content_types.TEXT_PLAIN, "Foo") |
When trying to access
request_context
when using either API Gateway v1 or v2 we can't have type hinting due to ALB being different - both inherit from BaseProxyEvent.Overloading the internal method _to_proxy_event with all potential returns, even with
Literal[ProxyEventType.APIGatewayProxyEvent]
didn't seem to yield any result, as MyPy and Intelisense continues to think it's aBaseProxyEvent
.Need a deeper understanding of MyPy and type invariants to potentially solve this.
The text was updated successfully, but these errors were encountered: