Rename URLMatcher -> URLPattern (#1109)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by GitHub · parent c6022de · Browse files · 4 files·+34 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+34 added−34 removed
- tests:+16 added−16 removed
- other:+18 added−18 removed
Provenance
- Commit
71d5234- Tree
1988e98- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
c6022de
4 files changed+34 added−34 removed
modified:httpx/_client.py+9 added-9 removed
42 unmodified lines43 VerifyTypes,44 )45 from ._utils import (46 NetRCInfo,47- URLMatcher,47+ URLPattern,48 enforce_http_url,49 get_environment_proxies,50 get_logger,51 same_origin,421 unmodified lines473 app=app,474 trust_env=trust_env,475 )476 self._proxies: typing.Dict[477- URLMatcher, typing.Optional[httpcore.SyncHTTPTransport]477+ URLPattern, typing.Optional[httpcore.SyncHTTPTransport]478 ] = {479- URLMatcher(key): None479+ URLPattern(key): None480 if proxy is None481 else self._init_proxy_transport(482 proxy,483 verify=verify,62 unmodified lines546 url = request.url547 enforce_http_url(request)548 549 if self._proxies and not should_not_be_proxied(url):550- for matcher, transport in self._proxies.items():551- if matcher.matches(url):550+ for pattern, transport in self._proxies.items():551+ if pattern.matches(url):552 return self._transport if transport is None else transport553 554 return self._transport555 441 unmodified lines997 app=app,998 trust_env=trust_env,999 )1000 self._proxies: typing.Dict[1001- URLMatcher, typing.Optional[httpcore.AsyncHTTPTransport]1001+ URLPattern, typing.Optional[httpcore.AsyncHTTPTransport]1002 ] = {1003- URLMatcher(key): None1003+ URLPattern(key): None1004 if proxy is None1005 else self._init_proxy_transport(1006 proxy,1007 verify=verify,62 unmodified lines1070 url = request.url1071 enforce_http_url(request)1072 1073 if self._proxies and not should_not_be_proxied(url):1074- for matcher, transport in self._proxies.items():1075- if matcher.matches(url):1074+ for pattern, transport in self._proxies.items():1075+ if pattern.matches(url):1076 return self._transport if transport is None else transport1077 1078 return self._transport1079 412 unmodified linesmodified:httpx/_utils.py+9 added-9 removed
432 unmodified lines433 return timedelta(seconds=perf_counter() - self.start)434 return timedelta(seconds=self.end - self.start)435 436 437-class URLMatcher:437+class URLPattern:438 """439 A utility class currently used for making lookups against proxy keys...440 441 # Wildcard matching...442- >>> pattern = URLMatcher("all")442+ >>> pattern = URLPattern("all")443 >>> pattern.matches(httpx.URL("http://example.com"))444 True445 446 # Witch scheme matching...447- >>> pattern = URLMatcher("https")447+ >>> pattern = URLPattern("https")448 >>> pattern.matches(httpx.URL("https://example.com"))449 True450 >>> pattern.matches(httpx.URL("http://example.com"))451 False452 453 # With domain matching...454- >>> pattern = URLMatcher("https://example.com")454+ >>> pattern = URLPattern("https://example.com")455 >>> pattern.matches(httpx.URL("https://example.com"))456 True457 >>> pattern.matches(httpx.URL("http://example.com"))458 False459 >>> pattern.matches(httpx.URL("https://other.com"))460 False461 462 # Wildcard scheme, with domain matching...463- >>> pattern = URLMatcher("all://example.com")463+ >>> pattern = URLPattern("all://example.com")464 >>> pattern.matches(httpx.URL("https://example.com"))465 True466 >>> pattern.matches(httpx.URL("http://example.com"))467 True468 >>> pattern.matches(httpx.URL("https://other.com"))469 False470 471 # With port matching...472- >>> pattern = URLMatcher("https://example.com:1234")472+ >>> pattern = URLPattern("https://example.com:1234")473 >>> pattern.matches(httpx.URL("https://example.com:1234"))474 True475 >>> pattern.matches(httpx.URL("https://example.com"))476 False22 unmodified lines499 500 @property501 def priority(self) -> tuple:502 """503- The priority allows URLMatcher instances to be sortable, so that503+ The priority allows URLPattern instances to be sortable, so that504 we can match from most specific to least specific.505 """506 port_priority = -1 if self.port is not None else 0507 host_priority = -len(self.host)2 unmodified lines510 511 def __hash__(self) -> int:512 return hash(self.pattern)513 514- def __lt__(self, other: "URLMatcher") -> bool:514+ def __lt__(self, other: "URLPattern") -> bool:515 return self.priority < other.priority516 517 def __eq__(self, other: typing.Any) -> bool:518- return isinstance(other, URLMatcher) and self.pattern == other.pattern518+ return isinstance(other, URLPattern) and self.pattern == other.pattern519 520 521 def warn_deprecated(message: str) -> None: # pragma: nocover522 warnings.warn(message, DeprecationWarning, stacklevel=2)modified:tests/client/test_proxies.py+4 added-4 removed
1 import httpcore2 import pytest3 4 import httpx5-from httpx._utils import URLMatcher5+from httpx._utils import URLPattern6 7 8 def url_to_origin(url: str):9 """26 unmodified lines36 def test_proxies_parameter(proxies, expected_proxies):37 client = httpx.AsyncClient(proxies=proxies)38 39 for proxy_key, url in expected_proxies:40- matcher = URLMatcher(proxy_key)41- assert matcher in client._proxies42- proxy = client._proxies[matcher]40+ pattern = URLPattern(proxy_key)41+ assert pattern in client._proxies42+ proxy = client._proxies[pattern]43 assert isinstance(proxy, httpcore.AsyncHTTPProxy)44 assert proxy.proxy_origin == url_to_origin(url)45 46 assert len(expected_proxies) == len(client._proxies)105 unmodified linesmodified:tests/test_utils.py+12 added-12 removed
6 unmodified lines7 import httpx8 from httpx._utils import (9 ElapsedTimer,10 NetRCInfo,11- URLMatcher,11+ URLPattern,12 get_ca_bundle_from_env,13 get_environment_proxies,14 guess_json_utf,15 obfuscate_sensitive_headers,314 unmodified lines330 ("", "https://example.com:123", True,),331 ],332 )333 def test_url_matches(pattern, url, expected):334- matcher = URLMatcher(pattern)335- assert matcher.matches(httpx.URL(url)) == expected334+ pattern = URLPattern(pattern)335+ assert pattern.matches(httpx.URL(url)) == expected336 337 338-def test_matcher_priority():338+def test_pattern_priority():339 matchers = [340- URLMatcher("all://"),341- URLMatcher("http://"),342- URLMatcher("http://example.com"),343- URLMatcher("http://example.com:123"),340+ URLPattern("all://"),341+ URLPattern("http://"),342+ URLPattern("http://example.com"),343+ URLPattern("http://example.com:123"),344 ]345 random.shuffle(matchers)346 assert sorted(matchers) == [347- URLMatcher("http://example.com:123"),348- URLMatcher("http://example.com"),349- URLMatcher("http://"),350- URLMatcher("all://"),347+ URLPattern("http://example.com:123"),348+ URLPattern("http://example.com"),349+ URLPattern("http://"),350+ URLPattern("all://"),351 ]