Skip to content

Commit

Permalink
Add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Jul 1, 2024
1 parent 6c36578 commit efcae39
Show file tree
Hide file tree
Showing 52 changed files with 1,569 additions and 1,015 deletions.
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.mypy]
files = ["src/rez/", "src/rezplugins/"]
exclude = [
'.*/rez/data/.*',
'.*/rez/vendor/.*',
'.*/rez/tests/.*',
'.*/rez/utils/lint_helper.py',
]
disable_error_code = ["var-annotated", "import-not-found"]
check_untyped_defs = true
# allow this for now:
allow_redefinition = true

[[tool.mypy.overrides]]
module = 'rez.utils.lint_helper'
follow_imports = "skip"
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:
signal.signal(signal.SIGUSR1, callback) # Register handler


Expand Down
70 changes: 46 additions & 24 deletions src/rez/build_process.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.packages import iter_packages
from rez.exceptions import BuildProcessError, BuildContextResolveError, \
ReleaseHookCancellingError, RezError, ReleaseError, BuildError, \
Expand All @@ -18,6 +20,13 @@
import getpass
import os.path
import sys
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from rez.build_system import BuildSystem
from rez.packages import Package, Variant
from rez.release_vcs import ReleaseVCS
from rez.developer_package import DeveloperPackage


debug_print = config.debug_printer("package_release")
Expand All @@ -29,9 +38,16 @@ def get_build_process_types():
return plugin_manager.get_plugins('build_process')


def create_build_process(process_type, working_dir, build_system, package=None,
vcs=None, ensure_latest=True, skip_repo_errors=False,
ignore_existing_tag=False, verbose=False, quiet=False):
def create_build_process(process_type: str,
working_dir: str,
build_system: BuildSystem,
package=None,
vcs: ReleaseVCS | None = None,
ensure_latest: bool = True,
skip_repo_errors: bool = False,
ignore_existing_tag: bool = False,
verbose: bool = False,
quiet: bool = False) -> BuildProcess:
"""Create a :class:`BuildProcess` instance.
.. warning::
Expand All @@ -44,7 +60,7 @@ def create_build_process(process_type, working_dir, build_system, package=None,
if process_type not in process_types:
raise BuildProcessError("Unknown build process: %r" % process_type)

cls = plugin_manager.get_plugin_class('build_process', process_type)
cls = plugin_manager.get_plugin_class('build_process', process_type, BuildProcess)

return cls(working_dir, # ignored (deprecated)
build_system,
Expand Down Expand Up @@ -77,7 +93,8 @@ class BuildProcess(object):
def name(cls):
raise NotImplementedError

def __init__(self, working_dir, build_system, package=None, vcs=None,
def __init__(self, working_dir: str, build_system: BuildSystem, package=None,
vcs: ReleaseVCS | None = None,
ensure_latest=True, skip_repo_errors=False,
ignore_existing_tag=False, verbose=False, quiet=False):
"""Create a BuildProcess.
Expand Down Expand Up @@ -119,14 +136,15 @@ def __init__(self, working_dir, build_system, package=None, vcs=None,
self.package.config.build_directory)

@property
def package(self):
def package(self) -> DeveloperPackage:
return self.build_system.package

@property
def working_dir(self):
def working_dir(self) -> str:
return self.build_system.working_dir

def build(self, install_path=None, clean=False, install=False, variants=None):
def build(self, install_path: str | None = None, clean: bool = False,
install: bool = False, variants: list[int] | None = None) -> int:
"""Perform the build process.
Iterates over the package's variants, resolves the environment for
Expand All @@ -149,7 +167,8 @@ def build(self, install_path=None, clean=False, install=False, variants=None):
"""
raise NotImplementedError

def release(self, release_message=None, variants=None):
def release(self, release_message: str | None = None,
variants: list[int] | None = None) -> int:
"""Perform the release process.
Iterates over the package's variants, building and installing each into
Expand All @@ -167,7 +186,7 @@ def release(self, release_message=None, variants=None):
"""
raise NotImplementedError

def get_changelog(self):
def get_changelog(self) -> str | None:
"""Get the changelog since last package release.
Returns:
Expand All @@ -187,7 +206,8 @@ 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=None, **kwargs):
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:
present_variants = range(self.package.num_variants)
Expand Down Expand Up @@ -215,7 +235,7 @@ def visit_variants(self, func, variants=None, **kwargs):

return num_visited, results

def get_package_install_path(self, path):
def get_package_install_path(self, path: str) -> str:
"""Return the installation path for a package (where its payload goes).
Args:
Expand All @@ -230,7 +250,8 @@ def get_package_install_path(self, path):
package_version=self.package.version
)

def create_build_context(self, variant, build_type, build_path):
def create_build_context(self, variant: Variant, build_type: BuildType,
build_path: str) -> tuple[ResolvedContext, str]:
"""Create a context to build the variant within."""
request = variant.get_requires(build_requires=True,
private_build_requires=True)
Expand Down Expand Up @@ -274,7 +295,7 @@ def create_build_context(self, variant, build_type, build_path):
raise BuildContextResolveError(context)
return context, rxt_filepath

def pre_release(self):
def pre_release(self) -> None:
release_settings = self.package.config.plugins.release_vcs

# test that the release path exists
Expand Down Expand Up @@ -322,7 +343,7 @@ def pre_release(self):
else:
break

def post_release(self, release_message=None):
def post_release(self, release_message=None) -> None:
tag_name = self.get_current_tag_name()

if self.vcs is None:
Expand All @@ -332,7 +353,7 @@ def post_release(self, release_message=None):
with self.repo_operation():
self.vcs.create_release_tag(tag_name=tag_name, message=release_message)

def get_current_tag_name(self):
def get_current_tag_name(self) -> str:
release_settings = self.package.config.plugins.release_vcs
try:
tag_name = self.package.format(release_settings.tag_name)
Expand All @@ -342,7 +363,7 @@ def get_current_tag_name(self):
tag_name = "unversioned"
return tag_name

def run_hooks(self, hook_event, **kwargs):
def run_hooks(self, hook_event, **kwargs) -> None:
hook_names = self.package.config.release_hooks or []
hooks = create_release_hooks(hook_names, self.working_dir)

Expand All @@ -357,12 +378,12 @@ def run_hooks(self, hook_event, **kwargs):
"%s cancelled by %s hook '%s': %s:\n%s"
% (hook_event.noun, hook_event.label, hook.name(),
e.__class__.__name__, str(e)))
except RezError:
except RezError as e:
debug_print("Error in %s hook '%s': %s:\n%s"
% (hook_event.label, hook.name(),
e.__class__.__name__, str(e)))

def get_previous_release(self):
def get_previous_release(self) -> Package | None:
release_path = self.package.config.release_packages_path
it = iter_packages(self.package.name, paths=[release_path])
packages = sorted(it, key=lambda x: x.version, reverse=True)
Expand All @@ -372,18 +393,19 @@ def get_previous_release(self):
return package
return None

def get_changelog(self):
def get_changelog(self) -> str | None:
previous_package = self.get_previous_release()
if previous_package:
previous_revision = previous_package.revision
else:
previous_revision = None

changelog = None
with self.repo_operation():
changelog = self.vcs.get_changelog(
previous_revision,
max_revisions=config.max_package_changelog_revisions)
if self.vcs:
with self.repo_operation():
changelog = self.vcs.get_changelog(
previous_revision,
max_revisions=config.max_package_changelog_revisions)

return changelog

Expand Down
Loading

0 comments on commit efcae39

Please sign in to comment.