Skip to content

Type defined with WithAnnotations fails mypy 0.981 #1190

@AllexVeldman

Description

@AllexVeldman

Bug report

What's wrong

Types defined using WithAnnotations started failing with mypy 0.981.

from typing import TypedDict
from django.db.models import IntegerField, Model, QuerySet, Value
from django_stubs_ext import WithAnnotations

class Bar(Model):
    x = IntegerField()

class BarAnnotations(TypedDict):
    y: int

def foo(xyz: QuerySet[Bar]) -> QuerySet[WithAnnotations[Bar, BarAnnotations]]:
    return xyz.annotate(y=Value(2))

reveal_type(foo)

mypy 0.971:

tmp.py:19: note: Revealed type is "def (xyz: django.db.models.query._QuerySet[tmp.Bar, tmp.Bar]) -> django.db.models.query._QuerySet[django_stubs_ext.WithAnnotations[tmp__Bar, TypedDict('tmp.BarAnnotations', {'y': builtins.int})], django_stubs_ext.WithAnnotations[tmp__Bar, TypedDict('tmp.BarAnnotations', {'y': builtins.int})]]"
Success: no issues found in 1 source file

mypy 0.981/0.982:

tmp.py:16: error: Incompatible return value type (got "_QuerySet[WithAnnotations[tmp__Bar, TypedDict({'y': Any})], WithAnnotations[tmp__Bar, TypedDict({'y': Any})]]", expected "_QuerySet[WithAnnotations[tmp__Bar], WithAnnotations[tmp__Bar]]")  [return-value]
tmp.py:19: note: Revealed type is "def (xyz: django.db.models.query._QuerySet[tmp.Bar, tmp.Bar]) -> django.db.models.query._QuerySet[django_stubs_ext.WithAnnotations[tmp__Bar], django_stubs_ext.WithAnnotations[tmp__Bar]]"
Found 1 error in 1 file (checked 1 source file)

It seems mypy infers the return-type of foo as un-annotated, I tried to dig into how the WithAnnotations works but got lost in the plugin quite quickly.
As I'm not sure if this is a mypy or a django-stubs issue, some pointers on where to dig would be welcome.

System information

  • OS: macOS 12.6
  • python version: 3.10.2
  • django version: 4.1.2
  • mypy version: 0.982
  • django-stubs version: 1.12.0
  • django-stubs-ext version: 0.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmypy-pluginIssues specific to mypy_django_plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions