mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-07 12:44:29 +08:00
various fixes
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from typing import cast, Type
|
||||
from typing import Type, cast
|
||||
|
||||
from django.db.models.base import Model
|
||||
from django.db.models.fields.related import ForeignKey
|
||||
@@ -18,7 +17,7 @@ from mypy_django_plugin.transformers import fields
|
||||
from mypy_django_plugin.transformers.fields import get_field_descriptor_types
|
||||
|
||||
|
||||
class ModelClassInitializer(metaclass=ABCMeta):
|
||||
class ModelClassInitializer:
|
||||
def __init__(self, ctx: ClassDefContext, django_context: DjangoContext):
|
||||
self.api = cast(NewSemanticAnalyzer, ctx.api)
|
||||
self.model_classdef = ctx.cls
|
||||
@@ -52,7 +51,6 @@ class ModelClassInitializer(metaclass=ABCMeta):
|
||||
return
|
||||
self.run_with_model_cls(model_cls)
|
||||
|
||||
@abstractmethod
|
||||
def run_with_model_cls(self, model_cls):
|
||||
pass
|
||||
|
||||
@@ -70,7 +68,7 @@ class InjectAnyAsBaseForNestedMeta(ModelClassInitializer):
|
||||
to get around incompatible Meta inner classes for different models.
|
||||
"""
|
||||
|
||||
def run_with_model_cls(self, model_cls: Type[Model])-> None:
|
||||
def run(self) -> None:
|
||||
meta_node = helpers.get_nested_meta_node_for_current_class(self.model_classdef.info)
|
||||
if meta_node is None:
|
||||
return None
|
||||
@@ -78,7 +76,7 @@ class InjectAnyAsBaseForNestedMeta(ModelClassInitializer):
|
||||
|
||||
|
||||
class AddDefaultPrimaryKey(ModelClassInitializer):
|
||||
def run_with_model_cls(self, model_cls: Type[Model])-> None:
|
||||
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
|
||||
auto_field = model_cls._meta.auto_field
|
||||
if auto_field and not self.model_classdef.info.has_readable_member(auto_field.attname):
|
||||
# autogenerated field
|
||||
@@ -91,7 +89,7 @@ class AddDefaultPrimaryKey(ModelClassInitializer):
|
||||
|
||||
|
||||
class AddRelatedModelsId(ModelClassInitializer):
|
||||
def run_with_model_cls(self, model_cls: Type[Model])-> None:
|
||||
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
|
||||
for field in model_cls._meta.get_fields():
|
||||
if isinstance(field, ForeignKey):
|
||||
rel_primary_key_field = self.django_context.get_primary_key_field(field.related_model)
|
||||
@@ -103,7 +101,7 @@ class AddRelatedModelsId(ModelClassInitializer):
|
||||
|
||||
|
||||
class AddManagers(ModelClassInitializer):
|
||||
def run_with_model_cls(self, model_cls: Type[Model])-> None:
|
||||
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
|
||||
for manager_name, manager in model_cls._meta.managers_map.items():
|
||||
if manager_name not in self.model_classdef.info.names:
|
||||
manager_fullname = helpers.get_class_fullname(manager.__class__)
|
||||
@@ -140,7 +138,7 @@ class AddManagers(ModelClassInitializer):
|
||||
|
||||
|
||||
class AddExtraFieldMethods(ModelClassInitializer):
|
||||
def run_with_model_cls(self, model_cls: Type[Model])-> None:
|
||||
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
|
||||
# get_FOO_display for choices
|
||||
for field in self.django_context.get_model_fields(model_cls):
|
||||
if field.choices:
|
||||
@@ -172,7 +170,7 @@ class AddExtraFieldMethods(ModelClassInitializer):
|
||||
|
||||
|
||||
class AddMetaOptionsAttribute(ModelClassInitializer):
|
||||
def run_with_model_cls(self, model_cls: Type[Model])-> None:
|
||||
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
|
||||
if '_meta' not in self.model_classdef.info.names:
|
||||
options_info = self.lookup_typeinfo_or_incomplete_defn_error(fullnames.OPTIONS_CLASS_FULLNAME)
|
||||
self.add_new_node_to_model_class('_meta',
|
||||
|
||||
Reference in New Issue
Block a user