Skip to content

AssertionError: Type info for django.db.models.query._QuerySet missing #1323

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

Closed
alexlatif opened this issue Jan 13, 2023 · 8 comments · Fixed by #1728
Closed

AssertionError: Type info for django.db.models.query._QuerySet missing #1323

alexlatif opened this issue Jan 13, 2023 · 8 comments · Fixed by #1728
Labels
bug Something isn't working crash "Internal error" crashes from mypy mypy-plugin Issues specific to mypy_django_plugin

Comments

@alexlatif
Copy link

alexlatif commented Jan 13, 2023

VScode mypy Django QuerySet error

Hi, I am not using anything out of the ordinary other than mypy and django-stubs. When my VScode runs mypy it crashes with this traceback below.

  File "/venv/lib/python3.11/site-packages/mypy_django_plugin/main.py", line 50, in transform_model_class
    process_model_class(ctx, django_context)
  File "/venv/lib/python3.11/site-packages/mypy_django_plugin/transformers/models.py", line 674, in process_model_class
    initializer_cls(ctx, django_context).run()
  File "/venv/lib/python3.11/site-packages/mypy_django_plugin/transformers/models.py", line 96, in run
    self.run_with_model_cls(model_cls)
  File "/venv/lib/python3.11/site-packages/mypy_django_plugin/transformers/models.py", line 377, in run_with_model_cls
    fallback_manager_info = self.get_or_create_manager_with_any_fallback()
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/mypy_django_plugin/transformers/models.py", line 131, in get_or_create_manager_with_any_fallback
    fallback_queryset = self.get_or_create_queryset_with_any_fallback()
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.11/site-packages/mypy_django_plugin/transformers/models.py", line 181, in get_or_create_queryset_with_any_fallback
    assert base_queryset_info, f"Type info for {fullnames.QUERYSET_CLASS_FULLNAME} missing"
AssertionError: Type info for django.db.models.query._QuerySet missing

mypy almost always seems to crash with VScode open. I had originally patched files here

File "/venv/lib/python3.11/site-packages/mypy_django_plugin/transformers/models.py", line 155, in get_or_create_queryset_with_any_fallback

But it was a very crude way of getting it to work.

It does on occasion work for a few minutes (or at least not error) but I cannot seem to replicate its success on command.

I have been through several open and closed issues closely related but can't find anything similar to what I am facing.

I also don't know if this bug will be covered by this release. - #1316

Would be grateful for any help on this as its stopping my VScode from running mypy on my code and get a popup appear everytime I do a code change.

System information

  • OS: Mac M2 Ventura 13.1
  • python version: 3.11.0
  • django version: 4.1.4
  • mypy version: 0.991
  • django-stubs version: 1.13.0
  • django-stubs-ext version: 0.7.0
  • vscode version: 1.74.3
  • 'vscode-mypy-extension` version: v0.2.2
@alexlatif alexlatif added the bug Something isn't working label Jan 13, 2023
@intgr intgr added mypy-plugin Issues specific to mypy_django_plugin crash "Internal error" crashes from mypy labels Jan 13, 2023
@intgr
Copy link
Collaborator

intgr commented Jan 19, 2023

I also don't know if this bug will be covered by this release. - #1316

I'm afraid not, sorry. The only fix to plugin code that's currently currently merged on master is #1317.

We currently don't have people actively working on mypy-plugin bugs, besides some drive-by contributors.

@ljodal
Copy link
Contributor

ljodal commented Jan 20, 2023

This looks a lot like code I've written 😇 Do you have any additional pointers to how this happens? I've never seen this bug before, but it seems a bit strange to me. Basically the error means that mypy has not loaded the Django models when checking a model file, which seems strange. I assumed that would always be the case. I can throw together a potential fix that you can try

@alexrudd2
Copy link

Can reproduce with a traceback at the exact same line. Running CLI mypy (no VSCode) in a pipenv.

  • OS: Mac M1 Ventura 13.1
  • python version: 3.10.9
  • mypy version: 1.1.1
❯ pipenv graph | grep django
django-crispy-forms==1.14.0
django-extensions==3.2.1
django-filter==22.1
django-maintenance-mode==0.18.0
django-phone-field==1.8.1
django-select2==8.1.0
  - django [required: >=3.2, installed: 4.1.7]
  - django-appconf [required: >=0.6.0, installed: 1.0.5]
    - django [required: Any, installed: 4.1.7]
django-tempus-dominus==5.1.2.17
  - django [required: Any, installed: 4.1.7]
djangorestframework-api-key==2.3.0
djangorestframework-csv==2.1.1
  - djangorestframework [required: Any, installed: 3.14.0]
    - django [required: >=3.0, installed: 4.1.7]
djangorestframework-datatables==0.7.0
  - djangorestframework [required: >=3.7.0, installed: 3.14.0]
    - django [required: >=3.0, installed: 4.1.7]
djangorestframework-stubs==1.9.1
  - django-stubs [required: >=1.13.0, installed: 1.15.0]
    - django [required: Any, installed: 4.1.7]
    - django-stubs-ext [required: >=0.7.0, installed: 0.7.0]
      - django [required: Any, installed: 4.1.7]

@ljodal
Copy link
Contributor

ljodal commented Mar 8, 2023

Do you have an example of a file that triggers this? I'm not able to reproduce this, so I'm not really able to fix it until I can understand how/why it fails

@alexrudd2
Copy link

Aha. The problem is caused by having both django-stubs and django-types installed at the same time.

It persists even after uninstalling django-types (some sort of mypy cache, I assume).
To fix, both django-types and django-stubs must be uninstalled, and then django-stubs re-installed.

I realize django-types is a fork, and it doesn't make sense to use both. Still, it's pretty easy to get confused as a newcomer. Is it possible to have the packages explicitly conflict?

@ljodal
Copy link
Contributor

ljodal commented Mar 8, 2023

Ah, thanks! I can try to see if I can make it handle that case or at the very least point to this issue when it pops up

@alexlatif
Copy link
Author

@ljodal hello mate, sorry for not getting back to you earlier as I didnt see the notification. I dont actually have django-types. What can I provide to help debug this? I am still running without the VScode mypy extension currently to avoid the error.

@reeteshranjan
Copy link

reeteshranjan commented Jul 1, 2023

Have you re-checked if you are using the right python virtual environment? I faced this and another issue when I moved to a new laptop with Ventura, and did not select the right virtual env with my project dependencies installed, but the system python interpreter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crash "Internal error" crashes from mypy mypy-plugin Issues specific to mypy_django_plugin
Development

Successfully merging a pull request may close this issue.

5 participants