Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using TypedDict to type check **kwargs#3123

igorp-collabora started this conversation inIdeas
Discussion options

Currently the most of the methods that have an underlying HTTP request takes a**kwargs: Any arguments that passed down to the HTTP handling functions. For example, it can betimeout orextra_headers.

However, because they are usingAny there is no type checking on the arguments used. This means a wrong argument name can be used.

To type hint**kwargs aTypedDict combined withUnpack can be used:

https://peps.python.org/pep-0692/

https://docs.python.org/3/library/typing.html#typing.Unpack

One of the issues implementing this can be that all unusedkwargs will be converted in to an HTTP query args in thehttp_request function. However, there is already aquery_parameters argument that was added to avoid collisions. Maybe make thequery_parameters the official way of adding extra http query and leave the extra kwargs only for backwards compatibility.

You must be logged in to vote

Replies: 1 comment 3 replies

Comment options

nejch
Feb 7, 2025
Maintainer

I'm a bit torn on this, because being able to provide API parameters for arbitrary endpoints directly as function arguments makes this library more convenient to use and the user code more pythonic.

On the other hand I assume without moving toquery_parameters we wouldn't really be able make good use of TypedDict here?

You must be logged in to vote
3 replies
@igorp-collabora
Comment options

On the other hand I assume without moving toquery_parameters we wouldn't really be able make good use of TypedDict here?

The TypedDict means a fixed keys. I believe it is impossible to have a TypedDict where only some keys have fixed keys and typed values.

the user code more pythonic.

Isn't it more explicit to have a dedicated keyword argument to add extra HTTP query instead of implicitly converting extra arguments.

@nejch
Comment options

nejchFeb 7, 2025
Maintainer

That's true. My thinking was more that for the higher-level functions, most of the time the query parameters are the only arguments users need to care about, and it's the other arguments that are more like extra args. So it feels more natural (to me) to use them like that as arguments rather than nesting them in a dict. For some custom methods/endpoints for example, we even expose them directly.

But maybe we should think about having a dedicated object that we can validate a bit in the long run. I think@JohnVillalovos had some similar ideas in the past regarding this, though.

@igorp-collabora
Comment options

It should be possible to have common and documented HTTP query parameters to still have named keys in the TypedDict. For example,sudo query. The undocumented keys can fallback to thequery_parameters.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Ideas
Labels
None yet
2 participants
@igorp-collabora@nejch

[8]ページ先頭

©2009-2025 Movatter.jp