(
obj: object,
path: Sequence[str],
*,
index: int,
flattened_key: str | None,
)
| 55 | |
| 56 | |
| 57 | def _extract_items( |
| 58 | obj: object, |
| 59 | path: Sequence[str], |
| 60 | *, |
| 61 | index: int, |
| 62 | flattened_key: str | None, |
| 63 | ) -> list[tuple[str, FileTypes]]: |
| 64 | try: |
| 65 | key = path[index] |
| 66 | except IndexError: |
| 67 | if isinstance(obj, NotGiven): |
| 68 | # no value was provided - we can safely ignore |
| 69 | return [] |
| 70 | |
| 71 | # cyclical import |
| 72 | from .._files import assert_is_file_content |
| 73 | |
| 74 | # We have exhausted the path, return the entry we found. |
| 75 | assert flattened_key is not None |
| 76 | |
| 77 | if is_list(obj): |
| 78 | files: list[tuple[str, FileTypes]] = [] |
| 79 | for entry in obj: |
| 80 | assert_is_file_content(entry, key=flattened_key + "[]" if flattened_key else "") |
| 81 | files.append((flattened_key + "[]", cast(FileTypes, entry))) |
| 82 | return files |
| 83 | |
| 84 | assert_is_file_content(obj, key=flattened_key) |
| 85 | return [(flattened_key, cast(FileTypes, obj))] |
| 86 | |
| 87 | index += 1 |
| 88 | if is_dict(obj): |
| 89 | try: |
| 90 | # We are at the last entry in the path so we must remove the field |
| 91 | if (len(path)) == index: |
| 92 | item = obj.pop(key) |
| 93 | else: |
| 94 | item = obj[key] |
| 95 | except KeyError: |
| 96 | # Key was not present in the dictionary, this is not indicative of an error |
| 97 | # as the given path may not point to a required field. We also do not want |
| 98 | # to enforce required fields as the API may differ from the spec in some cases. |
| 99 | return [] |
| 100 | if flattened_key is None: |
| 101 | flattened_key = key |
| 102 | else: |
| 103 | flattened_key += f"[{key}]" |
| 104 | return _extract_items( |
| 105 | item, |
| 106 | path, |
| 107 | index=index, |
| 108 | flattened_key=flattened_key, |
| 109 | ) |
| 110 | elif is_list(obj): |
| 111 | if key != "<array>": |
| 112 | return [] |
| 113 | |
| 114 | return flatten( |
no test coverage detected