Skip to content

Commit

Permalink
Keep abstract Django models internally in the plugin (#2017)
Browse files Browse the repository at this point in the history
Renders `class Meta` declarations unnecessary in any .pyi files, for
database models.

Ref: #2000
  • Loading branch information
flaeppe authored Mar 19, 2024
1 parent 85fa548 commit 71f7ada
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
3 changes: 0 additions & 3 deletions django-stubs/contrib/auth/base_user.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class AbstractBaseUser(models.Model):
last_login = models.DateTimeField(blank=True, null=True)
is_active: bool | BooleanField[bool | Combinable, bool]

class Meta:
abstract: Literal[True]

def get_username(self) -> str: ...
def natural_key(self) -> tuple[str]: ...
@property
Expand Down
6 changes: 0 additions & 6 deletions django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ class PermissionsMixin(models.Model):
groups = models.ManyToManyField(Group)
user_permissions = models.ManyToManyField(Permission)

class Meta:
abstract: Literal[True]

def get_user_permissions(self, obj: _AnyUser | None = ...) -> set[str]: ...
def get_group_permissions(self, obj: _AnyUser | None = ...) -> set[str]: ...
def get_all_permissions(self, obj: _AnyUser | None = ...) -> set[str]: ...
Expand All @@ -87,9 +84,6 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
EMAIL_FIELD: str
USERNAME_FIELD: str

class Meta:
abstract: Literal[True]

def get_full_name(self) -> str: ...
def get_short_name(self) -> str: ...
def email_user(
Expand Down
5 changes: 1 addition & 4 deletions django-stubs/contrib/sessions/base_session.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Any, Literal, TypeVar
from typing import Any, TypeVar

from django.contrib.sessions.backends.base import SessionBase
from django.db import models
Expand All @@ -16,9 +16,6 @@ class AbstractBaseSession(models.Model):
session_key: str
objects: Any

class Meta:
abstract: Literal[True]

@classmethod
def get_session_store_class(cls) -> type[SessionBase] | None: ...
def get_decoded(self) -> dict[str, Any]: ...
9 changes: 9 additions & 0 deletions mypy_django_plugin/lib/fullnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@

OBJECT_DOES_NOT_EXIST = "django.core.exceptions.ObjectDoesNotExist"
MULTIPLE_OBJECTS_RETURNED = "django.core.exceptions.MultipleObjectsReturned"

DJANGO_ABSTRACT_MODELS = frozenset(
(
"django.contrib.auth.base_user.AbstractBaseUser",
ABSTRACT_USER_MODEL_FULLNAME,
PERMISSION_MIXIN_CLASS_FULLNAME,
"django.contrib.sessions.base_session.AbstractBaseSession",
)
)
3 changes: 3 additions & 0 deletions mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ def add_new_manager_base(api: SemanticAnalyzerPluginInterface, fullname: str) ->


def is_abstract_model(model: TypeInfo) -> bool:
if model.fullname in fullnames.DJANGO_ABSTRACT_MODELS:
return True

if not is_model_type(model):
return False

Expand Down

0 comments on commit 71f7ada

Please sign in to comment.