Skip to content
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_in function: add example of giving a string to the keys argument #548

Open
KevinXOM opened this issue Aug 31, 2022 · 2 comments
Open

Comments

@KevinXOM
Copy link

It is currently tempting to test get_in like this:

get_in('x', {'x':5}) # returns 5

and conclude that this will also work:

get_in('test', {'test':5}) # actually returns None

It does not work, because 'test' is treated as ['t','e','s','t']. In complex dictionaries, you may actually get a value, like

get_in('xy', {'x': {'y': 5}} ) # returns 5

The documentation should probably call this out explicitly, if this is the intended behavior, perhaps by giving the 'xy' example above.

I, for one, wouldn't mind an implementation where get_in('test', {'test':5}) returns 5, but I wouldn't go so far as to say that is the right approach. I'm imagining it would facilitate doing something like this:

juxt(*map(curry(get_in), ['str1', ['str2', 'str3'], 'etc']))
@groutr
Copy link
Contributor

groutr commented Sep 1, 2022

You need to use get_in(['test'], {'test':5}). The first argument is treated as a sequence of keys. Every example in the docstring uses a list as the first argument.
get_in('xy', {'x': {'y': 5}} ) happens to work because strings are sequences in Python and is equivalent to get_in(['x', 'y'], {'x': {'y':5}}) (which is the suggested/encouraged form).

@KevinXOM
Copy link
Author

KevinXOM commented Sep 1, 2022

I am aware of the behavior, having explicitly explained it in my issue. I am saying that the documentation should not require the reader to infer the dev's intent by reading between the lines of their examples.

If the list form is suggested/encouraged, say so in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants