Skip to content

mypy can’t build migration graph #1312

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
VityasZV opened this issue Jan 6, 2023 · 5 comments · May be fixed by #1341
Closed

mypy can’t build migration graph #1312

VityasZV opened this issue Jan 6, 2023 · 5 comments · May be fixed by #1341
Labels
question Further information is requested

Comments

@VityasZV
Copy link

VityasZV commented Jan 6, 2023

Bug report

What's wrong

i have a django project, is runs and works correctly. But when I try to launch mypy, it shows some errors:
`mypy src --install-types --namespace-packages --implicit-reexport --config-file ./pyproject.toml
Error constructing plugin instance of NewSemanalDjangoPlugin

Traceback (most recent call last):
  File "/Users/viktorzadabin/HipHub/backend/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 95, in main
  File "mypy/main.py", line 174, in run_build
  File "mypy/build.py", line 193, in build
  File "mypy/build.py", line 248, in _build
  File "mypy/build.py", line 507, in load_plugins
  File "mypy/build.py", line 488, in load_plugins_from_config
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/mypy_django_plugin/main.py", line 73, in __init__
    self.django_context = DjangoContext(self.plugin_config.django_settings_module)
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/mypy_django_plugin/django/context.py", line 83, in __init__
    apps, settings = initialize_django(self.django_settings_module)
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/mypy_django_plugin/django/context.py", line 67, in initialize_django
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django_prometheus/apps.py", line 23, in ready
    ExportMigrations()
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django_prometheus/migrations.py", line 52, in ExportMigrations
    executor = MigrationExecutor(connections[alias])
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/db/migrations/loader.py", line 58, in __init__
    self.build_graph()
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/db/migrations/loader.py", line 276, in build_graph
    self.graph.validate_consistency()
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/db/migrations/graph.py", line 198, in validate_consistency
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/db/migrations/graph.py", line 198, in <listcomp>
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/db/migrations/graph.py", line 60, in raise_error
    raise NodeNotFoundError(self.error_message, self.key, origin=self.origin)
django.db.migrations.exceptions.NodeNotFoundError: Migration hub.0002_initial dependencies reference nonexistent parent node ('profile', '0001_initial')

but django server runs correctly with all applied migrations

How is that should be

mypy should launch successfully

System information

  • OS: MacOS Ventura 13.1
  • python version: 3.10.9
  • django version: 4.1.4
  • mypy version: 0.991
  • django-stubs version: 1.13.1
  • django-stubs-ext version: 0.7.0
@VityasZV VityasZV added the bug Something isn't working label Jan 6, 2023
@intgr
Copy link
Collaborator

intgr commented Jan 9, 2023

This error seems to indicate that Django can't find the profile application in INSTALLED_APPS.

Maybe INSTALLED_APPS differs between the settings file used in mypy (django_settings_module in mypy configuration) and the settings you're using normally?

@intgr
Copy link
Collaborator

intgr commented Jan 9, 2023

In any case this looks like misconfiguration of Django, not a bug in django-stubs.

@intgr intgr added question Further information is requested and removed bug Something isn't working labels Jan 9, 2023
@VityasZV
Copy link
Author

hmmm, seems that mypy looks to correct settings.py file, which contains following INSTALLED_APPS:

INSTALLED_APPS = [
    "grappelli",
    "django.contrib.postgres",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.sites",
    "allauth",
    "allauth.account",
    "allauth.socialaccount",
    "allauth.socialaccount.providers.google",
    # "allauth.socialaccount.providers.apple",
    # "allauth.socialaccount.providers.facebook",
    # "allauth.socialaccount.providers.telegram",
    # "allauth.socialaccount.providers.vk",
    # "allauth.socialaccount.providers.yandex",
    # "allauth.socialaccount.providers.mailru",
    "ninja_extra",
    "ninja_jwt",
    "django.contrib.staticfiles",
    #"django_prometheus",
    "django_dramatiq",
    "hub",
    "profile",
    "tag",
]

so, profile app is there.
Снимок экрана 2023-01-14 в 21 43 24

I’ve tried some approaches, for example, I’ve deleted django_prometheus from installed apps, and error text has changed:
(venv) ➜ backend git:(feature/hip-47) ✗ make mypy
mypy . --install-types --namespace-packages --implicit-reexport --config-file ./pyproject.toml
Traceback (most recent call last):
File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/apps/config.py", line 235, in get_model
return self.models[model_name.lower()]
KeyError: 'hiphubuser'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/viktorzadabin/HipHub/backend/venv/bin/mypy", line 8, in
sys.exit(console_entry())
File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/mypy/main.py", line 15, in console_entry
main()
File "mypy/main.py", line 95, in main
File "mypy/main.py", line 174, in run_build
File "mypy/build.py", line 193, in build
File "mypy/build.py", line 276, in _build
File "mypy/build.py", line 2850, in dispatch
File "mypy/build.py", line 3026, in load_graph
File "mypy/build.py", line 1999, in init
File "mypy/build.py", line 2290, in compute_dependencies
File "mypy/plugin.py", line 835, in get_additional_deps
File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/mypy_django_plugin/main.py", line 154, in get_additional_deps
related_model_cls = self.django_context.get_field_related_model_cls(field)
File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/mypy_django_plugin/django/context.py", line 347, in get_field_related_model_cls
related_model_cls = self.apps_registry.get_model(related_model_cls)
File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/apps/registry.py", line 213, in get_model
return app_config.get_model(model_name, require_ready=require_ready)
File "/Users/viktorzadabin/HipHub/backend/venv/lib/python3.10/site-packages/django/apps/config.py", line 237, in get_model
raise LookupError(
LookupError: App 'profile' doesn't have a 'HipHubUser' model.

Maybe now it’s easier to help me with this situation

@intgr
Copy link
Collaborator

intgr commented Jan 25, 2023

PR #1342 by flaeppe changed this logic to indicate a mypy error in code, instead of crashing. This can be suppressed with a # type: ignore comment.

If the next release doesn't fix your use case, feel free to reopen.

@intgr intgr closed this as completed Jan 25, 2023
@intgr
Copy link
Collaborator

intgr commented Jan 26, 2023

1.14.0 has been released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

Successfully merging a pull request may close this issue.

2 participants