diff --git a/pyproject.toml b/pyproject.toml index c0630844e44..2e0bab2a702 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -116,7 +116,6 @@ enable_error_code = ["ignore-without-code"] [[tool.mypy.overrides]] module = [ - 'poetry.inspection.info', 'poetry.installation.chef', 'poetry.installation.chooser', 'poetry.installation.executor', @@ -148,6 +147,7 @@ module = [ 'html5lib.*', 'jsonschema.*', 'pexpect.*', + 'pkginfo.*', 'poetry.core.*', 'requests_toolbelt.*', 'shellingham.*', diff --git a/src/poetry/inspection/info.py b/src/poetry/inspection/info.py index cd11239ba0e..513d0bda185 100644 --- a/src/poetry/inspection/info.py +++ b/src/poetry/inspection/info.py @@ -10,7 +10,11 @@ from pathlib import Path from tempfile import TemporaryDirectory from typing import TYPE_CHECKING +from typing import Any +from typing import Callable +from typing import ContextManager from typing import Iterator +from typing import cast import pkginfo @@ -81,9 +85,9 @@ def __init__( self.requires_python = requires_python self.files = files or [] self._cache_version = cache_version - self._source_type = None - self._source_url = None - self._source_reference = None + self._source_type: str | None = None + self._source_url: str | None = None + self._source_reference: str | None = None @property def cache_version(self) -> str | None: @@ -100,7 +104,7 @@ def update(self, other: PackageInfo) -> PackageInfo: self._cache_version = other.cache_version or self._cache_version return self - def asdict(self) -> dict[str, str | list[str] | None]: + def asdict(self) -> dict[str, Any]: """ Helper method to convert package info into a dictionary used for caching. """ @@ -116,7 +120,7 @@ def asdict(self) -> dict[str, str | list[str] | None]: } @classmethod - def load(cls, data: dict[str, str | list[str] | None]) -> PackageInfo: + def load(cls, data: dict[str, Any]) -> PackageInfo: """ Helper method to load data from a dictionary produced by `PackageInfo.asdict()`. @@ -169,7 +173,9 @@ def to_package( if root_dir or (self._source_type in {"directory"} and self._source_url): # this is a local poetry project, this means we can extract "richer" # requirement information, eg: development requirements etc. - poetry_package = self._get_poetry_package(path=root_dir or self._source_url) + poetry_package = self._get_poetry_package( + path=root_dir or Path(cast(str, self._source_url)) + ) if poetry_package: package.extras = poetry_package.extras for dependency in poetry_package.requires: @@ -274,6 +280,7 @@ def _from_sdist_file(cls, path: Path) -> PackageInfo: # So, we unpack and introspect suffix = path.suffix + context: Callable[[str], ContextManager[zipfile.ZipFile | tarfile.TarFile]] if suffix == ".zip": context = zipfile.ZipFile else: @@ -286,8 +293,8 @@ def _from_sdist_file(cls, path: Path) -> PackageInfo: context = tarfile.open - with TemporaryDirectory() as tmp: - tmp = Path(tmp) + with TemporaryDirectory() as tmp_str: + tmp = Path(tmp_str) with context(path.as_posix()) as archive: archive.extractall(tmp.as_posix()) @@ -394,7 +401,7 @@ def from_metadata(cls, path: Path) -> PackageInfo | None: if path.suffix in {".dist-info", ".egg-info"}: directories = [path] else: - directories = cls._find_dist_info(path=path) + directories = list(cls._find_dist_info(path=path)) for directory in directories: try: @@ -463,6 +470,7 @@ def from_directory(cls, path: Path, disable_build: bool = False) -> PackageInfo: build is attempted in order to gather metadata. """ project_package = cls._get_poetry_package(path) + info: PackageInfo | None if project_package: info = cls.from_package(project_package) else: @@ -480,6 +488,7 @@ def from_directory(cls, path: Path, disable_build: bool = False) -> PackageInfo: # we discovered PkgInfo but no requirements were listed + assert info info._source_type = "directory" info._source_url = path.as_posix() diff --git a/src/poetry/utils/env.py b/src/poetry/utils/env.py index ea7f8db36e6..1b95fc1b1e7 100644 --- a/src/poetry/utils/env.py +++ b/src/poetry/utils/env.py @@ -18,7 +18,6 @@ from tempfile import TemporaryDirectory from typing import TYPE_CHECKING from typing import Any -from typing import ContextManager from typing import Iterable from typing import Iterator from typing import TypeVar @@ -1830,7 +1829,7 @@ def _bin(self, bin: str) -> str: def ephemeral_environment( executable: str | Path | None = None, flags: dict[str, bool] = None, -) -> ContextManager[VirtualEnv]: +) -> Iterator[VirtualEnv]: with TemporaryDirectory() as tmp_dir: # TODO: cache PEP 517 build environment corresponding to each project venv venv_dir = Path(tmp_dir) / ".venv"