Skip to content

Suggestion: set NotRequired on all fields that aren't required #460

Open
@jakkdl

Description

@jakkdl

Reading through the spec and asgiref/typing.py I noticed that NotRequired was used for a few fields, but far from all fields that aren't required. It seems that state got marked as NotRequired in #354 for backwards compatibility reasons, but I don't see why the marker shouldn't also be used for all fields that are mentioned as ""Optional"" in the spec.

This arose as I was working on hypercorn, which bundles a copy of the typing spec, where the equivalent of this failed to typecheck:

from asgiref.typing import HTTPRequestEvent

e: HTTPRequestEvent = {"type": "http.request"}

But it clearly ""should"", given that https://asgi.readthedocs.io/en/latest/specs/www.html#request-receive-event says content and more_body are optional keys. But the types doesn't reflect that, leading to an error

asgiref/asgiref/typing.py

Lines 110 to 113 in db3ff43

class HTTPRequestEvent(TypedDict):
type: Literal["http.request"]
body: bytes
more_body: bool

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions