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

Reconsider UNSET? #1384

Closed
Closed
Labels
refactorIssues and PRs related to code refactoringuser-experienceEnsuring that users have a good experience using the library
Milestone
@florimondmanca

Description

@florimondmanca

Prompted by#1372

Right now several of our public API functions or methods have a reference to anUnsetType instantiated asUNSET in their signature. For example:

httpx/httpx/_client.py

Lines 690 to 705 in589c6e0

defrequest(
self,
method:str,
url:URLTypes,
*,
content:RequestContent=None,
data:RequestData=None,
files:RequestFiles=None,
json:typing.Any=None,
params:QueryParamTypes=None,
headers:HeaderTypes=None,
cookies:CookieTypes=None,
auth:typing.Union[AuthTypes,UnsetType]=UNSET,
allow_redirects:bool=True,
timeout:typing.Union[TimeoutTypes,UnsetType]=UNSET,
)->Response:

Here's the full list of public API objects and methods that refer toUnsetType:

  • Client.request
  • Client.stream
  • Client.send
  • Client.get,Client.post,Client.put,Client.patch,Client.delete,Client.options,Client.head
  • Equivalent methods onAsyncClient
  • Timeout(...)

This style was introduced in preference to egauth=None. The goal was to be able to distinguish between for example "auth was not passed" (indicating "use the defaultauth on the client"), and "auth was passed explicitly withNone" (indicating that the userreally does not want auth to be used).

But the problem is that now thisUNSET instance andUnsetType are part of the public API: if people want to override methods onClient and keep the default behavior, then they need to importUNSET and default to it in the method.

importhttpxfromhttpx._configimportUNSET# OopsclassMyClient(httpx.Client):defpost(self,*args,auth=UNSET,**kwargs):        ...# Perhaps pre-process `auth`

It's fair to ask whether those use cases are valid, and if people can't always find a way tonot have to refer toUNSET.

But there's also a case to be made for us dropping that "sentinel value" idea entirely. For example, we could:

  • Accept a**kwargs and pullauth from there.
  • Use a builtin instead, for exampleauth="__UNSET__".
    • Pros: builtin type (string) means it removes the "I need to importUNSET" problem.
    • Cons:
      • Still a sentinel value which users need to be aware of.
      • To type this properly we'd needLiteral["__UNSET__"], andLiteral is Py3.8+ (or we need to addtyping_extensions as a dependency).

I'm not sure what option is better, or if there are other options to consider, but I'm flagging this for 1.0 because this touches the public API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorIssues and PRs related to code refactoringuser-experienceEnsuring that users have a good experience using the library

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp