GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PEP 695 alias with `WithAnnotations` leaks its row type into unrelated `QuerySet` annotations · Issue #3424 · typeddjango/django-stubs

PEP 695 alias with WithAnnotations leaks its row type into unrelated QuerySet annotations #3424

New issue
Copy link
New issue
Copy link
Closed
Closed
PEP 695 alias with WithAnnotations leaks its row type into unrelated QuerySet annotations#3424
Copy link
Labels
bugSomething isn't working

Description

@milssky
milssky
opened on Jun 10, 2026
Issue body actions

Bug report

May be it related to #3423

A PEP 695 type alias containing QuerySet[WithAnnotations[...]] changes how unrelated QuerySet[Model] annotations are interpreted.

What's wrong

The annotated project type is incorrectly used as the row type of other querysets.

Reproduction

File structure:

repro/
├── __init__.py
├── settings.py
├── models.py
└── repository/
    ├── __init__.py
    ├── project.py
    └── user.py
mypy.ini

repro/settings.py:

SECRET_KEY = "repro"
INSTALLED_APPS = ["repro"]

repro/models.py:

from django.db import models


class Project(models.Model):
    name = models.CharField(max_length=100)


class User(models.Model):
    name = models.CharField(max_length=100)

repro/repository/project.py:

from typing import TypedDict

from django.db import models
from django_stubs_ext import WithAnnotations

from repro.models import Project


class ProjectAnnotations(TypedDict):
    occupied: int


type ProjectQuerySet = models.QuerySet[
    WithAnnotations[Project, ProjectAnnotations]
]


class ProjectRepository:
    def get_projects(self) -> ProjectQuerySet:
        return Project.objects.annotate(occupied=models.Count("id"))

repro/repository/user.py:

from django.db import models

from repro.models import User


class UserRepository:
    def get_users(self) -> models.QuerySet[User]:
        return User.objects.all()

repro/repository/__init__.py:

from repro.repository.project import ProjectRepository as ProjectRepository
from repro.repository.user import UserRepository as UserRepository

repro/__init__.py is empty.

mypy.ini:

[mypy]
plugins = mypy_django_plugin.main
strict = true

[mypy.plugins.django-stubs]
django_settings_module = repro.settings

Run:

mypy --no-incremental --config-file mypy.ini repro/repository/user.py

Actual behavior

repro/repository/user.py:8: error: Incompatible return value type
(got "QuerySet[User, User]",
expected "QuerySet[User, Project@AnnotatedWith[ProjectAnnotations]]")
[return-value]

The function is annotated as QuerySet[User], but mypy unexpectedly interprets its row type as Project@AnnotatedWith[ProjectAnnotations].

How is that should be

models.QuerySet[User] should be interpreted as:

QuerySet[User, User]

The ProjectQuerySet alias must not affect unrelated queryset annotations.

System information

  • OS: macos tahoe 26.5
  • python version: 3.13.11
  • django version: 6.0.6
  • mypy version: 2.1.0
  • django-stubs version:
  • django-stubs-ext version:

Activity

  1. milssky
    added
    bugSomething isn't working
    on Jun 10, 2026
  2. UnknownPlatypus commented on Jun 10, 2026

    @UnknownPlatypus
    UnknownPlatypus
    on Jun 10, 2026
    Contributor
    More actions

    Are you using latest django-stubs ? I think it was fixed recently

  3. milssky commented on Jun 10, 2026

    @milssky
    milssky
    on Jun 10, 2026
    Author
    More actions

    oh sorry, forgot stubs version
    i use
    django-stubs 6.0.5
    django-stubs-ext 6.0.5

  4. typeddjango
    deleted a comment from on Jul 27, 2026
  5. UnknownPlatypus commented on Nov 8, 2026

    @UnknownPlatypus
    UnknownPlatypus
    on Nov 8, 2026
    Contributor
    More actions

    PEP 695 alias with WithAnnotations leaked its row type into unrelated QuerySet[Model] annotations.

    This was a mypy bug fixed in python/mypy#21526, you just need to upgrade to mypy 2.2 and it should be fixed

  6. UnknownPlatypus
    closed this as completedon Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.