Skip to content

Confusing error messages when passing in the wrong URL type  #1258

Closed

Description

@wozniakty

else:

Say, for example, we're using a Starlette API which features a URL type: https://github.com/encode/starlette/blob/c566fc6c819f0d565f8cff432351fe009e83d866/starlette/datastructures.py#L14

And you might mistakenly think you could pass that URL type in and use it, but of course you'd be wrong because:

class URL:
    def __init__(self, url: URLTypes = "", params: QueryParamTypes = None) -> None:
        if isinstance(url, str):
            ...
        else:
            self._uri_reference = url._uri_reference

It is not a string, but also does not have a ._uri_reference field.

The resulting error we got, which was hard for us to debug was:

'URL' object has no attribute '_uri_reference'
Traceback (most recent call last):
  ...
  File <redacted>, line 27, in <redacted>
    async with AsyncClient(base_url=settings.service_baselink) as client:
  File "<redacted>/lib/python3.7/site-packages/httpx/_client.py", line 1125, in __init__
    trust_env=trust_env,
  File "<redacted>/lib/python3.7/site-packages/httpx/_client.py", line 72, in __init__
    self._base_url = self._enforce_trailing_slash(URL(base_url))
  File "<redacted>/lib/python3.7/site-packages/httpx/_models.py", line 74, in __init__
    self._uri_reference = url._uri_reference
AttributeError: 'URL' object has no attribute '_uri_reference'

I would suggest that this block be changed to have a further check on the appropriate expected types and then raise a type error rather than an attribute error here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    user-experienceEnsuring that users have a good experience using the library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions