Skip to content

Commit

Permalink
Use strict optional checking in misc.py (#11382)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Jul 17, 2023
1 parent b252ad8 commit 593b85f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Empty file.
10 changes: 6 additions & 4 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,17 @@ def add_trusted_host(
msg += f" (from {source})"
logger.info(msg)

host_port = parse_netloc(host)
if host_port not in self.pip_trusted_origins:
self.pip_trusted_origins.append(host_port)
parsed_host, parsed_port = parse_netloc(host)
if parsed_host is None:
raise ValueError(f"Trusted host URL must include a host part: {host!r}")
if (parsed_host, parsed_port) not in self.pip_trusted_origins:
self.pip_trusted_origins.append((parsed_host, parsed_port))

self.mount(
build_url_from_netloc(host, scheme="http") + "/", self._trusted_host_adapter
)
self.mount(build_url_from_netloc(host) + "/", self._trusted_host_adapter)
if not host_port[1]:
if not parsed_port:
self.mount(
build_url_from_netloc(host, scheme="http") + ":",
self._trusted_host_adapter,
Expand Down
20 changes: 10 additions & 10 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# The following comment should be removed at some point in the future.
# mypy: strict-optional=False

import contextlib
import errno
import getpass
Expand Down Expand Up @@ -344,17 +341,18 @@ def write_output(msg: Any, *args: Any) -> None:


class StreamWrapper(StringIO):
orig_stream: TextIO = None
orig_stream: TextIO

@classmethod
def from_stream(cls, orig_stream: TextIO) -> "StreamWrapper":
cls.orig_stream = orig_stream
return cls()
ret = cls()
ret.orig_stream = orig_stream
return ret

# compileall.compile_dir() needs stdout.encoding to print to stdout
# https://github.com/python/mypy/issues/4125
# type ignore is because TextIOBase.encoding is writeable
@property
def encoding(self): # type: ignore
def encoding(self) -> str: # type: ignore
return self.orig_stream.encoding


Expand Down Expand Up @@ -422,7 +420,7 @@ def build_url_from_netloc(netloc: str, scheme: str = "https") -> str:
return f"{scheme}://{netloc}"


def parse_netloc(netloc: str) -> Tuple[str, Optional[int]]:
def parse_netloc(netloc: str) -> Tuple[Optional[str], Optional[int]]:
"""
Return the host-port pair from a netloc.
"""
Expand Down Expand Up @@ -510,7 +508,9 @@ def _redact_netloc(netloc: str) -> Tuple[str]:
return (redact_netloc(netloc),)


def split_auth_netloc_from_url(url: str) -> Tuple[str, str, Tuple[str, str]]:
def split_auth_netloc_from_url(
url: str,
) -> Tuple[str, str, Tuple[Optional[str], Optional[str]]]:
"""
Parse a url into separate netloc, auth, and url with no auth.
Expand Down

0 comments on commit 593b85f

Please sign in to comment.