Skip to content

Commit

Permalink
use type vars to avoid override only done for type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Jan 20, 2024
1 parent ec2d0f3 commit 8309ad2
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/poetry/inspection/lazy_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Any
from typing import BinaryIO
from typing import ClassVar
from typing import TypeVar
from typing import cast
from urllib.parse import urlparse
from zipfile import BadZipFile
Expand Down Expand Up @@ -140,6 +141,9 @@ def minimal_intervals_covering(
yield from self._merge(start, end, left, right)


T = TypeVar("T", bound="ReadOnlyIOWrapper")


class ReadOnlyIOWrapper(BinaryIO):
"""Implement read-side ``BinaryIO`` methods wrapping an inner ``BinaryIO``.
Expand All @@ -150,7 +154,7 @@ class ReadOnlyIOWrapper(BinaryIO):
def __init__(self, inner: BinaryIO) -> None:
self._file = inner

def __enter__(self) -> ReadOnlyIOWrapper:
def __enter__(self: T) -> T:
self._file.__enter__()
return self

Expand Down Expand Up @@ -255,6 +259,9 @@ def writelines(self, lines: Iterable[Any]) -> None:
raise NotImplementedError


U = TypeVar("U", bound="LazyFileOverHTTP")


class LazyFileOverHTTP(ReadOnlyIOWrapper):
"""File-like object representing a fixed-length file over HTTP.
Expand All @@ -277,7 +284,7 @@ def __init__(
self._session = session
self._url = url

def __enter__(self) -> LazyFileOverHTTP:
def __enter__(self: U) -> U:
super().__enter__()
self._setup_content()
return self
Expand Down Expand Up @@ -457,16 +464,6 @@ class LazyWheelOverHTTP(LazyFileOverHTTP):

_metadata_regex = re.compile(r"^[^/]*\.dist-info/METADATA$")

# This override is needed for mypy so we can call ``.prefetch_metadata()``
# within a ``with`` block.
def __enter__(self) -> LazyWheelOverHTTP:
"""Fetch the remote file length and reset the log of downloaded intervals.
This method must be called before ``.read()`` or ``.prefetch_metadata()``.
"""
super().__enter__()
return self

def read_metadata(self, name: str) -> bytes:
"""Download and read the METADATA file from the remote wheel."""
with ZipFile(self) as zf:
Expand Down

0 comments on commit 8309ad2

Please sign in to comment.