Skip to content

Commit 593b85f

Browse files
authored
Use strict optional checking in misc.py (#11382)
1 parent b252ad8 commit 593b85f

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

news/14514698-7F32-4890-97C1-7403A685733D.trivial.rst

Whitespace-only changes.

src/pip/_internal/network/session.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,17 @@ def add_trusted_host(
419419
msg += f" (from {source})"
420420
logger.info(msg)
421421

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

426428
self.mount(
427429
build_url_from_netloc(host, scheme="http") + "/", self._trusted_host_adapter
428430
)
429431
self.mount(build_url_from_netloc(host) + "/", self._trusted_host_adapter)
430-
if not host_port[1]:
432+
if not parsed_port:
431433
self.mount(
432434
build_url_from_netloc(host, scheme="http") + ":",
433435
self._trusted_host_adapter,

src/pip/_internal/utils/misc.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: strict-optional=False
3-
41
import contextlib
52
import errno
63
import getpass
@@ -344,17 +341,18 @@ def write_output(msg: Any, *args: Any) -> None:
344341

345342

346343
class StreamWrapper(StringIO):
347-
orig_stream: TextIO = None
344+
orig_stream: TextIO
348345

349346
@classmethod
350347
def from_stream(cls, orig_stream: TextIO) -> "StreamWrapper":
351-
cls.orig_stream = orig_stream
352-
return cls()
348+
ret = cls()
349+
ret.orig_stream = orig_stream
350+
return ret
353351

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

360358

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

424422

425-
def parse_netloc(netloc: str) -> Tuple[str, Optional[int]]:
423+
def parse_netloc(netloc: str) -> Tuple[Optional[str], Optional[int]]:
426424
"""
427425
Return the host-port pair from a netloc.
428426
"""
@@ -510,7 +508,9 @@ def _redact_netloc(netloc: str) -> Tuple[str]:
510508
return (redact_netloc(netloc),)
511509

512510

513-
def split_auth_netloc_from_url(url: str) -> Tuple[str, str, Tuple[str, str]]:
511+
def split_auth_netloc_from_url(
512+
url: str,
513+
) -> Tuple[str, str, Tuple[Optional[str], Optional[str]]]:
514514
"""
515515
Parse a url into separate netloc, auth, and url with no auth.
516516

0 commit comments

Comments
 (0)