Skip to content

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.

httpx/_urls.py Outdated
"""

def __init__(self, url: URL | str = "", **kwargs: typing.Any) -> None:
def __init__(self, url: URL | str | typing.Any = "", **kwargs: typing.Any) -> None:
Copy link
Contributor

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
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand where you are coming from, but this would make all classes / objects "work" - more or less remove the static typing help. As in converting a static bug to a runtime bug.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. However, it s the fastest way to bring great compatibility at the (small) expense of having a runtime error if you pass something that does not resemble a URL into this object.

The alternative solution is to explicitly type every library that provides a URL object of some sort, which I'm not too keen on doing.

Copy link

@zyv zyv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be fantastic, especially because of Pydantic's HttpUrl... 👍

@MarcBresson
Copy link
Author

@tomchristie Hello, would you mind looking over this small pull request when you will have some time please?

@lovelydinosaur
Copy link
Contributor

@MarcBresson Thanks so much for your contribution, it really is appreciated.

I tend to prefer the strict typing approach here.


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.

5 participants