Convert the sequence `args` to the same type as `instance`.
(instance, args)
| 189 | |
| 190 | |
| 191 | def _sequence_like(instance, args): |
| 192 | """ |
| 193 | Convert the sequence `args` to the same type as `instance`. |
| 194 | """ |
| 195 | if isinstance(instance, dict): |
| 196 | result = dict(zip(_sorted(instance), args)) |
| 197 | return type(instance)((key, result[key]) for key in instance.keys()) |
| 198 | elif ( |
| 199 | isinstance(instance, tuple) |
| 200 | and hasattr(instance, "_fields") |
| 201 | and isinstance(instance._fields, Sequence) |
| 202 | and all(isinstance(f, str) for f in instance._fields) |
| 203 | ): |
| 204 | # This is a namedtuple |
| 205 | return type(instance)(*args) |
| 206 | else: |
| 207 | # Not a namedtuple |
| 208 | return type(instance)(args) |
| 209 | |
| 210 | |
| 211 | def _packed_nest_with_indices(structure, flat, index): |
no test coverage detected