-
Notifications
You must be signed in to change notification settings - Fork 127
Open
Description
@thequilo Do you remember, why the default is only considered for KeyError and IndexError?
When should get_by_path fail?
paderbox/paderbox/utils/nested.py
Lines 476 to 493 in 6727955
| except Exception as e: | |
| # Indexing a custom type can raise any exception, in which case | |
| # we try to broadcast | |
| # Not sure if broadcasting makes sense for lists/tuples. It is | |
| # hard to check for custom sequences because of str, so | |
| # sequences are broadcasted here | |
| if allow_early_stopping and not isinstance(container, Mapping): | |
| return container | |
| # We can't add another more specific except block because we have to | |
| # catch all exceptions for the broadcasting. Assuming here that | |
| # custom containers raise KeyErrors and IndexErrors correctly when | |
| # the indexed element is not found | |
| if isinstance(e, (KeyError, IndexError)): | |
| if default is not _NO_VALUE: | |
| return default | |
| raise |
Here, an extension of the doctest. IMHO the last example should work.
>>> d = {'a': 'b', 'c': {'d': {'e': 'f'}, 'g': [1, [2, 3], 4]}}
...
>>> get_by_path(d, 'c.b.c', default=42)
42
>>> get_by_path(d, 'a.b.c', default=42)
Traceback (most recent call last):
...
TypeError: string indices must be integersThe leafs in a nested structure can have any type and hence any Exception could be thrown.
So I don't get, why there is an if statement.
Metadata
Metadata
Assignees
Labels
No labels