question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mypy crash on querysets

See original GitHub issue

Bug report

Mypy crashes

What’s wrong

class SomeEvent(models.Model):

user_id = models.IntegerField(null=False, unique=True)
exception_name = models.CharField(max_length=200, null=True, blank=True)
exception_type = models.CharField(max_length=200, null=True, blank=True)

And then in code:

    data = (
        models.SomeEvent.objects.values('exception_name').order_by().annotate(Count('exception_name'))
    )

It crashes with:

some_handler.py:283: 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: 0.971
Traceback (most recent call last):
  File "/Users/myuser/.virtualenvs/python38env/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/build.py", line 154, in build
    result = _build(
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/build.py", line 230, in _build
    graph = dispatch(sources, manager, stdout)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/build.py", line 2729, in dispatch
    process_graph(graph, manager)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/build.py", line 3087, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/build.py", line 3185, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/build.py", line 2180, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 329, in check_first_pass
    self.accept(d)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 435, in accept
    stmt.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1039, in accept
    return visitor.visit_class_def(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 1823, in visit_class_def
    self.accept(defn.defs)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 435, in accept
    stmt.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1110, in accept
    return visitor.visit_block(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 2200, in visit_block
    self.accept(s)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 435, in accept
    stmt.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 758, in accept
    return visitor.visit_func_def(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 786, in visit_func_def
    self._visit_func_def(defn)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 790, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 852, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 1037, in check_func_def
    self.accept(item.body)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 435, in accept
    stmt.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1110, in accept
    return visitor.visit_block(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 2200, in visit_block
    self.accept(s)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 435, in accept
    stmt.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1176, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 2242, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checker.py", line 2436, in check_assignment
    rvalue_type = self.expr_checker.accept(rvalue)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 3999, in accept
    typ = node.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1771, in accept
    return visitor.visit_call_expr(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 306, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 355, in visit_call_expr_inner
    callee_type = get_proper_type(self.accept(e.callee, type_context, always_allow_any=True))
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 3999, in accept
    typ = node.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1691, in accept
    return visitor.visit_member_expr(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 2078, in visit_member_expr
    result = self.analyze_ordinary_member_access(e, is_lvalue)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 2089, in analyze_ordinary_member_access
    original_type = self.accept(e.expr)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 3999, in accept
    typ = node.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1771, in accept
    return visitor.visit_call_expr(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 306, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 355, in visit_call_expr_inner
    callee_type = get_proper_type(self.accept(e.callee, type_context, always_allow_any=True))
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 3999, in accept
    typ = node.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1691, in accept
    return visitor.visit_member_expr(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 2078, in visit_member_expr
    result = self.analyze_ordinary_member_access(e, is_lvalue)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 2089, in analyze_ordinary_member_access
    original_type = self.accept(e.expr)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 3999, in accept
    typ = node.accept(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/nodes.py", line 1771, in accept
    return visitor.visit_call_expr(self)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 306, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 384, in visit_call_expr_inner
    ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname,
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 893, in check_call_expr_with_callee_type
    ret_type, callee_type = self.check_call(
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 958, in check_call
    return self.check_callable_call(callee, args, arg_kinds, context, arg_names,
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 1086, in check_callable_call
    new_ret_type = self.apply_function_plugin(
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy/checkexpr.py", line 773, in apply_function_plugin
    return method_callback(
  File "/Users/myuser/.virtualenvs/python38env/lib/python3.8/site-packages/mypy_django_plugin/transformers/querysets.py", line 272, in extract_proper_type_queryset_values
    assert isinstance(default_return_type, Instance)

How is that should be

It should not crash

System information

  • OS: MacOSX 12.5.1
  • python version: 3.8.13
  • django version: 2.2.x
  • mypy version: 0.971
  • django-stubs version: 1.12.0
  • django-stubs-ext version: 0.5.0

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sazarycommented, Nov 17, 2022

same thing happened for me.

mypy output:

$ mypy --show-column-numbers --config-file ~/code/python/miare/mypy.ini --follow-imports=skip ~/code/python/miare/capacity_prediction/
[...]
/home/sazary/code/python/miare/capacity_prediction/services/utils/occasion_applier.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: 0.991
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 4665, in accept
  File "mypy/nodes.py", line 1841, in accept
  File "mypy/checkexpr.py", line 410, in visit_call_expr
  File "mypy/checkexpr.py", line 530, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 1181, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 1264, in check_call
  File "mypy/checkexpr.py", line 1466, in check_callable_call
  File "mypy/checkexpr.py", line 1021, in apply_function_plugin
  File "/home/sazary/.virtualenvs/miare39/lib/python3.9/site-packages/mypy_django_plugin/transformers/querysets.py", line 149, in extract_proper_type_queryset_values_list
    assert isinstance(default_return_type, Instance)
AssertionError:
/home/sazary/code/python/miare/capacity_prediction/services/utils/occasion_applier.py:23: : note: use --pdb to drop into pdb

the part of my code that mypy crashes on (occasion_applier.py lines 22 & 23):

        for occasion in occasions:
            occasion_area_ids = list(occasion.areas.values_list('id', flat=True))

I don’t use any custom manager for Occasion model, nor for Area. The related part is:

class Occasion(models.Model):
    [...]
    areas = models.ManyToManyField(to=DriverArea, related_name='+', verbose_name='مناطق')
    [...]

My mypy.ini file:

[mypy]
python_version = 3.9
ignore_missing_imports = True
show_traceback = True
incremental = True

plugins =
    mypy_django_plugin.main,
    mypy_drf_plugin.main
    ; numpy.typing.mypy_plugin

[mypy.plugins.django-stubs]
django_settings_module = biar.settings.development

versions:

django-stubs==1.13.0
django-stubs-ext==0.7.0
djangorestframework-stubs==1.7.0
mypy==0.991
mypy-extensions==0.4.3
Django==3.2.2

Python 3.9.15
OS: Ubuntu 22.04.1

monkey patching is enabled too.

0reactions
sidmitracommented, Dec 8, 2022

Same error as #701 . I tried upgrading to master and i can still replicate it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mypy crashes with KeyError: 'django_stubs_ext' when using ...
Bug report mypy crashes when reaching code with Django queries that annotate fields, which did not happen which django-stubs version 1.8.0.
Read more >
mypy-django/Lobby - Gitter
I'm trying to do a custom manager or a custom queryset as manager, and both get typed to Any, am I missing something?...
Read more >
django-types · PyPI
... mypy can't crash due to Django config, and that non- mypy type checkers ... BaseManager from django.db.models.query import QuerySet # NOTE: there...
Read more >
django-stubs-pr730 - Python Package Health Analysis | Snyk
FAQ · Is this an official Django project? · Is it safe to use this in production? · mypy crashes when I run...
Read more >
django-stubs - Bountysource
mypy crashes when the return type of a manager method is a forward reference (similar to ... @property def ancestors(self) -> QuerySet: """Return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found