Skip to content

Commit

Permalink
More types
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed May 22, 2024
1 parent 614c1f6 commit 4fd0bac
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 173 deletions.
17 changes: 10 additions & 7 deletions src/rez/build_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rez.developer_package import DeveloperPackage
from rez.resolved_context import ResolvedContext
from rez.packages import Package, Variant
from rez.rex import RexExecutor


class BuildResult(TypedDict):
Expand Down Expand Up @@ -128,7 +129,7 @@ def name(cls) -> str:

def __init__(self, working_dir: str, opts=None,
package: DeveloperPackage | None = None,
write_build_scripts=False, verbose=False, build_args=[],
write_build_scripts: bool = False, verbose: bool = False, build_args=[],
child_build_args=[]):
"""Create a build system instance.
Expand Down Expand Up @@ -182,13 +183,13 @@ def child_build_system(cls) -> str | None:
return None

@classmethod
def bind_cli(cls, parser: argparse.ArgumentParser, group):
def bind_cli(cls, parser: argparse.ArgumentParser, group: argparse._ArgumentGroup):
"""Expose parameters to an argparse.ArgumentParser that are specific
to this build system.
Args:
parser (`ArgumentParser`): Arg parser.
group (`ArgumentGroup`): Arg parser group - you should add args to
group (`_ArgumentGroup`): Arg parser group - you should add args to
this, NOT to `parser`.
"""
pass
Expand Down Expand Up @@ -230,8 +231,9 @@ def build(self,
raise NotImplementedError

@classmethod
def set_standard_vars(cls, executor, context, variant, build_type, install,
build_path, install_path=None):
def set_standard_vars(cls, executor: RexExecutor, context: ResolvedContext,
variant: Variant, build_type: BuildType, install: bool, build_path: str,
install_path: str | None = None) -> None:
"""Set some standard env vars that all build systems can rely on.
"""
from rez.config import config
Expand Down Expand Up @@ -318,8 +320,9 @@ def add_pre_build_commands(cls, executor, variant, build_type, install,
executor.execute_code(pre_build_commands)

@classmethod
def add_standard_build_actions(cls, executor, context, variant, build_type,
install, build_path, install_path=None):
def add_standard_build_actions(cls, executor: RexExecutor, context: ResolvedContext, variant: Variant,
build_type: BuildType, install: bool, build_path: str,
install_path: str | None = None) -> None:
"""Perform build actions common to every build system.
"""

Expand Down
4 changes: 3 additions & 1 deletion src/rez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright Contributors to the Rez Project


from __future__ import annotations

from rez import __version__
from rez.utils.data_utils import AttrDictWrapper, RO_AttrDictWrapper, \
convert_dicts, cached_property, cached_class_property, LazyAttributeMeta, \
Expand Down Expand Up @@ -752,7 +754,7 @@ def _data(self):
return data

@classmethod
def _create_main_config(cls, overrides=None):
def _create_main_config(cls, overrides=None) -> Config:
"""See comment block at top of 'rezconfig' describing how the main
config is assembled."""
filepaths = []
Expand Down
2 changes: 1 addition & 1 deletion src/rez/package_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@

class PackageMaker(AttrDictWrapper):
"""Utility class for creating packages."""
def __init__(self, name: str, data=None, package_cls=None):
def __init__(self, name: str, data=None, package_cls: type[Package] | None = None):
"""Create a package maker.
Args:
Expand Down
13 changes: 6 additions & 7 deletions src/rez/package_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from typing import Hashable, Iterator, TYPE_CHECKING

if TYPE_CHECKING:
from rez.package_resources import PackageFamilyResource, PackageResource, VariantResource
from rez.package_resources import (PackageFamilyResource, PackageResource, PackageResourceHelper,
VariantResource, PackageRepositoryResource)
from rez.packages import Package
from rez.utils.resources import Resource
from rez.version import Version
Expand Down Expand Up @@ -375,8 +376,7 @@ def get_equivalent_variant(self, variant_resource: VariantResource) -> VariantRe
"""
return self.install_variant(variant_resource, dry_run=True)

def get_parent_package_family(self, package_resource: VariantResource
) -> PackageFamilyResource:
def get_parent_package_family(self, package_resource: PackageResourceHelper) -> PackageFamilyResource:
"""Get the parent package family of the given package.
Args:
Expand All @@ -387,8 +387,7 @@ def get_parent_package_family(self, package_resource: VariantResource
"""
raise NotImplementedError

def get_parent_package(self, variant_resource: PackageFamilyResource
) -> PackageResource:
def get_parent_package(self, variant_resource: VariantResource) -> PackageRepositoryResource:
"""Get the parent package of the given variant.
Args:
Expand Down Expand Up @@ -431,7 +430,7 @@ def get_last_release_time(self, package_family_resource: PackageFamilyResource
"""
return 0

def make_resource_handle(self, resource_key, **variables) -> ResourceHandle:
def make_resource_handle(self, resource_key: str, **variables) -> ResourceHandle:
"""Create a `ResourceHandle`
Nearly all `ResourceHandle` creation should go through here, because it
Expand All @@ -455,7 +454,7 @@ def make_resource_handle(self, resource_key, **variables) -> ResourceHandle:
variables = resource_cls.normalize_variables(variables)
return ResourceHandle(resource_key, variables)

def get_resource(self, resource_key, **variables) -> Resource:
def get_resource(self, resource_key: str, **variables) -> Resource:
"""Get a resource.
Attempts to get and return a cached version of the resource if
Expand Down
5 changes: 4 additions & 1 deletion src/rez/package_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,11 @@ def subpath(self) -> str:
"""
return self._subpath()

@abstractmethod
def _root(self, ignore_shortlinks=False):
raise NotImplementedError

@abstractmethod
def _subpath(self, ignore_shortlinks=False):
raise NotImplementedError

Expand All @@ -397,6 +399,7 @@ def _subpath(self, ignore_shortlinks=False):
class PackageResourceHelper(PackageResource):
"""PackageResource with some common functionality included.
"""
# the resource key for a VariantResourceHelper subclass
variant_key: str

if TYPE_CHECKING:
Expand Down Expand Up @@ -428,7 +431,7 @@ def pre_commands(self) -> SourceCode:
def post_commands(self) -> SourceCode:
return self._convert_to_rex(self._post_commands)

def iter_variants(self) -> Iterator[Variant]:
def iter_variants(self) -> Iterator[VariantResourceHelper]:
num_variants = len(self.variants or [])

if num_variants == 0:
Expand Down
15 changes: 8 additions & 7 deletions src/rez/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from rez.developer_package import DeveloperPackage
from rez.version import Requirement
from rez.package_repository import PackageRepository
from rez.resolved_context import ResolvedContext


# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -63,7 +64,7 @@ class PackageFamily(PackageRepositoryResourceWrapper):
"""
keys = schema_keys(package_family_schema)

def __init__(self, resource):
def __init__(self, resource: PackageFamilyResource):
_check_class(resource, PackageFamilyResource)
super(PackageFamily, self).__init__(resource)

Expand All @@ -84,14 +85,14 @@ class PackageBaseResourceWrapper(PackageRepositoryResourceWrapper):
"requires": late_requires_schema
}

def __init__(self, resource, context=None):
def __init__(self, resource: PackageResource | VariantResource, context: ResolvedContext | None = None):
super(PackageBaseResourceWrapper, self).__init__(resource)
self.context = context

# cached results of late-bound funcs
self._late_binding_returnvalues = {}

def set_context(self, context):
def set_context(self, context: ResolvedContext):
self.context = context

def arbitrary_keys(self):
Expand Down Expand Up @@ -169,7 +170,7 @@ def _wrap_forwarded(self, key, value):
else:
return value

def _eval_late_binding(self, sourcecode):
def _eval_late_binding(self, sourcecode: SourceCode):
g = {}

if self.context is None:
Expand Down Expand Up @@ -209,7 +210,7 @@ class Package(PackageBaseResourceWrapper):
#: funcs, where ``this`` may be a package or variant.
is_variant = False

def __init__(self, resource, context=None):
def __init__(self, resource: PackageResource, context=None):
_check_class(resource, PackageResource)
super(Package, self).__init__(resource, context)

Expand Down Expand Up @@ -347,7 +348,7 @@ class Variant(PackageBaseResourceWrapper):
#: See :attr:`Package.is_variant`.
is_variant = True

def __init__(self, resource, context=None, parent=None):
def __init__(self, resource: VariantResource, context=None, parent=None):
_check_class(resource, VariantResource)
super(Variant, self).__init__(resource, context)
self._parent = parent
Expand Down Expand Up @@ -916,7 +917,7 @@ 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):
def get_latest_package(name: str, range_=None, paths: list[str] | None = None, error=False) -> Package | None:
"""Get the latest package for a given package name.
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/rez/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class InstallMode(Enum):
min_deps = 1


def run_pip_command(command_args, pip_version=None, python_version=None):
def run_pip_command(command_args, pip_version=None, python_version=None) -> Popen:
"""Run a pip command.
Args:
command_args (list of str): Args to pip.
Expand Down
Loading

0 comments on commit 4fd0bac

Please sign in to comment.