Skip to content
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

Declare manager class attributes on models as ClassVars #1672

Merged
merged 8 commits into from
Sep 5, 2023
4 changes: 2 additions & 2 deletions django-stubs/contrib/admin/models.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, ClassVar
from uuid import UUID

from django.db import models
Expand Down Expand Up @@ -28,7 +28,7 @@ class LogEntry(models.Model):
object_repr: models.CharField
action_flag: models.PositiveSmallIntegerField
change_message: models.TextField
objects: LogEntryManager
objects: ClassVar[LogEntryManager]
def is_addition(self) -> bool: ...
def is_change(self) -> bool: ...
def is_deletion(self) -> bool: ...
Expand Down
10 changes: 6 additions & 4 deletions django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Iterable
from typing import Any, Literal, TypeVar
from typing import Any, ClassVar, Literal, TypeVar

from django.contrib.auth.base_user import AbstractBaseUser as AbstractBaseUser
from django.contrib.auth.base_user import BaseUserManager as BaseUserManager
Expand All @@ -10,7 +10,7 @@ from django.db.models import QuerySet
from django.db.models.base import Model
from django.db.models.manager import EmptyManager
from django.utils.functional import _StrOrPromise
from typing_extensions import TypeAlias
from typing_extensions import Self, TypeAlias

_AnyUser: TypeAlias = Model | AnonymousUser

Expand All @@ -21,7 +21,7 @@ class PermissionManager(models.Manager[Permission]):

class Permission(models.Model):
content_type_id: int
objects: PermissionManager
objects: ClassVar[PermissionManager]

name = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
Expand All @@ -32,7 +32,7 @@ class GroupManager(models.Manager[Group]):
def get_by_natural_key(self, name: str) -> Group: ...

class Group(models.Model):
objects: GroupManager
objects: ClassVar[GroupManager]

name = models.CharField(max_length=150)
permissions = models.ManyToManyField(Permission)
Expand Down Expand Up @@ -81,6 +81,8 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
is_active = models.BooleanField()
date_joined = models.DateTimeField()

objects: ClassVar[UserManager[Self]]

EMAIL_FIELD: str
USERNAME_FIELD: str

Expand Down
4 changes: 2 additions & 2 deletions django-stubs/contrib/contenttypes/models.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, ClassVar

from django.db import models
from django.db.models.base import Model
Expand All @@ -15,7 +15,7 @@ class ContentType(models.Model):
id: int
app_label: models.CharField
model: models.CharField
objects: ContentTypeManager
objects: ClassVar[ContentTypeManager]
@property
def name(self) -> str: ...
def model_class(self) -> type[Model] | None: ...
Expand Down
6 changes: 5 additions & 1 deletion django-stubs/contrib/gis/db/backends/oracle/models.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from typing import Any
from typing import Any, ClassVar

from django.contrib.gis.db import models
from django.contrib.gis.db.backends.base.models import SpatialRefSysMixin
from django.db.models.manager import Manager
from typing_extensions import Self

class OracleGeometryColumns(models.Model):
table_name: Any
column_name: Any
srid: Any
objects: ClassVar[Manager[Self]]

class Meta:
app_label: str
Expand All @@ -24,6 +27,7 @@ class OracleSpatialRefSys(models.Model, SpatialRefSysMixin):
auth_name: Any
wktext: Any
cs_bounds: Any
objects: ClassVar[Manager[Self]]

class Meta:
app_label: str
Expand Down
5 changes: 4 additions & 1 deletion django-stubs/contrib/gis/db/backends/postgis/models.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any
from typing import Any, ClassVar

from django.contrib.gis.db.backends.base.models import SpatialRefSysMixin
from django.db import models
from typing_extensions import Self

class PostGISGeometryColumns(models.Model):
f_table_catalog: Any
Expand All @@ -11,6 +12,7 @@ class PostGISGeometryColumns(models.Model):
coord_dimension: Any
srid: Any
type: Any
objects: ClassVar[models.Manager[Self]]

class Meta:
app_label: str
Expand All @@ -27,6 +29,7 @@ class PostGISSpatialRefSys(models.Model, SpatialRefSysMixin):
auth_srid: Any
srtext: Any
proj4text: Any
objects: ClassVar[models.Manager[Self]]

class Meta:
app_label: str
Expand Down
5 changes: 4 additions & 1 deletion django-stubs/contrib/gis/db/backends/spatialite/models.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any
from typing import Any, ClassVar

from django.contrib.gis.db.backends.base.models import SpatialRefSysMixin
from django.db import models
from typing_extensions import Self

class SpatialiteGeometryColumns(models.Model):
f_table_name: Any
Expand All @@ -10,6 +11,7 @@ class SpatialiteGeometryColumns(models.Model):
srid: Any
spatial_index_enabled: Any
type: Any
objects: ClassVar[models.Manager[Self]]

class Meta:
app_label: str
Expand All @@ -27,6 +29,7 @@ class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin):
ref_sys_name: Any
proj4text: Any
srtext: Any
objects: ClassVar[models.Manager[Self]]

class Meta:
app_label: str
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/contrib/sites/models.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, ClassVar

from django.db import models
from django.http.request import HttpRequest
Expand All @@ -11,7 +11,7 @@ class SiteManager(models.Manager[Site]):
def get_by_natural_key(self, domain: str) -> Site: ...

class Site(models.Model):
objects: SiteManager
objects: ClassVar[SiteManager]

domain = models.CharField(max_length=100)
name = models.CharField(max_length=50)
Expand Down
5 changes: 4 additions & 1 deletion django-stubs/db/migrations/recorder.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from typing import Any
from typing import Any, ClassVar

from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models.base import Model
from django.db.models.manager import Manager
from django.db.models.query import QuerySet
from typing_extensions import Self

class MigrationRecorder:
class Migration(Model):
app: Any
name: Any
applied: Any
objects: ClassVar[Manager[Self]]
connection: BaseDatabaseWrapper
def __init__(self, connection: BaseDatabaseWrapper) -> None: ...
@property
Expand Down
9 changes: 5 additions & 4 deletions django-stubs/db/models/base.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from collections.abc import Collection, Iterable, Sequence
from typing import Any, Final, TypeVar
from typing import Any, ClassVar, Final, TypeVar

from django.core.checks.messages import CheckMessage
from django.core.exceptions import MultipleObjectsReturned as BaseMultipleObjectsReturned
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models import BaseConstraint, Field
from django.db.models.manager import BaseManager
from django.db.models.manager import BaseManager, Manager
from django.db.models.options import Options
from typing_extensions import Self

Expand All @@ -19,8 +19,6 @@ class ModelState:
fields_cache: ModelStateFieldsCacheDescriptor

class ModelBase(type):
@property
def objects(cls: type[_Self]) -> BaseManager[_Self]: ... # type: ignore[misc]
@property
def _default_manager(cls: type[_Self]) -> BaseManager[_Self]: ... # type: ignore[misc]
@property
Expand All @@ -34,6 +32,9 @@ class Model(metaclass=ModelBase):
# and re-add them to correct concrete subclasses of 'Model'
DoesNotExist: Final[type[ObjectDoesNotExist]]
MultipleObjectsReturned: Final[type[BaseMultipleObjectsReturned]]
# This 'objects' attribute will be deleted, via the plugin, in favor of managing it
# to only exist on subclasses it exists on during runtime.
objects: ClassVar[Manager[Self]]

class Meta: ...
_meta: Options[Any]
Expand Down
5 changes: 4 additions & 1 deletion mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,17 @@ def check_types_compatible(
api.check_subtype(actual_type, expected_type, ctx.context, error_message, "got", "expected")


def add_new_sym_for_info(info: TypeInfo, *, name: str, sym_type: MypyType, no_serialize: bool = False) -> None:
def add_new_sym_for_info(
info: TypeInfo, *, name: str, sym_type: MypyType, no_serialize: bool = False, is_classvar: bool = False
flaeppe marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
# type=: type of the variable itself
var = Var(name=name, type=sym_type)
# var.info: type of the object variable is bound to
var.info = info
var._fullname = info.fullname + "." + name
var.is_initialized_in_class = True
var.is_inferred = True
var.is_classvar = is_classvar
info.names[name] = SymbolTableNode(MDEF, var, plugin_generated=True, no_serialize=no_serialize)


Expand Down
23 changes: 17 additions & 6 deletions mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ def create_new_var(self, name: str, typ: MypyType) -> Var:
var.is_inferred = True
return var

def add_new_node_to_model_class(self, name: str, typ: MypyType, no_serialize: bool = False) -> None:
helpers.add_new_sym_for_info(self.model_classdef.info, name=name, sym_type=typ, no_serialize=no_serialize)
def add_new_node_to_model_class(
self, name: str, typ: MypyType, no_serialize: bool = False, is_classvar: bool = False
flaeppe marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
# TODO: Rename to signal that it is a `Var` that is added..
helpers.add_new_sym_for_info(
self.model_classdef.info, name=name, sym_type=typ, no_serialize=no_serialize, is_classvar=is_classvar
)

def add_new_class_for_current_module(self, name: str, bases: List[Instance]) -> TypeInfo:
current_module = self.api.modules[self.model_classdef.info.module_name]
Expand Down Expand Up @@ -311,7 +316,7 @@ def reparametrize_dynamically_created_manager(self, manager_name: str, manager_i
assert manager_info is not None
# Reparameterize dynamically created manager with model type
manager_type = Instance(manager_info, [Instance(self.model_classdef.info, [])])
self.add_new_node_to_model_class(manager_name, manager_type)
self.add_new_node_to_model_class(manager_name, manager_type, is_classvar=True)

def run_with_model_cls(self, model_cls: Type[Model]) -> None:
manager_info: Optional[TypeInfo]
Expand All @@ -336,7 +341,7 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
continue

manager_type = Instance(manager_info, [Instance(self.model_classdef.info, [])])
self.add_new_node_to_model_class(manager_name, manager_type)
self.add_new_node_to_model_class(manager_name, manager_type, is_classvar=True)

if incomplete_manager_defs:
if not self.api.final_iteration:
Expand All @@ -351,7 +356,9 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
# setting _some_ type
fallback_manager_info = self.get_or_create_manager_with_any_fallback()
self.add_new_node_to_model_class(
manager_name, Instance(fallback_manager_info, [Instance(self.model_classdef.info, [])])
manager_name,
Instance(fallback_manager_info, [Instance(self.model_classdef.info, [])]),
is_classvar=True,
)

# Find expression for e.g. `objects = SomeManager()`
Expand Down Expand Up @@ -433,7 +440,7 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
default_manager_info = generated_manager_info

default_manager = Instance(default_manager_info, [Instance(self.model_classdef.info, [])])
self.add_new_node_to_model_class("_default_manager", default_manager)
self.add_new_node_to_model_class("_default_manager", default_manager, is_classvar=True)


class AddRelatedManagers(ModelClassInitializer):
Expand Down Expand Up @@ -623,6 +630,10 @@ def adjust_model_class(cls, ctx: ClassDefContext) -> None:
):
del ctx.cls.info.names["MultipleObjectsReturned"]

objects = ctx.cls.info.names.get("objects")
if objects is not None and isinstance(objects.node, Var) and not objects.plugin_generated:
del ctx.cls.info.names["objects"]

return

def get_exception_bases(self, name: str) -> List[Instance]:
Expand Down
8 changes: 0 additions & 8 deletions scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,13 @@ django.contrib.gis.db.backends.oracle.features.DatabaseFeatures.supports_toleran
django.contrib.gis.db.backends.oracle.features.DatabaseFeatures.unsupported_geojson_options
django.contrib.gis.db.backends.oracle.introspection
django.contrib.gis.db.backends.oracle.models.OracleGeometryColumns.Meta
django.contrib.gis.db.backends.oracle.models.OracleGeometryColumns.objects
django.contrib.gis.db.backends.oracle.models.OracleSpatialRefSys.Meta
django.contrib.gis.db.backends.oracle.models.OracleSpatialRefSys.objects
django.contrib.gis.db.backends.oracle.operations
django.contrib.gis.db.backends.postgis.adapter.PostGISAdapter.prepare
django.contrib.gis.db.backends.postgis.features.DatabaseFeatures.empty_intersection_returns_none
django.contrib.gis.db.backends.postgis.features.DatabaseFeatures.supports_geography
django.contrib.gis.db.backends.postgis.models.PostGISGeometryColumns.Meta
django.contrib.gis.db.backends.postgis.models.PostGISGeometryColumns.objects
django.contrib.gis.db.backends.postgis.models.PostGISSpatialRefSys.Meta
django.contrib.gis.db.backends.postgis.models.PostGISSpatialRefSys.objects
django.contrib.gis.db.backends.postgis.operations.PostGISOperations.convert_extent
django.contrib.gis.db.backends.postgis.operations.PostGISOperations.convert_extent3d
django.contrib.gis.db.backends.postgis.operations.PostGISOperator.check_geography
Expand All @@ -335,9 +331,7 @@ django.contrib.gis.db.backends.spatialite.features.DatabaseFeatures.can_alter_ge
django.contrib.gis.db.backends.spatialite.features.DatabaseFeatures.django_test_skips
django.contrib.gis.db.backends.spatialite.features.DatabaseFeatures.supports_area_geodetic
django.contrib.gis.db.backends.spatialite.models.SpatialiteGeometryColumns.Meta
django.contrib.gis.db.backends.spatialite.models.SpatialiteGeometryColumns.objects
django.contrib.gis.db.backends.spatialite.models.SpatialiteSpatialRefSys.Meta
django.contrib.gis.db.backends.spatialite.models.SpatialiteSpatialRefSys.objects
django.contrib.gis.db.backends.spatialite.operations.SpatiaLiteOperations.convert_extent
django.contrib.gis.db.backends.spatialite.operations.SpatiaLiteOperations.from_text
django.contrib.gis.db.backends.spatialite.operations.SpatiaLiteOperations.geom_lib_version
Expand Down Expand Up @@ -1276,7 +1270,6 @@ django.db.migrations.questioner.NonInteractiveMigrationQuestioner.log_lack_of_mi
django.db.migrations.recorder.MigrationRecorder.Migration.get_next_by_applied
django.db.migrations.recorder.MigrationRecorder.Migration.get_previous_by_applied
django.db.migrations.recorder.MigrationRecorder.Migration.id
django.db.migrations.recorder.MigrationRecorder.Migration.objects
django.db.migrations.serializer.ChoicesSerializer
django.db.migrations.utils.FieldReference
django.db.migrations.utils.field_is_referenced
Expand Down Expand Up @@ -1534,7 +1527,6 @@ django.db.models.base.Model.Meta
django.db.models.base.Model.add_to_class
django.db.models.base.ModelBase.__new__
django.db.models.base.ModelBase.add_to_class
django.db.models.base.ModelBase.objects
django.db.models.base.ModelStateFieldsCacheDescriptor.__get__
django.db.models.base.make_foreign_order_accessors
django.db.models.base.method_get_order
Expand Down
Loading