Skip to content

Commit

Permalink
More types
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed May 23, 2024
1 parent 19dc1db commit 43aedad
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 80 deletions.
3 changes: 0 additions & 3 deletions src/rez/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ def callback(sig, frame):
txt = ''.join(traceback.format_stack(frame))
print()
print(txt)
else:
callback = None

if callback is not None:
signal.signal(signal.SIGUSR1, callback) # Register handler


Expand Down
2 changes: 1 addition & 1 deletion src/rez/build_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def repo_operation(self):
except exc_type as e:
print_warning("THE FOLLOWING ERROR WAS SKIPPED:\n%s" % str(e))

def visit_variants(self, func, variants: list[Variant] | None = None,
def visit_variants(self, func, variants: list[int] | None = None,
**kwargs) -> tuple[int, list[str | None]]:
"""Iterate over variants and call a function on each."""
if variants:
Expand Down
6 changes: 3 additions & 3 deletions src/rez/build_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def get_valid_build_systems(working_dir: str,

# detect valid build systems
clss = []
for buildsys_name in get_buildsys_types():
cls = plugin_manager.get_plugin_class('build_system', buildsys_name, BuildSystem)
for buildsys_name_ in get_buildsys_types():
cls = plugin_manager.get_plugin_class('build_system', buildsys_name_, BuildSystem)
if cls.is_valid_root(working_dir, package=package):
clss.append(cls)

Expand Down Expand Up @@ -163,7 +163,7 @@ def __init__(self, working_dir: str, opts=None,
self.opts = opts

@classmethod
def is_valid_root(cls, path: str) -> bool:
def is_valid_root(cls, path: str, package=None) -> bool:
"""Return True if this build system can build the source in path."""
raise NotImplementedError

Expand Down
3 changes: 2 additions & 1 deletion src/rez/cli/_complete_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ def __call__(self, prefix, **kwargs):
return []

matching_names = []
names = (x for x in names if x.startswith(fileprefix))

for name in names:
if not name.startswith(fileprefix):
continue
filepath = os.path.join(path, name)
if os.path.isfile(filepath):
if not self.files:
Expand Down
4 changes: 3 additions & 1 deletion src/rez/cli/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""
The main command-line entry point.
"""
from __future__ import annotations

import sys
import importlib
from argparse import _StoreTrueAction, SUPPRESS
Expand Down Expand Up @@ -165,7 +167,7 @@ def run(command=None):
extra_arg_groups = []

if opts.debug or _env_var_true("REZ_DEBUG"):
exc_type = _NeverError
exc_type: type[RezError] = _NeverError
else:
exc_type = RezError

Expand Down
6 changes: 4 additions & 2 deletions src/rez/cli/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
'''
Run a benchmarking suite for runtime resolves.
'''
from __future__ import annotations

import json
import os
import os.path
Expand All @@ -17,8 +19,8 @@

# globals
opts = None
out_dir = None
pkg_repo_dir = None
out_dir: str | None = None
pkg_repo_dir: str | None = None


def setup_parser(parser, completions=False):
Expand Down
2 changes: 1 addition & 1 deletion src/rez/cli/complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _pop_arg(l, p):
cmds = [k for k, v in subcommands.items() if not v.get("hidden")]

if prefix:
cmds = (x for x in cmds if x.startswith(prefix))
cmds = [x for x in cmds if x.startswith(prefix)]
print(" ".join(cmds))

if subcommand not in subcommands:
Expand Down
9 changes: 8 additions & 1 deletion src/rez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import os
import re
import copy
from typing import Protocol
from typing import Protocol, TYPE_CHECKING


class Validatable(Protocol):
Expand Down Expand Up @@ -554,6 +554,13 @@ class Config(object, metaclass=LazyAttributeMeta):
schema = config_schema
schema_error = ConfigurationError

if TYPE_CHECKING:
# mypy: The use of LazyAttributeMeta means that this class generates hundreds
# of spurious attribute errors. Adding this for the type analysis will silence
# them until the use of LazyAttributeMeta can be addressed.
def __getattr__(self, item):
pass

def __init__(self, filepaths, overrides=None, locked=False):
"""Create a config.
Expand Down
4 changes: 2 additions & 2 deletions src/rez/developer_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def from_path(cls, path, format=None):
data = None

if format is None:
formats = (FileFormat.py, FileFormat.yaml)
formats = [FileFormat.py, FileFormat.yaml]
else:
formats = (format,)
formats = [format]

try:
mode = os.stat(path).st_mode
Expand Down
4 changes: 2 additions & 2 deletions src/rez/package_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _print_package_list(variants):
packages = set([x.parent for x in variants])
packages = sorted(packages, key=lambda x: x.name)

rows = [["PACKAGE", "URI"],
["-------", "---"]]
rows = [("PACKAGE", "URI"),
("-------", "---")]
rows += [(x.name, x.uri) for x in packages]
print('\n'.join(columnise(rows)))
21 changes: 12 additions & 9 deletions src/rez/package_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
# Copyright Contributors to the Rez Project


from __future__ import annotations

from functools import partial
import os.path
import shutil
import time

from rez.config import config
from rez.exceptions import PackageCopyError
from rez.package_repository import package_repository_manager
from rez.packages import Variant
from rez.package_repository import package_repository_manager, PackageRepository
from rez.packages import Package, Variant
from rez.serialise import FileFormat
from rez.utils import with_noop
from rez.utils.base26 import create_unique_base26_symlink
Expand All @@ -20,10 +22,11 @@
safe_makedirs, additive_copytree, make_path_writable, get_existing_path


def copy_package(package, dest_repository, variants=None, shallow=False,
dest_name=None, dest_version=None, overwrite=False, force=False,
follow_symlinks=False, dry_run=False, keep_timestamp=False,
skip_payload=False, overrides=None, verbose=False):
def copy_package(package: Package, dest_repository: PackageRepository,
variants: list[int] | None = None, shallow: bool = False,
dest_name=None, dest_version=None, overwrite: bool = False, force: bool = False,
follow_symlinks: bool = False, dry_run: bool = False, keep_timestamp: bool = False,
skip_payload: bool = False, overrides=None, verbose: bool = False):
"""Copy a package from one package repository to another.
This copies the package definition and payload. The package can also be
Expand Down Expand Up @@ -227,8 +230,8 @@ def finalize():
return finalize()


def _copy_variant_payload(src_variant, dest_pkg_repo, shallow=False,
follow_symlinks=False, overrides=None, verbose=False):
def _copy_variant_payload(src_variant: Variant, dest_pkg_repo: PackageRepository, shallow: bool = False,
follow_symlinks: bool = False, overrides=None, verbose: bool = False):
# Get payload path of source variant. For some types (eg from a "memory"
# type repo) there may not be a root.
#
Expand All @@ -243,7 +246,7 @@ def _copy_variant_payload(src_variant, dest_pkg_repo, shallow=False,
if not os.path.isdir(variant_root):
raise PackageCopyError(
"Cannot copy source variant %s - its root does not appear to "
"be present on disk (%s)." % src_variant.uri, variant_root
"be present on disk (%s)." % (src_variant.uri, variant_root)
)

dest_variant_name = overrides.get("name") or src_variant.name
Expand Down
6 changes: 4 additions & 2 deletions src/rez/package_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
from rez.package_resources import help_schema, _commands_schema, \
_function_schema, late_bound
from rez.package_repository import create_memory_package_repository
from rez.packages import Package
from rez.packages import Package, Variant
from rez.package_py_utils import expand_requirement
from rez.vendor.schema.schema import Schema, Optional, Or, Use, And
from rez.version import Version
from contextlib import contextmanager
import os
from typing import Iterable


# this schema will automatically harden request strings like 'python-*'; see
Expand Down Expand Up @@ -135,6 +136,7 @@ def get_package(self) -> Package:

# retrieve the package from the new repository
family_resource = repo.get_package_family(self.name)
assert family_resource is not None
it = repo.iter_packages(family_resource)
package_resource = next(it)

Expand Down Expand Up @@ -199,7 +201,7 @@ def make_package(name, path, make_base=None, make_root=None, skip_existing=True,
#

package = maker.get_package()
src_variants = []
src_variants: list[Variant] = []

# skip those variants that already exist
if skip_existing:
Expand Down
2 changes: 1 addition & 1 deletion src/rez/package_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def _calc_sort_key(self, package_name, version):
first_after = self._get_first_after(package_name)
if first_after is None:
# all packages are before T
is_before = True
is_before: bool | int = True
else:
is_before = int(version < first_after)

Expand Down
8 changes: 4 additions & 4 deletions src/rez/package_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import threading
import os.path
import time
from typing import Hashable, Iterator, TYPE_CHECKING
from typing import Any, Hashable, Iterator, TYPE_CHECKING

if TYPE_CHECKING:
from rez.package_resources import (PackageFamilyResource, PackageResource, PackageResourceHelper,
Expand Down Expand Up @@ -186,15 +186,15 @@ def iter_variants(self, package_resource: PackageResource) -> Iterator[VariantRe
"""
raise NotImplementedError

def get_package(self, name: str, version: Version) -> Package | None:
def get_package(self, name: str, version: Version) -> PackageResourceHelper | None:
"""Get a package.
Args:
name (str): Package name.
version (`Version`): Package version.
Returns:
`Package` or None: Matching package, or None if not found.
`PackageResourceHelper` or None: Matching package, or None if not found.
"""
fam = self.get_package_family(name)
if fam is None:
Expand Down Expand Up @@ -335,7 +335,7 @@ def on_variant_install_cancelled(self, variant_resource: VariantResource):
pass

def install_variant(self, variant_resource: VariantResource,
dry_run=False, overrides=None) -> VariantResource:
dry_run: bool = False, overrides: dict[str, Any] = None) -> VariantResource:
"""Install a variant into this repository.
Use this function to install a variant from some other package repository
Expand Down
2 changes: 1 addition & 1 deletion src/rez/package_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class PackageFamilyResource(PackageRepositoryResource):
this class. It must satisfy the schema `package_family_schema`.
"""

def iter_packages(self) -> Iterator[Package]:
def iter_packages(self) -> Iterator[PackageResourceHelper]:
raise NotImplementedError


Expand Down
4 changes: 2 additions & 2 deletions src/rez/package_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ def _on_variant_requires(self, variant, params):
# If the combined requirements, minus conflict requests, is equal to the
# variant's requirements, then this variant is selected.
#
reqs1 = RequirementList(x for x in reqlist if not x.conflict)
reqs2 = RequirementList(x for x in variant.variant_requires if not x.conflict)
reqs1 = RequirementList([x for x in reqlist if not x.conflict])
reqs2 = RequirementList([x for x in variant.variant_requires if not x.conflict])
return (reqs1 == reqs2)

def _get_test_info(self, test_name, variant):
Expand Down
16 changes: 13 additions & 3 deletions src/rez/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import os
import sys
from typing import Iterator, TYPE_CHECKING
from typing import overload, Any, Iterator, Literal, TYPE_CHECKING

if TYPE_CHECKING:
from rez.developer_package import DeveloperPackage
Expand Down Expand Up @@ -171,7 +171,7 @@ def _wrap_forwarded(self, key, value):
return value

def _eval_late_binding(self, sourcecode: SourceCode):
g = {}
g: dict[str, Any] = {}

if self.context is None:
g["in_context"] = lambda: False
Expand Down Expand Up @@ -917,7 +917,17 @@ def get_completions(prefix: str, paths: list[str] | None = None, family_only=Fal
return words


def get_latest_package(name: str, range_=None, paths: list[str] | None = None, error=False) -> Package | None:
@overload
def get_latest_package(name: str, *, range_=None, paths: list[str] | None = None, error: Literal[True] = True) -> Package:
pass


@overload
def get_latest_package(name: str, *, range_=None, paths: list[str] | None = None, error: Literal[False] = False) -> Package | None:
pass


def get_latest_package(name: str, *, range_=None, paths: list[str] | None = None, error: bool = False) -> Package | None:
"""Get the latest package for a given package name.
Args:
Expand Down
Loading

0 comments on commit 43aedad

Please sign in to comment.