diff options
author | Harmen Stoppels <me@harmenstoppels.nl> | 2023-11-06 19:22:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-06 10:22:29 -0800 |
commit | 1235084c20f1efabbca680c03f9f4dc023b44c5d (patch) | |
tree | 9ffad09e95686a17ca7cc030a5bc4eb1530dd1b5 /lib/spack/docs | |
parent | b5538960c325a849bddc35506e4c219cee40a1d8 (diff) | |
download | spack-1235084c20f1efabbca680c03f9f4dc023b44c5d.tar.gz spack-1235084c20f1efabbca680c03f9f4dc023b44c5d.tar.bz2 spack-1235084c20f1efabbca680c03f9f4dc023b44c5d.tar.xz spack-1235084c20f1efabbca680c03f9f4dc023b44c5d.zip |
Introduce `default_args` context manager (#39964)
This adds a rather trivial context manager that lets you deduplicate repeated
arguments in directives, e.g.
```python
depends_on("py-x@1", when="@1", type=("build", "run"))
depends_on("py-x@2", when="@2", type=("build", "run"))
depends_on("py-x@3", when="@3", type=("build", "run"))
depends_on("py-x@4", when="@4", type=("build", "run"))
```
can be condensed to
```python
with default_args(type=("build", "run")):
depends_on("py-x@1", when="@1")
depends_on("py-x@2", when="@2")
depends_on("py-x@3", when="@3")
depends_on("py-x@4", when="@4")
```
The advantage is it's clear for humans, the downside it's less clear for type checkers due to type erasure.
Diffstat (limited to 'lib/spack/docs')
-rw-r--r-- | lib/spack/docs/packaging_guide.rst | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/spack/docs/packaging_guide.rst b/lib/spack/docs/packaging_guide.rst index 3b05ce8932..3dd1c7952d 100644 --- a/lib/spack/docs/packaging_guide.rst +++ b/lib/spack/docs/packaging_guide.rst @@ -3503,6 +3503,56 @@ is equivalent to: Constraints from nested context managers are also combined together, but they are rarely needed or recommended. +.. _default_args: + +------------------------ +Common default arguments +------------------------ + +Similarly, if directives have a common set of default arguments, you can +group them together in a ``with default_args()`` block: + +.. code-block:: python + + class PyExample(PythonPackage): + + with default_args(type=("build", "run")): + depends_on("py-foo") + depends_on("py-foo@2:", when="@2:") + depends_on("py-bar") + depends_on("py-bz") + +The above is short for: + +.. code-block:: python + + class PyExample(PythonPackage): + + depends_on("py-foo", type=("build", "run")) + depends_on("py-foo@2:", when="@2:", type=("build", "run")) + depends_on("py-bar", type=("build", "run")) + depends_on("py-bz", type=("build", "run")) + +.. note:: + + The ``with when()`` context manager is composable, while ``with default_args()`` + merely overrides the default. For example: + + .. code-block:: python + + with default_args(when="+feature"): + depends_on("foo") + depends_on("bar") + depends_on("baz", when="+baz") + + is equivalent to: + + .. code-block:: python + + depends_on("foo", when="+feature") + depends_on("bar", when="+feature") + depends_on("baz", when="+baz") # Note: not when="+feature+baz" + .. _install-method: ------------------ |