mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-08 04:54:48 +08:00
remove dataclasses dependency
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from dataclasses import dataclass
|
|
||||||
from typing import Any, Dict, Iterator, List, Optional, TYPE_CHECKING, Tuple, Type
|
from typing import Any, Dict, Iterator, List, Optional, TYPE_CHECKING, Tuple, Type
|
||||||
|
|
||||||
from django.core.exceptions import FieldError
|
from django.core.exceptions import FieldError
|
||||||
@@ -22,12 +21,6 @@ if TYPE_CHECKING:
|
|||||||
from django.conf import LazySettings
|
from django.conf import LazySettings
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class DjangoPluginConfig:
|
|
||||||
ignore_missing_settings: bool = False
|
|
||||||
ignore_missing_model_attributes: bool = False
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def temp_environ():
|
def temp_environ():
|
||||||
"""Allow the ability to set os.environ temporarily"""
|
"""Allow the ability to set os.environ temporarily"""
|
||||||
@@ -141,14 +134,11 @@ class DjangoLookupsContext:
|
|||||||
|
|
||||||
class DjangoContext:
|
class DjangoContext:
|
||||||
def __init__(self, plugin_toml_config: Optional[Dict[str, Any]]) -> None:
|
def __init__(self, plugin_toml_config: Optional[Dict[str, Any]]) -> None:
|
||||||
self.config = DjangoPluginConfig()
|
|
||||||
self.fields_context = DjangoFieldsContext(self)
|
self.fields_context = DjangoFieldsContext(self)
|
||||||
self.lookups_context = DjangoLookupsContext(self)
|
self.lookups_context = DjangoLookupsContext(self)
|
||||||
|
|
||||||
self.django_settings_module = None
|
self.django_settings_module = None
|
||||||
if plugin_toml_config:
|
if plugin_toml_config:
|
||||||
self.config.ignore_missing_settings = plugin_toml_config.get('ignore_missing_settings', False)
|
|
||||||
self.config.ignore_missing_model_attributes = plugin_toml_config.get('ignore_missing_model_attributes', False)
|
|
||||||
self.django_settings_module = plugin_toml_config.get('django_settings_module', None)
|
self.django_settings_module = plugin_toml_config.get('django_settings_module', None)
|
||||||
|
|
||||||
self.apps_registry: Optional[Dict[str, str]] = None
|
self.apps_registry: Optional[Dict[str, str]] = None
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import dataclasses
|
from abc import ABCMeta, abstractmethod
|
||||||
from abc import ABCMeta, abstractmethod
|
from abc import ABCMeta, abstractmethod
|
||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
from django.db.models.fields.related import ForeignKey
|
from django.db.models.fields.related import ForeignKey
|
||||||
from django.db.models.fields.reverse_related import ManyToManyRel, ManyToOneRel, OneToOneRel
|
from django.db.models.fields.reverse_related import ManyToManyRel, ManyToOneRel, OneToOneRel
|
||||||
from mypy.newsemanal.semanal import NewSemanticAnalyzer
|
from mypy.newsemanal.semanal import NewSemanticAnalyzer
|
||||||
from mypy.nodes import ClassDef, MDEF, SymbolTableNode, TypeInfo, Var
|
from mypy.nodes import MDEF, SymbolTableNode, TypeInfo, Var
|
||||||
from mypy.plugin import ClassDefContext
|
from mypy.plugin import ClassDefContext
|
||||||
from mypy.types import Instance
|
from mypy.types import Instance
|
||||||
|
|
||||||
@@ -15,19 +15,12 @@ from mypy_django_plugin.transformers import fields
|
|||||||
from mypy_django_plugin.transformers.fields import get_field_descriptor_types
|
from mypy_django_plugin.transformers.fields import get_field_descriptor_types
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
|
||||||
class ModelClassInitializer(metaclass=ABCMeta):
|
class ModelClassInitializer(metaclass=ABCMeta):
|
||||||
api: NewSemanticAnalyzer
|
def __init__(self, ctx: ClassDefContext, django_context: DjangoContext):
|
||||||
model_classdef: ClassDef
|
self.api = cast(NewSemanticAnalyzer, ctx.api)
|
||||||
django_context: DjangoContext
|
self.model_classdef = ctx.cls
|
||||||
ctx: ClassDefContext
|
self.django_context = django_context
|
||||||
|
self.ctx = ctx
|
||||||
@classmethod
|
|
||||||
def from_ctx(cls, ctx: ClassDefContext, django_context: DjangoContext):
|
|
||||||
return cls(api=cast(NewSemanticAnalyzer, ctx.api),
|
|
||||||
model_classdef=ctx.cls,
|
|
||||||
django_context=django_context,
|
|
||||||
ctx=ctx)
|
|
||||||
|
|
||||||
def lookup_typeinfo_or_incomplete_defn_error(self, fullname: str) -> TypeInfo:
|
def lookup_typeinfo_or_incomplete_defn_error(self, fullname: str) -> TypeInfo:
|
||||||
sym = self.api.lookup_fully_qualified_or_none(fullname)
|
sym = self.api.lookup_fully_qualified_or_none(fullname)
|
||||||
@@ -156,7 +149,7 @@ def process_model_class(ctx: ClassDefContext,
|
|||||||
]
|
]
|
||||||
for initializer_cls in initializers:
|
for initializer_cls in initializers:
|
||||||
try:
|
try:
|
||||||
initializer_cls.from_ctx(ctx, django_context).run()
|
initializer_cls(ctx, django_context).run()
|
||||||
except helpers.IncompleteDefnException:
|
except helpers.IncompleteDefnException:
|
||||||
if not ctx.api.final_iteration:
|
if not ctx.api.final_iteration:
|
||||||
ctx.api.defer()
|
ctx.api.defer()
|
||||||
|
|||||||
Reference in New Issue
Block a user