Skip to content

Conversation

@UnknownPlatypus
Copy link
Contributor

I have made things!

Followup to #2779 to handle more Prefetch usages using a generic class over the prefetched queryset and to_attr value (with plugin specialization to LiteralString).

We now:

  • handle calls defined outside of prefetch_related calls
  • handle Prefetch classes created from external function or variable assignments
  • correctly infer for case with the to_attr attribute filled but not the queryset attribute.
reveal_type(
    Article.objects.prefetch_related(
-     models.Prefetch("tags", to_attr="tags")).get().tags)  # N: Revealed type is "builtins.list[Any]"
+     models.Prefetch("tags", to_attr="tags")).get().tags)  # N: Revealed type is "builtins.list[myapp.models.Tag]"
    )
)

I've also expanded the tests with a test for annotated prefetched queryset

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again!

@sobolevn sobolevn merged commit f3427b1 into typeddjango:master Aug 27, 2025
37 checks passed
@UnknownPlatypus UnknownPlatypus deleted the extend-prefetch-hook branch August 27, 2025 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants