[utils] Support filter traversal key

Thx yt-dlp/yt-dlp#10653
This commit is contained in:
dirkf
2025-10-31 12:20:26 +00:00
parent cca41c9d2c
commit 96419fa706
3 changed files with 22 additions and 0 deletions

View File

@@ -473,6 +473,14 @@ class TestTraversal(_TestCase):
self.assertIs(traverse_obj(morsel, [(None,), any]), morsel,
msg='Morsel should not be implicitly changed to dict on usage')
def test_traversal_filter(self):
data = [None, False, True, 0, 1, 0.0, 1.1, '', 'str', {}, {0: 0}, [], [1]]
self.assertEqual(
traverse_obj(data, (Ellipsis, filter)),
[True, 1, 1.1, 'str', {0: 0}, [1]],
'`filter` should filter falsy values')
def test_get_first(self):
self.assertEqual(get_first([{'a': None}, {'a': 'spam'}], 'a'), 'spam')