[utils] Use key None in traverse_obj to return as-is

This commit is contained in:
pukkandan 2022-01-02 03:07:24 +05:30
parent 4c922dd3fc
commit 1797b073ed
No known key found for this signature in database
GPG key ID: 0F00D95A001F4698

View file

@ -4939,11 +4939,12 @@ def traverse_obj(
''' Traverse nested list/dict/tuple ''' Traverse nested list/dict/tuple
@param path_list A list of paths which are checked one by one. @param path_list A list of paths which are checked one by one.
Each path is a list of keys where each key is a string, Each path is a list of keys where each key is a string,
a function, a tuple of strings or "...". a function, a tuple of strings/None or "...".
When a fuction is given, it takes the key as argument and When a fuction is given, it takes the key as argument and
returns whether the key matches or not. When a tuple is given, returns whether the key matches or not. When a tuple is given,
all the keys given in the tuple are traversed, and all the keys given in the tuple are traversed, and
"..." traverses all the keys in the object "..." traverses all the keys in the object
"None" returns the object without traversal
@param default Default value to return @param default Default value to return
@param expected_type Only accept final value of this type (Can also be any callable) @param expected_type Only accept final value of this type (Can also be any callable)
@param get_all Return all the values obtained from a path or only the first one @param get_all Return all the values obtained from a path or only the first one
@ -4962,8 +4963,8 @@ def _traverse_obj(obj, path, _current_depth=0):
nonlocal depth nonlocal depth
path = tuple(variadic(path)) path = tuple(variadic(path))
for i, key in enumerate(path): for i, key in enumerate(path):
if obj is None: if None in (key, obj):
return None return obj
if isinstance(key, (list, tuple)): if isinstance(key, (list, tuple)):
obj = [_traverse_obj(obj, sub_key, _current_depth) for sub_key in key] obj = [_traverse_obj(obj, sub_key, _current_depth) for sub_key in key]
key = ... key = ...