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

Restore inner class Meta in models where it is accessible #2168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions django-stubs/contrib/admin/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class LogEntryManager(models.Manager[LogEntry]):
) -> LogEntry: ...

class LogEntry(models.Model):
# Note: do not create `class Meta` here.
action_time: models.DateTimeField
user: models.ForeignKey
content_type: models.ForeignKey
Expand Down
3 changes: 3 additions & 0 deletions django-stubs/contrib/auth/base_user.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class AbstractBaseUser(models.Model):
last_login = models.DateTimeField(blank=True, null=True)
is_active: bool | BooleanField[bool | Combinable, bool]

class Meta:
abstract: bool

def get_username(self) -> str: ...
def natural_key(self) -> tuple[str]: ...
@property
Expand Down
10 changes: 10 additions & 0 deletions django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PermissionManager(models.Manager[Permission]):
def get_by_natural_key(self, codename: str, app_label: str, model: str) -> Permission: ...

class Permission(models.Model):
# Note: do not create `class Meta` here.
content_type_id: int
objects: ClassVar[PermissionManager]

Expand All @@ -32,6 +33,7 @@ class GroupManager(models.Manager[Group]):
def get_by_natural_key(self, name: str) -> Group: ...

class Group(models.Model):
# Note: do not create `class Meta` here.
objects: ClassVar[GroupManager]

name = models.CharField(max_length=150)
Expand Down Expand Up @@ -61,6 +63,9 @@ class PermissionsMixin(models.Model):
groups = models.ManyToManyField(Group)
user_permissions = models.ManyToManyField(Permission)

class Meta:
abstract: bool

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 @@ -84,6 +89,11 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
EMAIL_FIELD: str
USERNAME_FIELD: str

class Meta:
verbose_name: str
verbose_name_plural: str
abstract: bool

def get_full_name(self) -> str: ...
def get_short_name(self) -> str: ...
def email_user(
Expand Down
1 change: 1 addition & 0 deletions django-stubs/contrib/contenttypes/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ContentTypeManager(models.Manager[ContentType]):
def clear_cache(self) -> None: ...

class ContentType(models.Model):
# Note: do not create `class Meta` here.
id: int
app_label: models.CharField
model: models.CharField
Expand Down
1 change: 1 addition & 0 deletions django-stubs/contrib/flatpages/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from django.contrib.sites.models import Site
from django.db import models

class FlatPage(models.Model):
# Note: do not create `class Meta` here.
url: models.CharField
title: models.CharField
content: models.TextField
Expand Down
1 change: 1 addition & 0 deletions django-stubs/contrib/redirects/models.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models

class Redirect(models.Model):
# Note: do not create `class Meta` here.
site: models.ForeignKey
old_path: models.CharField
new_path: models.CharField
5 changes: 5 additions & 0 deletions django-stubs/contrib/sessions/base_session.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class AbstractBaseSession(models.Model):
session_key: str
objects: Any

class Meta:
verbose_name: str
verbose_name_plural: str
abstract: bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Literal[True]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using Literal[True] here won't help anyone, to the contrary.

If the user subclasses this Meta and sets abstract = False, mypy will consider that a type error since it's not True. But it would be perfectly legal usage.


@classmethod
def get_session_store_class(cls) -> type[SessionBase] | None: ...
def get_decoded(self) -> dict[str, Any]: ...
1 change: 1 addition & 0 deletions django-stubs/contrib/sites/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SiteManager(models.Manager[Site]):
def get_by_natural_key(self, domain: str) -> Site: ...

class Site(models.Model):
# Note: do not create `class Meta` here.
objects: ClassVar[SiteManager]

domain = models.CharField(max_length=100)
Expand Down
7 changes: 1 addition & 6 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,10 @@ django.db.models.query_utils.Q.referenced_base_fields
django.contrib.gis.db.models.Q.referenced_base_fields
django.db.backends.oracle.features.DatabaseFeatures.django_test_skips

# Ignore missing inner `Meta` class, see PR #2000 for the related discussion
django.contrib.auth.base_user.AbstractBaseUser.Meta
# Ignore missing inner `Meta` class, see PR #2000 and #2168
django.contrib.auth.forms.BaseUserCreationForm.Meta
django.contrib.auth.forms.UserChangeForm.Meta
django.contrib.auth.models.AbstractBaseUser.Meta
django.contrib.auth.models.AbstractUser.Meta
django.contrib.auth.models.PermissionsMixin.Meta
django.contrib.flatpages.forms.FlatpageForm.Meta
django.contrib.sessions.base_session.AbstractBaseSession.Meta

# Custom __str__ that we don't want to overcomplicate:
django.forms.utils.RenderableMixin.__str__
Expand Down
Loading