Skip to content

Commit

Permalink
make compatible with python3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Jun 22, 2024
1 parent 08d2f97 commit e73c6c1
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 55 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.mypy]
files = ["src/rez/"]
files = ["src/rez/", "src/rezplugins/"]
exclude = [
'.*/rez/data/.*',
'.*/rez/vendor/.*',
Expand Down
14 changes: 9 additions & 5 deletions src/rez/build_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import argparse
import os.path
from typing import TYPE_CHECKING, TypedDict
from typing import TYPE_CHECKING


from rez.build_process import BuildType
Expand All @@ -15,16 +15,20 @@
from rez.rex_bindings import VariantBinding

if TYPE_CHECKING:
from typing import TypedDict # not available until python 3.8
from rez.developer_package import DeveloperPackage
from rez.resolved_context import ResolvedContext
from rez.packages import Package, Variant
from rez.rex import RexExecutor

# FIXME: move this out of TYPE_CHECKING block when python 3.7 support is dropped
class BuildResult(TypedDict, total=False):
success: bool
extra_files: list[str]
build_env_script: str

class BuildResult(TypedDict):
success: bool
extra_files: list[str]
build_env_script: str
else:
BuildResult = dict


def get_buildsys_types():
Expand Down
3 changes: 2 additions & 1 deletion src/rez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
from rez.vendor.schema.schema import Schema, SchemaError, And, Or, Use
from rez.vendor import yaml
from rez.vendor.yaml.error import YAMLError
from rez.utils.typing import Protocol
import rez.deprecations
from contextlib import contextmanager
from functools import lru_cache
from inspect import ismodule
import os
import re
import copy
from typing import Protocol, TYPE_CHECKING
from typing import TYPE_CHECKING


class Validatable(Protocol):
Expand Down
2 changes: 1 addition & 1 deletion src/rez/deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def warn(message, category, pre_formatted=False, stacklevel=1, filename=None, **
original_formatwarning = warnings.formatwarning
if pre_formatted:

def formatwarning(_, category, *args, **kwargs):
def formatwarning(_, category, *args, **kwargs) -> str:
return "{0}{1}: {2}\n".format(
"{0}: ".format(filename) if filename else "", category.__name__, message
)
Expand Down
17 changes: 12 additions & 5 deletions src/rez/package_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
# Copyright Contributors to the Rez Project


from __future__ import annotations

from rez.packages import iter_packages
from rez.exceptions import ConfigurationError
from rez.config import config
from rez.utils.data_utils import cached_property, cached_class_property
from rez.version import VersionedObject, Requirement
from hashlib import sha1
from typing import Pattern
import fnmatch
import re

Expand Down Expand Up @@ -328,6 +331,7 @@ class Rule(object):

#: Rule name
name: str
_family: str | None

def match(self, package):
"""Apply the rule to the package.
Expand All @@ -340,7 +344,7 @@ def match(self, package):
"""
raise NotImplementedError

def family(self):
def family(self) -> str | None:
"""Returns a package family string if this rule only applies to a given
package family, otherwise None.
Expand Down Expand Up @@ -412,7 +416,7 @@ def _parse_label(cls, txt):
return None, txt

@classmethod
def _extract_family(cls, txt):
def _extract_family(cls, txt) -> str | None:
m = cls.family_re.match(txt)
if m:
return m.group()[:-1]
Expand All @@ -426,7 +430,10 @@ def __repr__(self):


class RegexRuleBase(Rule):
def match(self, package):
regex: Pattern[str]
txt: str

def match(self, package) -> bool:
return bool(self.regex.match(package.qualified_name))

def cost(self):
Expand All @@ -448,7 +455,7 @@ class RegexRule(RegexRuleBase):
"""
name = "regex"

def __init__(self, s):
def __init__(self, s: str):
"""Create a regex rule.
Args:
Expand All @@ -466,7 +473,7 @@ class GlobRule(RegexRuleBase):
"""
name = "glob"

def __init__(self, s):
def __init__(self, s: str):
"""Create a glob rule.
Args:
Expand Down
8 changes: 2 additions & 6 deletions src/rez/package_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

from inspect import isclass
from hashlib import sha1
from typing import Any, Callable, Iterable, List, Protocol, TYPE_CHECKING
from typing import Any, Callable, Iterable, List, TYPE_CHECKING

from rez.config import config
from rez.utils.data_utils import cached_class_property
from rez.version import Version, VersionRange
from rez.version._version import _Comparable, _ReversedComparable, _LowerBound, _UpperBound, _Bound
from rez.packages import iter_packages, Package
from rez.utils.typing import SupportsLessThan

if TYPE_CHECKING:
# this is not available in typing until 3.11, but due to __future__.annotations
Expand All @@ -22,11 +23,6 @@
ALL_PACKAGES = "*"


class SupportsLessThan(Protocol):
def __lt__(self, __other: Any) -> bool:
pass


class FallbackComparable(_Comparable):
"""First tries to compare objects using the main_comparable, but if that
fails, compares using the fallback_comparable object.
Expand Down
6 changes: 4 additions & 2 deletions src/rez/package_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,10 @@ def on_variant_install_cancelled(self, variant_resource: VariantResource):
"""
pass

def install_variant(self, variant_resource: VariantResource,
dry_run: bool = False, overrides: dict[str, Any] = None) -> VariantResource:
def install_variant(self,
variant_resource: VariantResource,
dry_run: bool = False,
overrides: dict[str, Any] | None = None) -> VariantResource:
"""Install a variant into this repository.
Use this function to install a variant from some other package repository
Expand Down
3 changes: 2 additions & 1 deletion src/rez/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

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

if TYPE_CHECKING:
from typing import Literal # not available in typing module until 3.8
from rez.developer_package import DeveloperPackage
from rez.version import Requirement
from rez.package_repository import PackageRepository
Expand Down
2 changes: 2 additions & 0 deletions src/rez/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def _set_cached_solve(self, solver_dict):
release_times_dict = {}
variant_states_dict = {}

assert self.resolved_packages_ is not None, \
"self.resolved_packages_ is set in _set_result when status is 'solved'"
for variant in self.resolved_packages_:
time_ = get_last_release_time(variant.name, self.package_paths)

Expand Down
13 changes: 2 additions & 11 deletions src/rez/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
PackageFamilyNotFoundError, RezSystemError
from rez.version import Version, VersionRange
from rez.version import VersionedObject, Requirement, RequirementList
from rez.utils.typing import SupportsLessThan, Protocol
from contextlib import contextmanager
from enum import Enum
from itertools import product, chain
from typing import Any, Callable, Generator, Protocol, Iterator, TypeVar, TYPE_CHECKING
from typing import Any, Callable, Generator, Iterator, TypeVar, TYPE_CHECKING
import copy
import time
import sys
Expand All @@ -43,16 +44,6 @@
T = TypeVar("T")


class SupportsLessThan(Protocol):
def __lt__(self, __other: Any) -> bool:
pass


class SupportsWrite(Protocol):
def write(self, __s: str) -> object:
pass


# a hidden control for forcing to non-optimized solving mode. This is here as
# first port of call for narrowing down the cause of a solver bug if we see one
#
Expand Down
18 changes: 16 additions & 2 deletions src/rez/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@
from rez.vendor.yaml.error import YAMLError
from rez.utils.yaml import dump_yaml
from collections import defaultdict
from typing import TYPE_CHECKING
import os
import os.path
import shutil
import sys


if TYPE_CHECKING:
from typing import TypedDict

# FIXME: move this out of TYPE_CHECKING block when python 3.7 support is dropped
class Tool(TypedDict):
tool_name: str
tool_alias: str
context_name: str
variant: int
else:
Tool = dict


class Suite(object):
"""A collection of contexts.
Expand Down Expand Up @@ -47,7 +61,7 @@ def __init__(self):
self.next_priority = 1

self.tools = None
self.tool_conflicts = None
self.tool_conflicts: defaultdict[str, list[Tool]] | None = None
self.hidden_tools = None

@property
Expand Down Expand Up @@ -725,7 +739,7 @@ def _update_tools(self):
if alias is None:
alias = "%s%s%s" % (prefix, tool_name, suffix)

entry = dict(tool_name=tool_name,
entry = Tool(tool_name=tool_name,
tool_alias=alias,
context_name=context_name,
variant=variant)
Expand Down
4 changes: 2 additions & 2 deletions src/rez/utils/patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def get_patched_request(requires, patchlist):
'^': (True, True, True)
}

requires = [Requirement(x) if not isinstance(x, Requirement) else x
for x in requires]
requires: list[Requirement | None] = [
Requirement(x) if not isinstance(x, Requirement) else x for x in requires]
appended = []

for patch in patchlist:
Expand Down
1 change: 1 addition & 0 deletions src/rez/utils/platform_.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ def _difftool(self):


# singleton
# FIXME: is is valid for platform_ to be None?
platform_ = None
name = platform.system().lower()
if name == "linux":
Expand Down
3 changes: 2 additions & 1 deletion src/rez/utils/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Utilities for working with dict-based schemas.
"""
from rez.vendor.schema.schema import Schema, Optional, Use, And
from rez.config import Validatable


# an alias which just so happens to be the same number of characters as
Expand Down Expand Up @@ -68,7 +69,7 @@ def _to(value):
d[k] = _to(v)
if allow_custom_keys:
d[Optional(str)] = modifier or object
schema = Schema(d)
schema: Validatable = Schema(d)
elif modifier:
schema = And(value, modifier)
else:
Expand Down
20 changes: 20 additions & 0 deletions src/rez/utils/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project


from __future__ import absolute_import, print_function

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
# FIXME: use typing.Protocol instead of this workaround when python 3.7 support is dropped
from typing import Protocol

else:
class Protocol(object):
pass


class SupportsLessThan(Protocol):
def __lt__(self, __other: Any) -> bool:
pass
Loading

0 comments on commit e73c6c1

Please sign in to comment.