Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy crashes when it encounters Django ORM query with self-referencing relationship #10549

Closed
jacobjove opened this issue May 29, 2021 · 2 comments
Labels

Comments

@jacobjove
Copy link

jacobjove commented May 29, 2021

Crash Report

(Tell us what happened.)

Traceback

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.9/site-packages/mypy_django_plugin/transformers/orm_lookups.py", line 31, in typecheck_queryset_filter
    lookup_type = django_context.resolve_lookup_expected_type(ctx, model_cls, lookup_kwarg)
  File "/Users/jacob/modularhistory/.venv/lib/python3.9/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.9/site-packages/django/db/models/sql/query.py", line 1052, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/Users/jacob/modularhistory/.venv/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1484, in names_to_path
    pathinfos = field.get_path_info(filtered_relation)
  File "/Users/jacob/modularhistory/.venv/lib/python3.9/site-packages/django/db/models/fields/related.py", line 723, in get_path_info
    opts = self.remote_field.model._meta
AttributeError: 'str' object has no attribute '_meta'
apps/trees/models.py:89: : note: use --pdb to drop into pdb

I did some debugging and found that when this 'str' object has no attribute '_meta' error happens, the value of self.remote_field is 'self'.

To Reproduce

Here's the abridged content of my models.py:

"""Based on https://github.com/peopledoc/django-ltree-demo."""

from typing import Type

from django.core.exceptions import ValidationError
from django.db.models.query import QuerySet
from django.utils.translation import ugettext_lazy as _

from core.models.model import Model


class TreeModel(Model):
    """Implements Postgres ltree for self-referencing hierarchy."""

    parent = models.ForeignKey(
        to='self',
        null=True,
        blank=True,
        related_name='children',
        on_delete=models.CASCADE,
        verbose_name=_('parent'),
    )
    name = models.TextField(verbose_name=_('name'))

    # https://docs.djangoproject.com/en/dev/ref/models/options/#model-meta-options
    class Meta:
        abstract = True

    @property
    def siblings(self) -> QuerySet['TreeModel']:
        """Return the model instances's siblings."""
        # Here is where it fails:
        return self.__class__.objects.exclude(id=self.id).filter(parent_id=self.parent_id)
@jacobjove jacobjove changed the title Mypy crashes when it encounters Django ORM lookup traversing self-referencing m2m relationship Mypy crashes when it encounters Django ORM lookup traversing self-referencing relationship May 29, 2021
@jacobjove jacobjove changed the title Mypy crashes when it encounters Django ORM lookup traversing self-referencing relationship Mypy crashes when it encounters Django ORM query with self-referencing relationship May 29, 2021
@JelleZijlstra
Copy link
Member

This seems like an issue with mypy_django_plugin, which is a project separate from mypy itself.

@jacobjove
Copy link
Author

Moved this issue to typeddjango: typeddjango/django-stubs#626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants