summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2023-01-20 09:53:57 +0100
committerGitHub <noreply@github.com>2023-01-20 09:53:57 +0100
commit8f8394a829f883d48a18f811466b4765d9214d74 (patch)
tree799601c9be4a5c4957d292be33f55416ab233908
parentfb7c8d176809cb3511b1565720fb61a25c847f47 (diff)
downloadspack-8f8394a829f883d48a18f811466b4765d9214d74.tar.gz
spack-8f8394a829f883d48a18f811466b4765d9214d74.tar.bz2
spack-8f8394a829f883d48a18f811466b4765d9214d74.tar.xz
spack-8f8394a829f883d48a18f811466b4765d9214d74.zip
Add type hints to DependencySpec (#35021)
* DependencySpec: add type hints * DependencySpec: make deptypes a keyword only argument
-rw-r--r--lib/spack/spack/installer.py2
-rw-r--r--lib/spack/spack/spec.py32
2 files changed, 18 insertions, 16 deletions
diff --git a/lib/spack/spack/installer.py b/lib/spack/spack/installer.py
index 48fa6869d8..1b9800054d 100644
--- a/lib/spack/spack/installer.py
+++ b/lib/spack/spack/installer.py
@@ -233,7 +233,7 @@ def _packages_needed_to_bootstrap_compiler(compiler, architecture, pkgs):
dep.concretize()
# mark compiler as depended-on by the packages that use it
for pkg in pkgs:
- dep._dependents.add(spack.spec.DependencySpec(pkg.spec, dep, ("build",)))
+ dep._dependents.add(spack.spec.DependencySpec(pkg.spec, dep, deptypes=("build",)))
packages = [(s.package, False) for s in dep.traverse(order="post", root=False)]
packages.append((dep.package, True))
diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py
index 108bf94f9e..ebaefbcf27 100644
--- a/lib/spack/spack/spec.py
+++ b/lib/spack/spack/spec.py
@@ -56,6 +56,7 @@ import os
import re
import sys
import warnings
+from typing import Tuple
import ruamel.yaml as yaml
@@ -653,8 +654,9 @@ class CompilerSpec(object):
@lang.lazy_lexicographic_ordering
-class DependencySpec(object):
- """DependencySpecs connect two nodes in the DAG, and contain deptypes.
+class DependencySpec:
+ """DependencySpecs represent an edge in the DAG, and contain dependency types
+ and information on the virtuals being provided.
Dependencies can be one (or more) of several types:
@@ -662,20 +664,20 @@ class DependencySpec(object):
- link: is linked to and added to compiler flags.
- run: needs to be in the PATH for the package to run.
- Fields:
- - spec: Spec depended on by parent.
- - parent: Spec that depends on `spec`.
- - deptypes: list of strings, representing dependency relationships.
+ Args:
+ parent: starting node of the edge
+ spec: ending node of the edge.
+ deptypes: list of strings, representing dependency relationships.
"""
__slots__ = "parent", "spec", "deptypes"
- def __init__(self, parent, spec, deptypes):
+ def __init__(self, parent: "Spec", spec: "Spec", *, deptypes: dp.DependencyArgument):
self.parent = parent
self.spec = spec
self.deptypes = dp.canonical_deptype(deptypes)
- def update_deptypes(self, deptypes):
+ def update_deptypes(self, deptypes: dp.DependencyArgument) -> bool:
deptypes = set(deptypes)
deptypes.update(self.deptypes)
deptypes = tuple(sorted(deptypes))
@@ -684,10 +686,10 @@ class DependencySpec(object):
self.deptypes = deptypes
return changed
- def copy(self):
- return DependencySpec(self.parent, self.spec, self.deptypes)
+ def copy(self) -> "DependencySpec":
+ return DependencySpec(self.parent, self.spec, deptypes=self.deptypes)
- def add_type(self, type):
+ def add_type(self, type: dp.DependencyArgument):
self.deptypes = dp.canonical_deptype(self.deptypes + dp.canonical_deptype(type))
def _cmp_iter(self):
@@ -695,17 +697,17 @@ class DependencySpec(object):
yield self.spec.name if self.spec else None
yield self.deptypes
- def __str__(self):
+ def __str__(self) -> str:
return "%s %s--> %s" % (
self.parent.name if self.parent else None,
self.deptypes,
self.spec.name if self.spec else None,
)
- def canonical(self):
+ def canonical(self) -> Tuple[str, str, Tuple[str, ...]]:
return self.parent.dag_hash(), self.spec.dag_hash(), self.deptypes
- def flip(self):
+ def flip(self) -> "DependencySpec":
return DependencySpec(parent=self.spec, spec=self.parent, deptypes=self.deptypes)
@@ -1575,7 +1577,7 @@ class Spec(object):
edge.add_type(deptype)
return
- edge = DependencySpec(self, dependency_spec, deptype)
+ edge = DependencySpec(self, dependency_spec, deptypes=deptype)
self._dependencies.add(edge)
dependency_spec._dependents.add(edge)