python / python/mypy

Crash on forward references in pydantic and django plugins

Open
#18,579 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

crash priority-0-high
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Crash Report

Mypy was working fine on my django project until about a week ago, when I started getting this error. I'm not sure what caused it. My python code used to be in the root directory of my project and now I moved it to the backend/ directory, and the problem started around then, but I'm not sure whether that is related.

I am using pre-commit and have mypy set up in my .pre-commit-config.yaml. The error actually only happens when I run mypy as part of pre-commit. If I just run mypy . from the command line, it works fine.

Presently, whenever I run pre-commit, I get an INTERNAL ERROR (details below).

Traceback

/home/travis/Repos/bethpage/backend/bethpage/models.py:23: 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.16.0+dev.23e2d0f8cbaa2f4874d8f6a6bee3756922c78407
Traceback (most recent call last):
  File "/home/travis/Repos/bethpage/.tox/py312/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/main.py", line 119, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/main.py", line 203, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/build.py", line 191, in build
    result = _build(
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/build.py", line 267, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/build.py", line 2937, in dispatch
    process_graph(graph, manager)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/build.py", line 3335, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/build.py", line 3430, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal_main.py", line 94, in semantic_analysis_for_scc
    process_functions(graph, scc, patches)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal_main.py", line 252, in process_functions
    process_top_level_function(
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal_main.py", line 291, in process_top_level_function
    deferred, incomplete, progress = semantic_analyze_target(
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal_main.py", line 351, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal.py", line 655, in refresh_partial
    self.accept(node)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal.py", line 7254, in accept
    node.accept(self)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/nodes.py", line 811, in accept
    return visitor.visit_func_def(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal.py", line 913, in visit_func_def
    self.analyze_func_def(defn)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal.py", line 958, in analyze_func_def
    self.defer(defn)
  File "/home/travis/Repos/bethpage/.tox/py312/lib/python3.12/site-packages/mypy/semanal.py", line 6900, in defer
    assert not self.final_iteration, "Must not defer during final iteration"
           ^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Must not defer during final iteration
/home/travis/Repos/bethpage/backend/bethpage/models.py:23: : note: use --pdb to drop into pdb

To Reproduce

This crash happens any time I change any file in my backend/ directory and then run git add -A; pre-commit.

It does not happen if I just run cd backend; mypy .

If I use the --no-incremental flag or delete the cache directory, the problem still persists.

If I open up backend/bethpage/models.py and add a line that just says "pass" somewhere above line 23 (the line cited in the error), then run git add -A; pre-commit, it actually works fine and completes without errors. The next time I need to commit something, I'll get the error again, but I can just remove that "pass" line and once again, the error goes away.

Here is the beginning of that /backend/bethpage/models.py file:

import uuid
from typing import (
    Any,
    ClassVar,
    Dict,
    Generic,
    Iterable,
    List,
    Mapping,
    Optional,
    Self,
    Tuple,
    TypeVar,
)

from django.db import models, transaction
from django.db.models.fields.reverse_related import ForeignObjectRel
from django.utils import timezone

T = TypeVar("T", bound="BaseModel")


class BaseQuerySet(models.QuerySet[T], Generic[T]):

    def bulk_create(self, objs: Iterable[T], *args: Any, **kwargs: Any) -> List[T]:
        for obj in objs:
            obj.full_clean()
        return super().bulk_create(objs, *args, **kwargs)
...etc...

Your Environment

  • Mypy version used: I have tried versions of mypy from 1.0.0 to 1.14.1 and have also tried using the master branch on github, always with the same error.
  • Python version used: 3.12
  • Operating system: Arch Linux

.pre-commit-config.yaml

repos:
  - repo: local
    hooks:
      - id: mypy
        name: mypy
        entry: mypy
        language: system
        types: [python]
        args:
          - --config-file=backend/mypy.ini
          - --show-traceback
...etc...

mypy.ini

[mypy]
python_version = 3.12
mypy_path = backend
plugins =
  mypy_django_plugin.main,
  mypy_drf_plugin.main
strict = True
show_error_codes = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_any_unimported = True
check_untyped_defs = True
no_implicit_optional = True
warn_unused_ignores = True
warn_return_any = True
warn_unreachable = True

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash by running the local mypy pre-commit hook with backend/mypy.ini, then compare it with cd backend; mypy . and the --no-incremental case. Start at mypy/semanal.py around the final-iteration assertion and use backend/bethpage/models.py as the trigger; done means the pre-commit run completes without the INTERNAL ERROR.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.