Skip to content
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

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

Open
jakkdl opened this issue Apr 18, 2024 · 8 comments
Open

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

jakkdl opened this issue Apr 18, 2024 · 8 comments

Comments

@jakkdl
Copy link

jakkdl commented Apr 18, 2024

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

@Kludex
Copy link
Contributor

Kludex commented Apr 18, 2024

I agree with this. The problem is that the spec is ambiguous because not required is interpreted as None sometimes.

I've completely redo the typing on the uvicorn.typing module.

@Kludex
Copy link
Contributor

Kludex commented Apr 18, 2024

Also, I don't like the typing module lives on the asgiref package.

@jakkdl
Copy link
Author

jakkdl commented Apr 18, 2024

Yeah it's not great that asgi-types is separate and have deviated from the ones in this package.
Looking at asgi-types it is much more liberal with NotRequired (https://github.com/Kludex/asgi-types/blob/main/asgi_types/__init__.py), but at least in my interpretation of the spec it is missing several ones, e.g. HTTPRequestEvent has more_body but not body marked.

And yeah the spec should probably avoid the term "Optional" for non-required fields when that usually means | None.

@andrewgodwin
Copy link
Member

Generally, if the spec says a field is optional, the intent there is that its key can be missing (i.e. it is NotRequired). Happy to take a PR to fix these up.

@Kludex
Copy link
Contributor

Kludex commented Apr 18, 2024

Generally, if the spec says a field is optional, the intent there is that its key can be missing (i.e. it is NotRequired). Happy to take a PR to fix these up.

There was actually a previous discussion about this when the typing module was introduced by Phil. FYI

@Kludex
Copy link
Contributor

Kludex commented Apr 19, 2024

Yeah it's not great that asgi-types is separate and have deviated from the ones in this package. Looking at asgi-types it is much more liberal with NotRequired (Kludex/asgi-types@main/asgi_types/init.py), but at least in my interpretation of the spec it is missing several ones, e.g. HTTPRequestEvent has more_body but not body marked.

And yeah the spec should probably avoid the term "Optional" for non-required fields when that usually means | None.

This is more up-to-date: https://github.com/encode/uvicorn/blob/master/uvicorn/_types.py

@jakkdl
Copy link
Author

jakkdl commented Apr 23, 2024

Yeah it's not great that asgi-types is separate and have deviated from the ones in this package. Looking at asgi-types it is much more liberal with NotRequired (Kludex/asgi-types@main/asgi_types/init.py), but at least in my interpretation of the spec it is missing several ones, e.g. HTTPRequestEvent has more_body but not body marked.
And yeah the spec should probably avoid the term "Optional" for non-required fields when that usually means | None.

This is more up-to-date: https://github.com/encode/uvicorn/blob/master/uvicorn/_types.py

sooo, any reason not to copy these upstream? Would be nice if projects like uvicorn or hypercorn could import the types from asgi-types and ensure those are kept up to date instead of keeping slightly different copies in their own repositories

@Kludex
Copy link
Contributor

Kludex commented Apr 23, 2024

Time... @jakkdl I've invited you as a collaborator to asgi-types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants