Skip to content

How to type check an Application? #5864

Closed
@Dreamsorcerer

Description

I think this is something we should discuss and make any changes necessary before v4 is released.

Our examples and demos tend to use the app object in a way that is incompatible with static typing:

def handler(request: Request):
    db = request.app["db"]  # Any

app = Application()
app["db"] = ...

This tends to leave large amounts of code without static typing (e.g. anything that interacts with DB or interacts with the values returned from the DB).

How can we improve this to have better typing support?

I don't think it's possible to inherit from TypedDict, so I don't think there is any way to support the current dict-like interface.

If we moved the dict to a separate attribute, then maybe it could be possible to do something along these lines:

class ConfigDict(TypedDict):
    db: ...

class MyApp(web.Application):
    config: ConfigDict

class MyRequest(web.Request):
    app: MyApp
    config_dict: ConfigDict  # ??

def handler(request: MyRequest):
    db = request.app.config["db"]

app = MyApp()
app.dict["db"] = ...

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions