mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-08 04:54:48 +08:00
add support for Apps.get_model for migrations
This commit is contained in:
24
mypy_django_plugin/plugins/migrations.py
Normal file
24
mypy_django_plugin/plugins/migrations.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from typing import cast
|
||||
|
||||
from mypy.checker import TypeChecker
|
||||
from mypy.nodes import TypeInfo
|
||||
from mypy.plugin import MethodContext
|
||||
from mypy.types import Type, Instance, TypeType
|
||||
|
||||
from mypy_django_plugin import helpers
|
||||
|
||||
|
||||
def determine_model_cls_from_string_for_migrations(ctx: MethodContext) -> Type:
|
||||
app_label = ctx.args[ctx.callee_arg_names.index('app_label')][0].value
|
||||
model_name = ctx.args[ctx.callee_arg_names.index('model_name')][0].value
|
||||
|
||||
api = cast(TypeChecker, ctx.api)
|
||||
model_fullname = helpers.get_model_fullname(app_label, model_name, all_modules=api.modules)
|
||||
|
||||
if model_fullname is None:
|
||||
return ctx.default_return_type
|
||||
model_info = helpers.lookup_fully_qualified_generic(model_fullname,
|
||||
all_modules=api.modules)
|
||||
if model_info is None or not isinstance(model_info, TypeInfo):
|
||||
return ctx.default_return_type
|
||||
return TypeType(Instance(model_info, []))
|
||||
Reference in New Issue
Block a user