Repository: yt-dlp/yt-dlp
File: yt_dlp/utils.py (Python)

Code before:
                            If `True`, any non-compatible object will first be
                            converted into a string and then traversed into.


    @returns                The result of the object traversal.
                            If successful, `get_all=True`, and the path branches at least once,
                            then a list of results is returned instead.
                            A list is always returned if the last path branches and no `default` is given.
    """
    is_sequence = lambda x: isinstance(x, collections.abc.Sequence) and not isinstance(x, (str, bytes))
    casefold = lambda k: k.casefold() if isinstance(k, str) else k

    if isinstance(expected_type, type):
        type_test = lambda val: val if isinstance(val, expected_type) else None
    else:
        type_test = lambda val: try_call(expected_type or IDENTITY, args=(val,))

    def apply_key(key, obj):
        if obj is None:
            return

        elif key is None:
            yield obj

        elif isinstance(key, set):
            assert len(key) == 1
            yield try_call(next(iter(key)), args=(obj,))

        elif isinstance(key, (list, tuple)):
            for branch in key:
                _, result = apply_path(obj, branch)
                yield from result

        elif key is ...:
            if isinstance(obj, collections.abc.Mapping):
                yield from obj.values()
            elif is_sequence(obj):
                yield from obj
            elif isinstance(obj, re.Match):
                yield from obj.groups()
            elif traverse_string:
                yield from str(obj)

        elif callable(key):
            if is_sequence(obj):
                iter_obj = enumerate(obj)
            elif isinstance(obj, collections.abc.Mapping):
                iter_obj = obj.items()
            elif isinstance(obj, re.Match):
                iter_obj = itertools.chain(
                    enumerate((obj.group(), *obj.groups())),

Review comment (suggestion):
```suggestion
            assert len(key) == 1, 'Set should only be used to wrap transformations'
```

Code after:
    @param is_user_input    Whether the keys are generated from user input.
                            If `True` strings get converted to `int`/`slice` if needed.
    @param traverse_string  Whether to traverse into objects as strings.
                            If `True`, any non-compatible object will first be
                            converted into a string and then traversed into.


    @returns                The result of the object traversal.
                            If successful, `get_all=True`, and the path branches at least once,
                            then a list of results is returned instead.
                            A list is always returned if the last path branches and no `default` is given.
    """
    is_sequence = lambda x: isinstance(x, collections.abc.Sequence) and not isinstance(x, (str, bytes))
    casefold = lambda k: k.casefold() if isinstance(k, str) else k

    if isinstance(expected_type, type):
        type_test = lambda val: val if isinstance(val, expected_type) else None
    else:
        type_test = lambda val: try_call(expected_type or IDENTITY, args=(val,))

    def apply_key(key, test_type, obj):
        if obj is None:
            return

        elif key is None:
            yield obj

        elif isinstance(key, set):
            assert len(key) == 1, 'Set should only be used to wrap a single item'
            item = next(iter(key))
            if isinstance(item, type):
                if isinstance(obj, item):
                    yield obj
            else:
                yield try_call(item, args=(obj,))

        elif isinstance(key, (list, tuple)):
            for branch in key:
                _, result = apply_path(obj, branch, test_type)
                yield from result

        elif key is ...:
            if isinstance(obj, collections.abc.Mapping):
                yield from obj.values()
            elif is_sequence(obj):
                yield from obj
            elif isinstance(obj, re.Match):
                yield from obj.groups()
            elif traverse_string:
                yield from str(obj)