Skip to content

Fix mypy crash on QuerySet subclass with forward-referenced TypeVar#3241

Open
UnknownPlatypus wants to merge 1 commit intotypeddjango:masterfrom
UnknownPlatypus:fix/queryset-forward-ref-typevar-crash
Open

Fix mypy crash on QuerySet subclass with forward-referenced TypeVar#3241
UnknownPlatypus wants to merge 1 commit intotypeddjango:masterfrom
UnknownPlatypus:fix/queryset-forward-ref-typevar-crash

Conversation

@UnknownPlatypus
Copy link
Copy Markdown
Contributor

Prior to the plugin code change, a fresh mypy --strict tests/assert_type was crashing like so:

tests/assert_type/db/models/test_managers.py:15: 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.19.1
Traceback (most recent call last):
  File "mypy/semanal.py", line 7568, in accept
  File "mypy/nodes.py", line 995, in accept
  File "mypy/semanal.py", line 978, in visit_func_def
  File "mypy/semanal.py", line 1023, in analyze_func_def
  File "mypy/semanal.py", line 7214, in defer
AssertionError: Must not defer during final iteration
tests/assert_type/db/models/test_managers.py:15: : note: use --pdb to drop into pdb

Fixes #2911

…ound

When a QuerySet subclass uses a forward-referenced TypeVar bound, the plugin generated an as_manager classmethod with unresolved placeholder types, causing mypy to crash with AssertionError: Must not defer during final iteration.

The fix is to check for unresolved TypeVars before generating the method, and defer if any are found.

Fixes typeddjango#2911
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.

Mypy v1.18.2, django-stubs 5.2.7, Django 5.1 on python 3.13.9

1 participant