Skip to content

Commit

Permalink
Merge pull request #303 from maresb/typing-improvements
Browse files Browse the repository at this point in the history
Typing improvements
  • Loading branch information
mariusvniekerk authored Jan 2, 2023
2 parents dd529b4 + 64f5922 commit 4f07300
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
35 changes: 19 additions & 16 deletions conda_lock/pypi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
from packaging.tags import compatible_tags, cpython_tags

from conda_lock import src_parser
from conda_lock._vendor.poetry.core.packages import Dependency as PoetryDependency
from conda_lock._vendor.poetry.core.packages import Package as PoetryPackage
from conda_lock._vendor.poetry.core.packages import (
Dependency,
Package,
ProjectPackage,
URLDependency,
ProjectPackage as PoetryProjectPackage,
)
from conda_lock._vendor.poetry.core.packages import URLDependency as PoetryURLDependency
from conda_lock._vendor.poetry.factory import Factory
from conda_lock._vendor.poetry.installation.chooser import Chooser
from conda_lock._vendor.poetry.installation.operations.uninstall import Uninstall
from conda_lock._vendor.poetry.puzzle import Solver
from conda_lock._vendor.poetry.puzzle import Solver as PoetrySolver
from conda_lock._vendor.poetry.repositories.pool import Pool
from conda_lock._vendor.poetry.repositories.pypi_repository import PyPiRepository
from conda_lock._vendor.poetry.repositories.repository import Repository
Expand Down Expand Up @@ -142,15 +142,15 @@ def parse_pip_requirement(requirement: str) -> Optional[Dict[str, str]]:
return match.groupdict()


def get_dependency(dep: src_parser.Dependency) -> Dependency:
def get_dependency(dep: src_parser.Dependency) -> PoetryDependency:
# FIXME: how do deal with extras?
extras: List[str] = []
if isinstance(dep, src_parser.VersionedDependency):
return Dependency(
return PoetryDependency(
name=dep.name, constraint=dep.version or "*", extras=dep.extras
)
elif isinstance(dep, src_parser.URLDependency):
return URLDependency(
return PoetryURLDependency(
name=dep.name,
url=f"{dep.url}#{dep.hashes[0].replace(':','=')}",
extras=extras,
Expand All @@ -159,16 +159,16 @@ def get_dependency(dep: src_parser.Dependency) -> Dependency:
raise ValueError(f"Unknown requirement {dep}")


def get_package(locked: src_parser.LockedDependency) -> Package:
def get_package(locked: src_parser.LockedDependency) -> PoetryPackage:
if locked.source is not None:
return Package(
return PoetryPackage(
locked.name,
source_type="url",
source_url=locked.source.url,
version="0.0.0",
)
else:
return Package(locked.name, version=locked.version)
return PoetryPackage(locked.name, version=locked.version)


def solve_pypi(
Expand Down Expand Up @@ -203,8 +203,10 @@ def solve_pypi(
Print chatter from solver
"""
dummy_package = ProjectPackage("_dummy_package_", "0.0.0")
dependencies = [get_dependency(spec) for spec in pip_specs.values()]
dummy_package = PoetryProjectPackage("_dummy_package_", "0.0.0")
dependencies: List[PoetryDependency] = [
get_dependency(spec) for spec in pip_specs.values()
]
for dep in dependencies:
dummy_package.add_dependency(dep)

Expand Down Expand Up @@ -240,7 +242,7 @@ def solve_pypi(
# treat conda packages as both locked and installed
for name, version in python_packages.items():
for repo in (locked, installed):
repo.add_package(Package(name=name, version=version))
repo.add_package(PoetryPackage(name=name, version=version))
# treat pip packages as locked only
for spec in pip_locked.values():
locked.add_package(get_package(spec))
Expand All @@ -250,12 +252,13 @@ def solve_pypi(
io.set_verbosity(VERY_VERBOSE)
else:
io = NullIO()
s = Solver(
s = PoetrySolver(
dummy_package,
pool=pool,
installed=installed,
locked=locked,
io=io,
# ConsoleIO type is expected, but NullIO may be given:
io=io, # type: ignore
)
to_update = list(
{spec.name for spec in pip_locked.values()}.intersection(use_latest)
Expand Down
9 changes: 6 additions & 3 deletions conda_lock/src_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def for_platform(self, platform: str) -> bool:
return self.platform is None or platform in self.platform


class Dependency(StrictModel):
class _BaseDependency(StrictModel):
name: str
manager: Literal["conda", "pip"] = "conda"
optional: bool = False
Expand All @@ -70,17 +70,20 @@ class Dependency(StrictModel):
selectors: Selectors = Selectors()


class VersionedDependency(Dependency):
class VersionedDependency(_BaseDependency):
version: str
build: Optional[str] = None
conda_channel: Optional[str] = None


class URLDependency(Dependency):
class URLDependency(_BaseDependency):
url: str
hashes: List[str]


Dependency = Union[VersionedDependency, URLDependency]


class Package(StrictModel):
url: str
hash: str
Expand Down

0 comments on commit 4f07300

Please sign in to comment.