summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2023-01-05 17:48:29 +0100
committerGitHub <noreply@github.com>2023-01-05 17:48:29 +0100
commiteace2ebb08c9da7954ae09a4b8aefc72d695eff1 (patch)
tree488e6ea702c77194e0e862b2e777bfd3e9a9ac6c
parent441c0a4fee2028ec2fe29842b52b91b74d11687f (diff)
downloadspack-eace2ebb08c9da7954ae09a4b8aefc72d695eff1.tar.gz
spack-eace2ebb08c9da7954ae09a4b8aefc72d695eff1.tar.bz2
spack-eace2ebb08c9da7954ae09a4b8aefc72d695eff1.tar.xz
spack-eace2ebb08c9da7954ae09a4b8aefc72d695eff1.zip
spack.dependency: add type hints (#34807)
-rw-r--r--lib/spack/spack/dependency.py43
-rw-r--r--lib/spack/spack/graph.py2
2 files changed, 27 insertions, 18 deletions
diff --git a/lib/spack/spack/dependency.py b/lib/spack/spack/dependency.py
index bdf660fd1d..177dff1c19 100644
--- a/lib/spack/spack/dependency.py
+++ b/lib/spack/spack/dependency.py
@@ -3,6 +3,8 @@
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
"""Data structures that represent Spack's dependency relationships."""
+from typing import Dict, List, Optional, Set, Tuple, Union
+
import spack.spec
#: The types of dependency relationships that Spack understands.
@@ -11,8 +13,11 @@ all_deptypes = ("build", "link", "run", "test")
#: Default dependency type if none is specified
default_deptype = ("build", "link")
+#: Type hint for the arguments accepting a dependency type
+DependencyArgument = Union[str, List[str], Tuple[str, ...]]
+
-def deptype_chars(*type_tuples):
+def deptype_chars(*type_tuples: str) -> str:
"""Create a string representing deptypes for many dependencies.
The string will be some subset of 'blrt', like 'bl ', 'b t', or
@@ -24,7 +29,7 @@ def deptype_chars(*type_tuples):
whether ANY dpeendency in the list has the deptypes (so the deptypes
are merged).
"""
- types = set()
+ types: Set[str] = set()
for t in type_tuples:
if t:
types.update(t)
@@ -32,13 +37,12 @@ def deptype_chars(*type_tuples):
return "".join(t[0] if t in types else " " for t in all_deptypes)
-def canonical_deptype(deptype):
+def canonical_deptype(deptype: DependencyArgument) -> Tuple[str, ...]:
"""Convert deptype to a canonical sorted tuple, or raise ValueError.
Args:
- deptype (str or list or tuple): string representing dependency
- type, or a list/tuple of such strings. Can also be the
- builtin function ``all`` or the string 'all', which result in
+ deptype: string representing dependency type, or a list/tuple of such strings.
+ Can also be the builtin function ``all`` or the string 'all', which result in
a tuple of all dependency types known to Spack.
"""
if deptype in ("all", all):
@@ -58,7 +62,7 @@ def canonical_deptype(deptype):
raise ValueError("Invalid dependency type: %s" % repr(deptype))
-class Dependency(object):
+class Dependency:
"""Class representing metadata for a dependency on a package.
This class differs from ``spack.spec.DependencySpec`` because it
@@ -85,13 +89,18 @@ class Dependency(object):
"""
- def __init__(self, pkg, spec, type=default_deptype):
+ def __init__(
+ self,
+ pkg: "spack.package_base.PackageBase",
+ spec: "spack.spec.Spec",
+ type: Optional[Tuple[str, ...]] = default_deptype,
+ ):
"""Create a new Dependency.
Args:
- pkg (type): Package that has this dependency
- spec (Spec): Spec indicating dependency requirements
- type (sequence): strings describing dependency relationship
+ pkg: Package that has this dependency
+ spec: Spec indicating dependency requirements
+ type: strings describing dependency relationship
"""
assert isinstance(spec, spack.spec.Spec)
@@ -100,7 +109,7 @@ class Dependency(object):
# This dict maps condition specs to lists of Patch objects, just
# as the patches dict on packages does.
- self.patches = {}
+ self.patches: Dict[spack.spec.Spec, "List[spack.patch.Patch]"] = {}
if type is None:
self.type = set(default_deptype)
@@ -108,11 +117,11 @@ class Dependency(object):
self.type = set(type)
@property
- def name(self):
+ def name(self) -> str:
"""Get the name of the dependency package."""
return self.spec.name
- def merge(self, other):
+ def merge(self, other: "Dependency"):
"""Merge constraints, deptypes, and patches of other into self."""
self.spec.constrain(other.spec)
self.type |= other.type
@@ -125,6 +134,6 @@ class Dependency(object):
else:
self.patches[cond] = other.patches[cond]
- def __repr__(self):
- types = deptype_chars(self.type)
- return "<Dependency: %s -> %s [%s]>" % (self.pkg.name, self.spec, types)
+ def __repr__(self) -> str:
+ types = deptype_chars(*self.type)
+ return f"<Dependency: {self.pkg.name} -> {self.spec} [{types}]>"
diff --git a/lib/spack/spack/graph.py b/lib/spack/spack/graph.py
index 3187334b90..d70c7ff46a 100644
--- a/lib/spack/spack/graph.py
+++ b/lib/spack/spack/graph.py
@@ -569,7 +569,7 @@ def static_graph_dot(
def graph_dot(
specs: List[spack.spec.Spec],
builder: Optional[DotGraphBuilder] = None,
- deptype: Optional[Union[str, Tuple[str, ...]]] = "all",
+ deptype: spack.dependency.DependencyArgument = "all",
out: Optional[TextIO] = None,
):
"""DOT graph of the concrete specs passed as input.