New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get config item if variable is None #6862
Conversation
Another idea is Another idea is to add another option to diff --git a/dask/config.py b/dask/config.py
index 27d8492b..d6f94057 100644
--- a/dask/config.py
+++ b/dask/config.py
@@ -425,7 +425,9 @@ def refresh(config=config, defaults=defaults, **kwargs):
def get(key, default=no_default, config=config):
"""
- Get elements from global config
+ Get elements from global config. If tuple is passed instead of key,
+ return first item in tuple if not None, else return config value for
+ second item.
Use '.' for nested access
@@ -441,10 +443,16 @@ def get(key, default=no_default, config=config):
>>> config.get('foo.x.y', default=123) # doctest: +SKIP
123
+ >>> config.get((4, 'foo.x.y)) # doctest: +SKIP
+ 4
See Also
--------
dask.config.set
"""
+ if isinstance(key, tuple):
+ value, key = key
+ if value is not None:
+ return value
keys = key.split(".")
result = config
for k in keys: |
I like the idea of incorporating this into The tuple may be a little opaque to others when reading a piece of code. I wonder if a kwarg would be better? But I guess the trouble with a kwarg is the ordering. |
Yeah what if the diff --git a/dask/config.py b/dask/config.py
index 27d8492b..3da2526c 100644
--- a/dask/config.py
+++ b/dask/config.py
@@ -423,9 +423,12 @@ def refresh(config=config, defaults=defaults, **kwargs):
update(config, collect(**kwargs))
-def get(key, default=no_default, config=config):
+def get(*args, default=no_default, config=config):
"""
- Get elements from global config
+ Get elements from global config.
+
+ If multiple args are passed, the first item is returned if not None,
+ else return config value for second item (the key).
Use '.' for nested access
@@ -441,10 +444,19 @@ def get(key, default=no_default, config=config):
>>> config.get('foo.x.y', default=123) # doctest: +SKIP
123
+ >>> config.get(4, 'foo.x.y')
+ 4
+
See Also
--------
dask.config.set
"""
+ if len(args) == 1:
+ key, = args
+ elif len(args) == 2:
+ value, key = args
+ if value is not None:
+ return value
keys = key.split(".")
result = config
for k in keys: |
Ok one more idea. We could abuse the |
Yeah I like this. You beat me to a diff 😁.
This would also work. It is less magic, which is good. But I think I still prefer the other approach. |
I had to convince myself that it'd work :)
👍 me too |
My only concern is that there may be existing code which doesn't explicitly use the Like here in distributed. |
We could do something like if the first arg is a string, check if it's a key... but it does add some magic. |
Or we could take it slow and deprecate passing default as an arg. |
Or we can just pick a new name and have a new function :) |
This is probably the right approach. But I also want to move fast and break stuff. |
Is using |
Not necessarily. Some use the Dask config system as a convenient way to store metadata. We sometimes do this internally in Dask Lines 83 to 90 in ff3ea6c
and Distributed (as Jacob pointed out) |
Ah thanks @jrbourbeau I hadn't considered that case. I guess in that case maybe we should deprecate using |
I am happy with that, I'll update this PR to reflect that. But given that this is a substantial change it might be good to get some more eyes on this. |
Having a poke around in dask/dask it appears there are a large number of situations where the default is passed as an arg. I'm nervous that enforcing it being a kwarg will break a lot of things. |
I don't think this PR should enforce anything, just warn. But if that seems like it'll be too annoying, I am fine just going with the tuple approach or the new function approach. |
Ah that's fair. I was just adding a |
Long term I'm hesitant that we should aim for The new function approach seems nice. We could also add a new my_kwarg if my_kwarg is not None else dask.config.get("mycluster.my-kwarg") into dask.config.get("mycluster.my-kwarg", conditional=my_kwarg) |
Thanks @jrbourbeau!
No we could still accept
We did also discuss this approach. I think the consensus was that the ordering was a little annoying because kwargs can't go first and it feels like that argument should be first. However it seems like the least surprising way to implement this feature. Also coming up with a good and succinct name for this kwarg is a tricky one. |
Ah gotcha, thank you for clarifying |
@jacobtomlinson will you be modifying to use the new diff, or were you wanting to reach a consensus that made everyone happy (as oppsed to merely content) ? |
@martindurant I'm not sure which diff to do with here. We've discussed multiple approaches and haven't seemed to settle on any of them. |
I feel like if this was accomplished, we would all agree on the kwarg version.
(where I have not tried to come up with a better term!) |
I don't want to hold this up. @jacobtomlinson if you are happy with the kwarg approach that is totally fine with me. |
Welp @jacobtomlinson should we just go with |
Thanks for the nudge @jsignell. I decided to go for the kwarg approach as you and @jrbourbeau suggested. I named the kwarg |
I prefer |
Thanks @jsignell. I think from that list my preference would be @jrbourbeau care to break the tie? |
In case it helps I prefer |
That sounds reasonable. Updated! Thanks! |
Sorry for coming in here late, but can I ask someone to provide a motivating use case for this? |
In general I don't understand the pattern of "get this config value, but actually then throw it away and give me this other value instead". Am I understanding the objective here correctly? If so, my inclination is to leave this to user code unless it is very very common. |
@mrocklin : this was about avoiding having to write "thing = config.get("thing") if thing is None else thing" or something like that, in many many places; where |
Ah, reading through issue now. My apologies for the friction, that discussion is long ago enough to have escaped medium-term mental storage. Happy to retract my concern. |
+1 for |
@mrocklin's concerns are very valid, however it is my opinion that the usage that @martindurant mentioned is very common, especially in downstream cluster manager packages like Thanks for the input everyone, I think this is a pretty central change and lazy concensus made me a little nervous here.
Merging! |
Also thanks for all your time here @jsignell. This rabbit hole was pretty deep for such a small change. I appreciated having someone along for the ride. |
black dask
/flake8 dask
Add config method which either passes a value straight through, of if it is
None
gets a value from the config.Method name still very much up for discussion. Current contenders are:
default_get
get_if_none
maybe_get
(@jsignell)Closes #6860