diff --git a/test/test_traversal.py b/test/test_traversal.py index 45985afa94..b0b1ab6444 100644 --- a/test/test_traversal.py +++ b/test/test_traversal.py @@ -546,6 +546,18 @@ class TestTraversalHelpers: {'url': 'https://example.com/subs/de4'}, ], }, 'non str types should be replaced by default id' + assert traverse_obj([ + {'name': '', 'ext': '', 'url': 'https://example.com/subs/en'}, + ], [..., { + 'id': 'name', + 'ext': 'ext', + 'url': 'url', + }, all, {subs_list_to_dict(lang='en', ext='vtt')}]) == { + 'en': [{ + 'ext': 'vtt', + 'url': 'https://example.com/subs/en', + }], + }, 'empty id and ext should be replaced by defaults' def test_trim_str(self): with pytest.raises(TypeError): diff --git a/yt_dlp/utils/traversal.py b/yt_dlp/utils/traversal.py index 76b51f53d1..e0d6f7e512 100644 --- a/yt_dlp/utils/traversal.py +++ b/yt_dlp/utils/traversal.py @@ -360,12 +360,12 @@ def subs_list_to_dict(subs: list[dict] | None = None, /, *, lang='und', ext=None if not url_or_none(sub.get('url')) and not sub.get('data'): continue sub_id = sub.pop('id', None) - if not isinstance(sub_id, str): + if not isinstance(sub_id, str) or not sub_id: if not lang: continue sub_id = lang sub_ext = sub.get('ext') - if not isinstance(sub_ext, str): + if not isinstance(sub_ext, str) or not sub_ext: if not ext: sub.pop('ext', None) else: