Introduce default_args context manager#39964
Conversation
default_args context manager
tgamblin
left a comment
There was a problem hiding this comment.
This looks awesome to me, but can you make it so that with when is implemented using the same mechanism? i.e., shouldn’t this:
with when(“@2:”):become syntactic sugar for:
with default_args(when=“@2:”):?
I think when is special enough to keep, and it’s more readable for its special case. But we don’t need two code paths in the implementation for basically the same thing.
|
Hm, it's not exactly the same, since What's expected here: with default_args(type="build"), default_args(type=("link", "run")):
depends_on("x")is that equivalent to depends_on("x", type=("build", "link", "run"))or depends_on("x", type=("link", "run")) |
|
@tgamblin
This: with when("+foo"):
with when("+bar"):
depends_on("xxx", when="+baz")is equivalent to: depends_on("xxx", when="+foo+bar+baz") |
|
Ok, I agree with @alalazo and that's a great reason why Are there any other default args worth having special semantics for? I can't think of any off the top of my head that I'd justify implementing semantics other than override for. but RE: @haampie, this is awful: with default_args(type="build"), default_args(type=("link", "run")):
depends_on("x")I think most people would expect that to be equivalent to I could see you wanting an override if the second context manager were in the middle of some statements like: with default_args(when="@2.0:", type="build"):
depends_on("x")
with default_args(type=("link", "run")):
depends_on("y")
depends_on("z")but I think we should just encourage people not to do this. I worry about people composing with default_args(when="@2.0:", type="build"):
depends_on("x")
with when("@3.0:"):
depends_on("y")
depends_on("z")or: with default_args(when="@2.0:", type="build"):
depends_on("x")
with when("@3.0:"):
depends_on("y")
depends_on("z")Should this be disallowed? I kind of think we should just raise and recommend using Finally, I think regardless of what we settle on, the semantics need to be documented with a note saying how they're different from |
Would love to be able to use |
|
@tgamblin should be good to go? For documentation see https://spack--39964.org.readthedocs.build/en/39964/packaging_guide.html#common-default-arguments |
|
And this one too @tgamblin :D |
| depends_on("[email protected]:", when="+colorama") | ||
| depends_on("[email protected]:", when="+uvloop") | ||
| depends_on("[email protected]:", when="+d") | ||
| depends_on("[email protected]:", when="+jupyter") | ||
| depends_on("[email protected]:", when="+jupyter") |
There was a problem hiding this comment.
These should be indented...
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.
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.
This adds a rather trivial context manager that lets you deduplicate repeated
arguments in directives, e.g.
can be condensed to
The advantage is it's clear for humans, the downside it's less clear for type checkers due to type erasure