Skip to content

Custom manager causes phantom errors and sometimes crash #709

@christianbundy

Description

@christianbundy

Bug report

What's wrong

Still working on a minimal repro, but here's my stack trace:

Traceback (most recent call last):
  File "/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/.venv/lib/python3.9/site-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 87, in main
  File "mypy/main.py", line 165, in run_build
  File "mypy/build.py", line 179, in build
  File "mypy/build.py", line 254, in _build
  File "mypy/build.py", line 2697, in dispatch
  File "mypy/build.py", line 3021, in process_graph
  File "mypy/build.py", line 3137, in process_stale_scc
  File "mypy/errors.py", line 565, in file_messages
  File "mypy/errors.py", line 538, in format_messages
IndexError: list index out of range

I have a FooManager:

from django.db import models

class FooManager(models.Manager):

    def create_with_foo(self, *args, **kwargs) -> models.Model: # Line 7
        pass

    def save_foo(self, model: models.Model) -> None: # Line 25
        pass

It's often used on models like this:

objects = FooManager()

For some reason, three completely unrelated files are getting these errors:

some_file.py:7: error: Name "models" is not defined  [name-defined]
    )
    ^
some_file.py:25: error: Name "models" is not defined  [name-defined]
            does_not_matter_anything_could_be_here()

I tried adding whitespace to the top of some_file.py but the line numbers didn't change. Odd. I looked through my project for files where lines 7 and 25 referenced models and found my FooManager. I added 1 line to the top of FooManager and the mypy errors incremented by 1. I added 100 lines to the top of FooManager and got this sweet stack trace.

How is that should be

There should be no errors.

System information

  • OS:
  • python version: 3.9.6
  • django version: 3.1
  • mypy version: 0.910
  • django-stubs version: 1.8.0
  • django-stubs-ext version: 0.3.1

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