-
-
Notifications
You must be signed in to change notification settings - Fork 856
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
add support for any type of URL as long as it can be stringified #3450
base: master
Are you sure you want to change the base?
Conversation
httpx/_urls.py
Outdated
@@ -74,7 +74,7 @@ class URL: | |||
themselves. | |||
""" | |||
|
|||
def __init__(self, url: URL | str = "", **kwargs: typing.Any) -> None: | |||
def __init__(self, url: URL | str | typing.Any = "", **kwargs: typing.Any) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will allow any type, which makes the other 2 types in this union irrelevant.
def __init__(self, url: URL | str | typing.Any = "", **kwargs: typing.Any) -> None: | |
def __init__(self, url: URL | str = "", **kwargs: typing.Any) -> None: |
Maybe what you want is something like this?
class HasDunderStr(Protocol):
def __str__(self): ...
But... Not sure this is useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quite like keeping the first two types. It makes it explicit that there is a great compatibility for them.
I like your idea to use protocol. I'm gonna implement it right away.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the "But... Not sure this is useful." 👀 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes it explicit what can go in this function. An object that doesn't respect this protocol will effectively raise an error.
Co-authored-by: Marcelo Trylesinski <[email protected]>
Summary
for reference : #3142
This PR brings compatibility with pydantic HttpURL object (or any object).
I'm using pydantic HttpURL type in my project (for validation purposes) but had errors when I passed the pydantic http object directly to https.request function. If the type of the given request is not httpx.URL, then the object will be stringified and the string will be passed on
urlparse
.Checklist