Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: fix typings for context.py #229

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/fromager/context.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
import logging
import pathlib
import typing
from urllib.parse import urlparse

from packaging.requirements import Requirement
from packaging.utils import canonicalize_name
from packaging.utils import NormalizedName, canonicalize_name
from packaging.version import Version

from . import constraints, settings
Expand Down Expand Up @@ -51,46 +52,50 @@ def __init__(
# Push items onto the stack as we start to resolve their
# dependencies so at the end we have a list of items that need to
# be built in order.
self._build_stack = []
self._build_requirements = set()
self._build_stack: list[typing.Any] = []
self._build_requirements: set[tuple[NormalizedName, str]] = set()

# Track requirements we've seen before so we don't resolve the
# same dependencies over and over and so we can break cycles in
# the dependency list. The key is the requirements spec, rather
# than the package, in case we do have multiple rules for the same
# package.
self._seen_requirements = set()
self._seen_requirements: set[tuple[NormalizedName, tuple[str, ...], str]] = (
tiran marked this conversation as resolved.
Show resolved Hide resolved
set()
)

@property
def pip_wheel_server_args(self) -> list[str]:
args = ["--index-url", self.wheel_server_url]
parsed = urlparse(self.wheel_server_url)
if parsed.scheme != "https":
if parsed.scheme != "https" and parsed.hostname:
args = args + ["--trusted-host", parsed.hostname]
return args

def _resolved_key(self, req: Requirement, version: Version) -> tuple:
def _resolved_key(
self, req: Requirement, version: Version
) -> tuple[NormalizedName, tuple[str, ...], str]:
return (canonicalize_name(req.name), tuple(sorted(req.extras)), str(version))

def mark_as_seen(self, req: Requirement, version: Version):
def mark_as_seen(self, req: Requirement, version: Version) -> None:
key = self._resolved_key(req, version)
logger.debug(f"{req.name}: remembering seen sdist {key}")
self._seen_requirements.add(key)

def has_been_seen(self, req: Requirement, version: Version) -> tuple:
def has_been_seen(self, req: Requirement, version: Version) -> bool:
return self._resolved_key(req, version) in self._seen_requirements

def add_to_build_order(
self,
req_type: str,
req: Requirement,
version: Version,
why: list,
why: list[typing.Any],
tiran marked this conversation as resolved.
Show resolved Hide resolved
source_url: str,
source_url_type: str,
prebuilt: bool = False,
constraint: Requirement | None = None,
):
) -> None:
# We only care if this version of this package has been built,
# and don't want to trigger building it twice. The "extras"
# value, included in the _resolved_key() output, can confuse
Expand Down Expand Up @@ -122,7 +127,7 @@ def add_to_build_order(
self._constraints_filename, self._build_stack
)

def setup(self):
def setup(self) -> None:
# The work dir must already exist, so don't try to create it.
# Use os.makedirs() to create the others in case the paths
# already exist.
Expand Down
Loading