Skip to content

.exclude(pk=...) causes crash #625

@jacobjove

Description

@jacobjove

Crash Report

Mypy crashes when it encounters a Django ORM query that uses .exclude(pk=...).

For example:

    @property
    def ancestors(self) -> QuerySet:
        """Return the model instances's ancestors, based on its LTree field."""
        return self.__class__.objects.exclude(pk=self.pk).filter(
            path__descendant=self.path  # custom lookup
        )

    @property
    def descendants(self) -> QuerySet:
        """Return the model instances's descendants, based on its LTree field."""
        return self.__class__.objects.exclude(pk=self.pk).filter(
            path__ancestor=self.path  # custom lookup
        )

Traceback

version: 0.812
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 3898, in accept
  File "mypy/nodes.py", line 1555, in accept
  File "mypy/checkexpr.py", line 270, in visit_call_expr
  File "mypy/checkexpr.py", line 347, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 852, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 911, in check_call
  File "mypy/checkexpr.py", line 1023, in check_callable_call
  File "mypy/checkexpr.py", line 732, in apply_function_plugin
  File "/Users/jacob/modularhistory/.venv/lib/python3.7/site-packages/mypy_django_plugin/transformers/orm_lookups.py", line 32, in typecheck_queryset_filter
    lookup_type = django_context.resolve_lookup_expected_type(ctx, model_cls, lookup_kwarg)
  File "/Users/jacob/modularhistory/.venv/lib/python3.7/site-packages/mypy_django_plugin/django/context.py", line 335, in resolve_lookup_expected_type
    lookup_parts, field_parts, is_expression = query.solve_lookup_type(lookup)
  File "/Users/jacob/modularhistory/.venv/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1084, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/Users/jacob/modularhistory/.venv/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1442, in names_to_path
    name = opts.pk.name
AttributeError: 'NoneType' object has no attribute 'name'

Your Environment

  • Mypy version used: 0.812
  • Mypy configuration options from mypy.ini (and other config files):
python_version = 3.7
disallow_untyped_calls = False
ignore_errors = False
show_error_codes = True
strict_equality = True
warn_unused_ignores = False
warn_unused_configs = True
plugins =
    mypy_django_plugin.main,
    mypy_drf_plugin.main
  • Python version used: 3.7.10
  • Operating system and version: MacOS Big Sur 11.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcrash"Internal error" crashes from mypymypy-pluginIssues specific to mypy_django_plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions