Skip to content

prefetch_related_objects cannot work with Iterables - it is expecting a SequenceΒ #2948

@AlexandrOnuf

Description

@AlexandrOnuf

Bug report

Typing for prefetch_related_objects says that it can accept Iterable, but code indicates that it should be a Sequence (I suppose)

Rel. to this typing stubs

def prefetch_related_objects(model_instances: Iterable[_Model], *related_lookups: str | Prefetch) -> None: ...

Django 5.2.8

What's wrong

Typing for prefetch_related_objects says that it can accept Iterable, but if we will call it with set, frozenset, keys or values view, we will see this error

   2370     break
   2372 # Descend down tree
   2373 
   2374 # We assume that objects retrieved are homogeneous (which is the premise
   2375 # of prefetch_related), so what applies to first object applies to all.
-> 2376 first_obj = obj_list[0]
   2377 to_attr = lookup.get_current_to_attr(level)[0]
   2378 prefetcher, descriptor, attr_found, is_fetched = get_prefetcher(
   2379     first_obj, through_attr, to_attr
   2380 )

TypeError: 'set' object is not subscriptable

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions