-
-
Notifications
You must be signed in to change notification settings - Fork 859
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
URL.port becomes Optional[int] #1080
Conversation
@@ -543,10 +543,13 @@ def _transport_for_url(self, url: URL) -> httpcore.SyncHTTPTransport: | |||
enforce_http_url(url) | |||
|
|||
if self._proxies and not should_not_be_proxied(url): | |||
is_default_port = (url.scheme == "http" and url.port == 80) or ( | |||
url.scheme == "https" and url.port == 443 | |||
default_port = {"http": 80, "https": 443}[url.scheme] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a .get()
to avoid KeyError: 'ftp'
?
default_port = {"http": 80, "https": 443}[url.scheme] | |
default_port = {"http": 80, "https": 443}.get(url.scheme) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no, because we've called enforce_http_url(url)
before doing anything else, so url.scheme
will always be http|https
here, and default_port
will be an int
rather than an Optional[int]
.
(Agree that it's a bit futzy, and I think it'd potentially clean up a bit if we rethought the proxy_keys/mount API.)
httpx/_client.py
Outdated
hostname = ( | ||
f"{url.host}:{default_port}" | ||
if url.port is None | ||
else f"{url.host}:{url.port}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be simplified:
hostname = ( | |
f"{url.host}:{default_port}" | |
if url.port is None | |
else f"{url.host}:{url.port}" | |
) | |
port = url.port or default_port | |
hostname = f"{url.host}:{port}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call yup.
httpx/_client.py
Outdated
default_port = {"http": 80, "https": 443}[url.scheme] | ||
is_default_port = url.port is None or url.port == default_port | ||
hostname = ( | ||
f"{url.host}:{default_port}" | ||
if url.port is None | ||
else f"{url.host}:{url.port}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comments as above
@@ -273,12 +273,20 @@ def enforce_http_url(url: "URL") -> None: | |||
raise InvalidURL('URL scheme must be "http" or "https".') | |||
|
|||
|
|||
def port_or_default(url: "URL") -> typing.Optional[int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be used above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds sensible to me! And in line with the choice we've made in HTTPCore too.
This PR changes
URL.port
fromint
toOptional[int]
.Previously any non http/https URL without an explicit port component would raise an error if
url.port
was accessed.Now
url.port
is optional, and only returns any explicitly included port in the URL, without also trying to use a scheme-default if one is not present.This is important because...
URL('ftp://www.example.com').port == None
which makes more sense thanKeyError("ftp")
._transport_for_url
, rather than at any point in the codebase that accessesurl.port
.(Yes, it's a little bit fiddly)