aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.67k stars 1.01k forks source link

Export event classes (or at least BaseLambdaEvent) or provide typeshed stub library #2094

Open dupuyarc opened 9 months ago

dupuyarc commented 9 months ago

The Chalice documentation for the CloudWatchEvent class gives an example function with a type annotation:

@app.schedule('rate(1 hour)')
def every_hour(event: CloudWatchEvent):
    pass

However, the chalice package does not export this type in init.py, so there is no importable definition of the class to use, which results in errors from IDEs and type-checkers like MyPy.

Exporting all the event classes would prevent the error in a type annotation, but may generate warnings when attributes are referenced, since those appear to be dynamically generated for each subclass. There might not be much advantage over just exporting the BaseLambdaEvent class as a "minimum viable type checking" solution.

It might be better to create a separate type stub library that can be imported if TYPE_CHECKING: that provides dummy classes with the specific attributes actually created for each event class.