Uh oh!
There was an error while loading. Please reload this page.
URL.port becomes Optional[int] - #1080
Merged
Merged
Conversation
| 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] |
Contributor
There was a problem hiding this comment.
Should this be a .get() to avoid KeyError: 'ftp'?
Suggested change
| default_port= {"http": 80, "https": 443}[url.scheme] | |
| default_port= {"http": 80, "https": 443}.get(url.scheme) |
ContributorAuthor
There was a problem hiding this comment.
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.)
Comment on lines
548
to
552
| hostname = ( | ||
| f"{url.host}:{default_port}" | ||
| if url.port is None | ||
| else f"{url.host}:{url.port}" | ||
| ) |
Contributor
There was a problem hiding this comment.
Could this be simplified:
Suggested change
| hostname= ( | |
| f"{url.host}:{default_port}" | |
| ifurl.portisNone | |
| elsef"{url.host}:{url.port}" | |
| ) | |
| port=url.portordefault_port | |
| hostname=f"{url.host}:{port}" |
Comment on lines
1082
to
1088
| 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}" | ||
| ) |
| raise InvalidURL('URL scheme must be "http" or "https".') | ||
| def port_or_default(url: "URL") -> typing.Optional[int]: |
Contributor
There was a problem hiding this comment.
Maybe this should be used above?
florimondmanca
approved these changes
Jul 24, 2020
florimondmanca
left a comment
Contributor
There was a problem hiding this comment.
Sounds sensible to me! And in line with the choice we've made in HTTPCore too.
Merged
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR changes
URL.portfrominttoOptional[int].Previously any non http/https URL without an explicit port component would raise an error if
url.portwas accessed.Now
url.portis 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 == Nonewhich 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)