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

add support for any type of URL as long as it can be stringified #3450

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

MarcBresson
Copy link

@MarcBresson MarcBresson commented Dec 12, 2024

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

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

docs/api.md Outdated Show resolved Hide resolved
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:
Copy link
Member

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.

Suggested change
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.

Copy link
Author

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.

Copy link
Member

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." 👀 👍

Copy link
Author

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.

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

Successfully merging this pull request may close these issues.

2 participants