Skip to content

Generic Queryset INTERNALERROR #2479

@pelme

Description

@pelme

Bug report

Starting from django-stubs 5.0.3, the following example crashes with an internal mypy error:

from django.db import models
import typing as t

TMyBase = t.TypeVar("TMyBase", bound="MyBase")

class MyBase(models.Model):
	class Meta:
		abstract = True


class MyQuerySet(models.QuerySet[TMyBase]):
	pass

Output:

❯ pip freeze
asgiref==3.8.1
Django==5.0.4
django-stubs==5.0.3
django-stubs-ext==5.1.1
mypy==1.11.2
mypy-extensions==1.0.0
sqlparse==0.5.3
types-PyYAML==6.0.12.20241230
typing_extensions==4.12.2

❯ mypy --no-incremental --show-traceback .
./tstapp/models.py:11: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.11.2
Traceback (most recent call last):
  File "mypy/semanal.py", line 7087, in accept
  File "mypy/nodes.py", line 819, in accept
  File "mypy/semanal.py", line 874, in visit_func_def
  File "mypy/semanal.py", line 919, in analyze_func_def
  File "mypy/semanal.py", line 6742, in defer
AssertionError: Must not defer during final iteration
./tstapp/models.py:11: : note: use --pdb to drop into pdb

How is that should be

Running the same example with django-stubs 5.0.2:

❯ pip freeze
asgiref==3.8.1
Django==5.0.4
django-stubs==5.0.2
django-stubs-ext==5.1.1
mypy==1.10.1
mypy-extensions==1.0.0
sqlparse==0.5.3
types-PyYAML==6.0.12.20241230
typing_extensions==4.12.2

❯ mypy --show-traceback .
Success: no issues found in 13 source files

Example project

You can reproduce this by downloading this project (contains the model definition above) and running it locally:
tst.zip

Install django-stubs 5.0.2 without the problem:

pip install 'django-stubs[compatible-mypy]==5.0.2'

Install django-stubs 5.0.3 with the problem

pip install 'django-stubs[compatible-mypy]==5.0.2'

Workaround

The crash can be worked around by putting TMyBase after the MyBase definition.

System information

  • OS:
  • python version: 3.12.7
  • django version: I tested with 5.0.4 and 5.1.4 with the same result

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