diff --git a/src/poetry/console/commands/config.py b/src/poetry/console/commands/config.py index ca5880a01e2..13e8b2262a0 100644 --- a/src/poetry/console/commands/config.py +++ b/src/poetry/console/commands/config.py @@ -68,7 +68,7 @@ def unique_config_values(self) -> dict[str, tuple[Any, Any]]: ), "virtualenvs.path": (str, lambda val: str(Path(val))), "virtualenvs.prefer-active-python": (boolean_validator, boolean_normalizer), - "virtualenvs.prompt": (str, lambda val: str(val)), + "virtualenvs.prompt": (str, str), "experimental.system-git-client": (boolean_validator, boolean_normalizer), "installer.modern-installation": (boolean_validator, boolean_normalizer), "installer.parallel": (boolean_validator, boolean_normalizer), diff --git a/src/poetry/inspection/lazy_wheel.py b/src/poetry/inspection/lazy_wheel.py index 0361d788594..a0b2bcdccda 100644 --- a/src/poetry/inspection/lazy_wheel.py +++ b/src/poetry/inspection/lazy_wheel.py @@ -30,6 +30,7 @@ if TYPE_CHECKING: from collections.abc import Iterable from collections.abc import Iterator + from types import TracebackType from packaging.metadata import RawMetadata from requests import Session @@ -51,12 +52,12 @@ class UnsupportedWheel(Exception): class InvalidWheel(Exception): """Invalid (e.g. corrupt) wheel.""" - def __init__(self, location: str, name: str): + def __init__(self, location: str, name: str) -> None: self.location = location self.name = name def __str__(self) -> str: - return f"Wheel '{self.name}' located at {self.location} is invalid." + return f"Wheel {self.name} located at {self.location} is invalid." def metadata_from_wheel_url( @@ -240,8 +241,13 @@ def __enter__(self) -> ReadOnlyIOWrapper: self._file.__enter__() return self - def __exit__(self, *exc: Any) -> None: - self._file.__exit__(*exc) + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: + self._file.__exit__(exc_type, exc_value, traceback) def __iter__(self) -> Iterator[bytes]: raise NotImplementedError @@ -298,10 +304,15 @@ def __enter__(self) -> LazyRemoteResource: self._setup_content() return self - def __exit__(self, *exc: Any) -> None: + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: """Call ``self._reset_content()``.""" self._reset_content() - super().__exit__(*exc) + super().__exit__(exc_type, exc_value, traceback) @contextmanager def _stay(self) -> Iterator[None]: @@ -455,10 +466,15 @@ def __enter__(self) -> LazyHTTPFile: super().__enter__() return self - def __exit__(self, *exc: Any) -> None: + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: """Logs request count to quickly identify any pathological cases in log data.""" logger.debug("%d requests for url %s", self._request_count, self._url) - super().__exit__(*exc) + super().__exit__(exc_type, exc_value, traceback) def _content_length_from_head(self) -> int: """Performs a HEAD request to extract the Content-Length. @@ -514,6 +530,8 @@ class LazyWheelOverHTTP(LazyHTTPFile): # multiple times in the same invocation against an index without this support. _domains_without_negative_range: ClassVar[set[str]] = set() + _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: @@ -711,7 +729,6 @@ def _prefetch_metadata(self, name: str) -> str: can be downloaded in a single ranged GET request.""" logger.debug("begin prefetching METADATA for %s", name) - metadata_regex = re.compile(r"^[^/]*\.dist-info/METADATA$") start: int | None = None end: int | None = None @@ -723,19 +740,19 @@ def _prefetch_metadata(self, name: str) -> str: filename = "" for info in zf.infolist(): if start is None: - if metadata_regex.search(info.filename): + if self._metadata_regex.search(info.filename): filename = info.filename start = info.header_offset continue else: # The last .dist-info/ entry may be before the end of the file if the # wheel's entries are sorted lexicographically (which is unusual). - if not metadata_regex.search(info.filename): + if not self._metadata_regex.search(info.filename): end = info.header_offset break if start is None: raise UnsupportedWheel( - f"no {metadata_regex!r} found for {name} in {self.name}" + f"no {self._metadata_regex!r} found for {name} in {self.name}" ) # If it is the last entry of the zip, then give us everything # until the start of the central directory. diff --git a/tests/inspection/test_lazy_wheel.py b/tests/inspection/test_lazy_wheel.py index c3bf310e773..b32bdd48d4f 100644 --- a/tests/inspection/test_lazy_wheel.py +++ b/tests/inspection/test_lazy_wheel.py @@ -28,9 +28,9 @@ from tests.types import FixtureDirGetter - HttPrettyResponse = Tuple[int, Dict[str, Any], bytes] # status code, headers, body - HttPrettyRequestCallback = Callable[ - [HTTPrettyRequest, str, Dict[str, Any]], HttPrettyResponse + HTTPrettyResponse = Tuple[int, Dict[str, Any], bytes] # status code, headers, body + HTTPrettyRequestCallback = Callable[ + [HTTPrettyRequest, str, Dict[str, Any]], HTTPrettyResponse ] class RequestCallbackFactory(Protocol): @@ -39,7 +39,7 @@ def __call__( *, accept_ranges: str | None = "bytes", negative_offset_error: tuple[int, bytes] | None = None, - ) -> HttPrettyRequestCallback: ... + ) -> HTTPrettyRequestCallback: ... NEGATIVE_OFFSET_AS_POSITIVE = -1 @@ -47,7 +47,7 @@ def __call__( def build_head_response( accept_ranges: str | None, content_length: int, response_headers: dict[str, Any] -) -> HttPrettyResponse: +) -> HTTPrettyResponse: response_headers["Content-Length"] = content_length if accept_ranges: response_headers["Accept-Ranges"] = accept_ranges @@ -60,7 +60,7 @@ def build_partial_response( response_headers: dict[str, Any], *, negative_offset_as_positive: bool = False, -) -> HttPrettyResponse: +) -> HTTPrettyResponse: status_code = 206 response_headers["Accept-Ranges"] = "bytes" total_length = len(wheel_bytes) @@ -81,9 +81,7 @@ def build_partial_response( body = wheel_bytes[offset:] else: # range with start and end - rng_parts = rng.split("-") - start = int(rng_parts[0]) - end = int(rng_parts[1]) + start, end = map(int, rng.split("-")) body = wheel_bytes[start : end + 1] response_headers["Content-Range"] = f"bytes {start}-{end}/{total_length}" return status_code, response_headers, body @@ -95,13 +93,13 @@ def _factory( *, accept_ranges: str | None = "bytes", negative_offset_error: tuple[int, bytes] | None = None, - ) -> HttPrettyRequestCallback: + ) -> HTTPrettyRequestCallback: def handle_request( request: HTTPrettyRequest, uri: str, response_headers: dict[str, Any] - ) -> HttPrettyResponse: + ) -> HTTPrettyResponse: name = Path(urlparse(uri).path).name - wheel = Path(__file__).parent.parent.joinpath( + wheel = Path(__file__).parents[1] / ( "repositories/fixtures/pypi.org/dists/" + name ) @@ -122,9 +120,7 @@ def handle_request( accept_ranges, len(wheel_bytes), response_headers ) - rng = request.headers.get("Range", "") - if rng: - rng = rng.split("=")[1] + rng = request.headers.get("Range", "=").split("=")[1] negative_offset_as_positive = False if negative_offset_error and rng.startswith("-"): @@ -226,7 +222,7 @@ def test_metadata_from_wheel_url_smaller_than_initial_chunk_size( handle_request_factory: RequestCallbackFactory, negative_offset_as_positive: bool, ) -> None: - domain = f"tiny-wheel-{str(negative_offset_as_positive).lower()}.com" + domain = f"tiny-wheel-{str(negative_offset_as_positive).casefold()}.com" uri_regex = re.compile(f"^https://{domain}/.*$") request_callback = handle_request_factory( negative_offset_error=( diff --git a/tests/repositories/test_http_repository.py b/tests/repositories/test_http_repository.py index 88d63171f86..d6591d42301 100644 --- a/tests/repositories/test_http_repository.py +++ b/tests/repositories/test_http_repository.py @@ -68,7 +68,7 @@ def test_get_info_from_wheel( 'importlib-metadata (>=1.7.0) ; python_version < "3.8"' ] - if lazy_wheel and (supports_range_requests or supports_range_requests is None): + if lazy_wheel and supports_range_requests is not False: mock_metadata_from_wheel_url.assert_called_once_with( filename, url, repo.session )