]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/utils/traversal.py
[utils] `traverse_obj`: Allow unbranching using `all` and `any` (#9571)
[yt-dlp.git] / yt_dlp / utils / traversal.py
index 8938f4c78298f29bda65adfe1f1e44dba56b0506..926a3d0a13994cf12f91417834c933ca797d4287 100644 (file)
@@ -228,6 +228,15 @@ def apply_path(start_obj, path, test_type):
             if not casesense and isinstance(key, str):
                 key = key.casefold()
 
+            if key in (any, all):
+                has_branched = False
+                filtered_objs = (obj for obj in objs if obj not in (None, {}))
+                if key is any:
+                    objs = (next(filtered_objs, None),)
+                else:
+                    objs = (list(filtered_objs),)
+                continue
+
             if __debug__ and callable(key):
                 # Verify function signature
                 inspect.signature(key).bind(None, None)