(self, url: URL | str = "", **kwargs: typing.Any)
| 75 | """ |
| 76 | |
| 77 | def __init__(self, url: URL | str = "", **kwargs: typing.Any) -> None: |
| 78 | if kwargs: |
| 79 | allowed = { |
| 80 | "scheme": str, |
| 81 | "username": str, |
| 82 | "password": str, |
| 83 | "userinfo": bytes, |
| 84 | "host": str, |
| 85 | "port": int, |
| 86 | "netloc": bytes, |
| 87 | "path": str, |
| 88 | "query": bytes, |
| 89 | "raw_path": bytes, |
| 90 | "fragment": str, |
| 91 | "params": object, |
| 92 | } |
| 93 | |
| 94 | # Perform type checking for all supported keyword arguments. |
| 95 | for key, value in kwargs.items(): |
| 96 | if key not in allowed: |
| 97 | message = f"{key!r} is an invalid keyword argument for URL()" |
| 98 | raise TypeError(message) |
| 99 | if value is not None and not isinstance(value, allowed[key]): |
| 100 | expected = allowed[key].__name__ |
| 101 | seen = type(value).__name__ |
| 102 | message = f"Argument {key!r} must be {expected} but got {seen}" |
| 103 | raise TypeError(message) |
| 104 | if isinstance(value, bytes): |
| 105 | kwargs[key] = value.decode("ascii") |
| 106 | |
| 107 | if "params" in kwargs: |
| 108 | # Replace any "params" keyword with the raw "query" instead. |
| 109 | # |
| 110 | # Ensure that empty params use `kwargs["query"] = None` rather |
| 111 | # than `kwargs["query"] = ""`, so that generated URLs do not |
| 112 | # include an empty trailing "?". |
| 113 | params = kwargs.pop("params") |
| 114 | kwargs["query"] = None if not params else str(QueryParams(params)) |
| 115 | |
| 116 | if isinstance(url, str): |
| 117 | self._uri_reference = urlparse(url, **kwargs) |
| 118 | elif isinstance(url, URL): |
| 119 | self._uri_reference = url._uri_reference.copy_with(**kwargs) |
| 120 | else: |
| 121 | raise TypeError( |
| 122 | "Invalid type for url. Expected str or httpx.URL," |
| 123 | f" got {type(url)}: {url!r}" |
| 124 | ) |
| 125 | |
| 126 | @property |
| 127 | def scheme(self) -> str: |
nothing calls this directly
no test coverage detected