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 upon encountering Django ORM query with .exclude(pk=...) #10531

Closed
jacobjove opened this issue May 25, 2021 · 1 comment
Closed
Labels

Comments

@jacobjove
Copy link

jacobjove commented May 25, 2021

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
@jacobjove jacobjove changed the title Mypy crashes upon encountering custom Django ORM lookups Mypy crashes upon encountering Django ORM query with .exclude(pk=...) May 29, 2021
@jacobjove
Copy link
Author

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

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

1 participant