Skip to content

Commit

Permalink
chore(inspection): remove type errors (#5570)
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent authored May 18, 2022
1 parent 797eb87 commit f05329a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -148,6 +147,7 @@ module = [
'html5lib.*',
'jsonschema.*',
'pexpect.*',
'pkginfo.*',
'poetry.core.*',
'requests_toolbelt.*',
'shellingham.*',
Expand Down
27 changes: 18 additions & 9 deletions src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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.
"""
Expand All @@ -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()`.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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())

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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()

Expand Down
3 changes: 1 addition & 2 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f05329a

Please sign in to comment.