Skip to content

Type prefetch_related lookups and infer Prefetch models - #426

Merged
kodiakhq[bot] merged 2 commits into
sbdchd:mainfrom
kalekseev:feat/typed-prefetch-related
Oct 14, 2026
Merged

kodiakhq[bot] merged 2 commits into
sbdchd:mainfrom
kalekseev:feat/typed-prefetch-related

Conversation

@kalekseev

@kalekseev kalekseev commented Oct 10, 2026 •

Copy link
Copy Markdown
  • QuerySet.prefetch_related and Manager.prefetch_related accept str | Prefetch lookups, plus the positional None overload that clears prefetches. They keep the queryset return type.
  • Prefetch infers its model from its queryset argument and defaults to Any without one. Prefetch.queryset is nullable, matching runtime.

@kalekseev
kalekseev force-pushed the feat/typed-prefetch-related branch from 93f7042 to 7444998 Compare October 11, 2026 11:43
@kalekseev
kalekseev marked this pull request as ready for review October 11, 2026 11:45
@kalekseev

Copy link
Copy Markdown
Author

@sbdchd ready for review

@sbdchd sbdchd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks!

@kodiakhq
kodiakhq Bot merged commit 20905e2 into sbdchd:main Oct 14, 2026
2 checks passed
UnknownPlatypus added a commit to UnknownPlatypus/django-stubs that referenced this pull request Nov 11, 2026
# I have made things!

While triaging the merged [django-types](https://github.com/sbdchd/django-types) PRs, many turned out to be already fixed here, but nothing ran the behaviour through pyright, pyrefly and ty, and some of it had no test at all. This adds those tests, with no stub changes:

- `prefetch_related()` overloads and `Prefetch.queryset`
- managers not supporting `len()`, indexing or `delete()` like querysets
- `execute_wrapper()` and `constraint_checks_disabled()` as context managers
- the async queryset methods (`aget`, `afirst`, `acreate`, `aiterator`, `async for`)
- `upload_to` callables taking `(instance, filename)`
- `User.objects` as `UserManager[User]`
- `admin.register()` keeping the admin subclass, and the admin action/display decorators keeping the signature
- `AdminSite.get_model_admin()` and `LogEntry.objects.log_actions()` overloads
- `formset_factory()`, `@receiver`, `@deconstructible`, `keep_lazy_text`, the cache view decorators and `request.headers`

## Related issues
Tests proposed while triaging sbdchd#426, sbdchd#425, sbdchd#420, sbdchd#405, sbdchd#350, sbdchd#330, sbdchd#310, sbdchd#308, sbdchd#288, sbdchd#262, sbdchd#238, sbdchd#120, sbdchd#119, sbdchd#105, sbdchd#42, sbdchd#37, sbdchd#8, sbdchd#2.

## AI Policy
- [ ] I have read and agree to the [AI Policy](https://github.com/typeddjango/django-stubs/blob/master/.github/AI_POLICY.md), removed any "Co-Authored-By" lines attributing coding agents, and manually reviewed the final result
UnknownPlatypus added a commit to typeddjango/django-stubs that referenced this pull request Nov 11, 2026
# I have made things!

While triaging the merged [django-types](https://github.com/sbdchd/django-types) PRs, many turned out to be already fixed here, but nothing ran the behaviour through pyright, pyrefly and ty, and some of it had no test at all. This adds those tests, with no stub changes:

- `prefetch_related()` overloads and `Prefetch.queryset`
- managers not supporting `len()`, indexing or `delete()` like querysets
- `execute_wrapper()` and `constraint_checks_disabled()` as context managers
- the async queryset methods (`aget`, `afirst`, `acreate`, `aiterator`, `async for`)
- `upload_to` callables taking `(instance, filename)`
- `User.objects` as `UserManager[User]`
- `admin.register()` keeping the admin subclass, and the admin action/display decorators keeping the signature
- `AdminSite.get_model_admin()` and `LogEntry.objects.log_actions()` overloads
- `formset_factory()`, `@receiver`, `@deconstructible`, `keep_lazy_text`, the cache view decorators and `request.headers`

## Related issues
Tests proposed while triaging sbdchd#426, sbdchd#425, sbdchd#420, sbdchd#405, sbdchd#350, sbdchd#330, sbdchd#310, sbdchd#308, sbdchd#288, sbdchd#262, sbdchd#238, sbdchd#120, sbdchd#119, sbdchd#105, sbdchd#42, sbdchd#37, sbdchd#8, sbdchd#2.

## AI Policy
- [ ] I have read and agree to the [AI Policy](https://github.com/typeddjango/django-stubs/blob/master/.github/AI_POLICY.md), removed any "Co-Authored-By" lines attributing coding agents, and manually reviewed the final result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants