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

Remove class Meta from Model and Form class stubs #2000

Merged
merged 24 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
00dd125
remove incorrect `forms.BaseForm.Meta`
jorenham Mar 11, 2024
95f5c69
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 11, 2024
bcc175b
add missing `Meta` for specific forms
jorenham Mar 11, 2024
b4476d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 11, 2024
dca57cb
corrected unbound typevars for form Meta classes
jorenham Mar 11, 2024
2d50e21
add `django_stubs_ext.db.models.ModelMeta`
jorenham Mar 12, 2024
cfa2675
add `django_stubs_ext.db.models.Mode;WithMeta`
jorenham Mar 12, 2024
48f754c
make models with `Meta` a `ModelWithMeta`
jorenham Mar 12, 2024
b32e9ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 12, 2024
68cd002
removed `ModelWithMeta`
jorenham Mar 12, 2024
4232c7e
fix subtypes of `ModelWithMeta`
jorenham Mar 12, 2024
2434812
Merge branch 'master' into patch-1
jorenham Mar 18, 2024
8c42cfc
fix misplaced `import`
jorenham Mar 18, 2024
2336642
remove unused allowlist entries
jorenham Mar 18, 2024
5a185b2
Merge branch 'master' into patch-1
jorenham Mar 19, 2024
9cc9b4f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 19, 2024
7c7773d
restored some allowlist entries
jorenham Mar 19, 2024
33b5bee
remove inner `Meta` types in forms and models
jorenham Mar 25, 2024
c5029e0
removed unused `ModelMeta` union type
jorenham Mar 25, 2024
637b630
remove unused allowlist entries
jorenham Mar 25, 2024
130bfe1
add allowlist entries for inner `Meta`
jorenham Mar 25, 2024
4501bb7
Update ext/django_stubs_ext/db/models/__init__.py
jorenham Apr 2, 2024
304ea1f
undo misplaced typing fix
jorenham Apr 2, 2024
eee1abc
move `Meta` entries from `allowlist_todo.txt` to `allowlist.txt`, as …
jorenham Apr 2, 2024
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
5 changes: 3 additions & 2 deletions django-stubs/contrib/auth/base_user.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ from django.db.models.base import Model
from django.db.models.expressions import Combinable
from django.db.models.fields import BooleanField

from django_stubs_ext.db.models import ModelMeta as _ModelMeta

_T = TypeVar("_T", bound=Model)

class BaseUserManager(models.Manager[_T]):
Expand All @@ -20,8 +22,7 @@ class AbstractBaseUser(models.Model):
last_login = models.DateTimeField(blank=True, null=True)
is_active: bool | BooleanField[bool | Combinable, bool]

class Meta:
abstract: Literal[True]
Meta: ClassVar[type[_ModelMeta]]

def get_username(self) -> str: ...
def natural_key(self) -> tuple[str]: ...
Expand Down
16 changes: 13 additions & 3 deletions django-stubs/contrib/auth/forms.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from collections.abc import Iterable
from typing import Any, TypeVar
from collections.abc import Iterable, Mapping
from typing import Any, Literal, Sequence, TypeVar

from django import forms
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.fields import _ErrorMessagesDict
from django.forms.fields import _ClassLevelWidgetT
from django.forms.fields import Field, _ClassLevelWidgetT
from django.forms.widgets import Widget
from django.http.request import HttpRequest

Expand All @@ -28,6 +28,11 @@ class UsernameField(forms.CharField):
def widget_attrs(self, widget: Widget) -> dict[str, Any]: ...

class BaseUserCreationForm(forms.ModelForm[_User]):
class Meta:
model: type[AbstractBaseUser] = ...
fields: Sequence[str] | Literal["__all__"] = ...
field_classes: Mapping[str, type[Field]] = ...

error_messages: _ErrorMessagesDict
password1: forms.Field
password2: forms.Field
Expand All @@ -39,6 +44,11 @@ class UserCreationForm(BaseUserCreationForm[_User]):
def clean_username(self) -> str: ...

class UserChangeForm(forms.ModelForm[_User]):
class Meta:
model: type[AbstractBaseUser] = ...
fields: Sequence[str] | Literal["__all__"] = ...
field_classes: Mapping[str, type[Field]] = ...

password: forms.Field
def __init__(self, *args: Any, **kwargs: Any) -> None: ...

Expand Down
8 changes: 4 additions & 4 deletions django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ from django.db.models.manager import EmptyManager
from django.utils.functional import _StrOrPromise
from typing_extensions import Self, TypeAlias

from django_stubs_ext.db.models import ModelMeta as _ModelMeta

_AnyUser: TypeAlias = Model | AnonymousUser

def update_last_login(sender: type[AbstractBaseUser], user: AbstractBaseUser, **kwargs: Any) -> None: ...
Expand Down Expand Up @@ -61,8 +63,7 @@ class PermissionsMixin(models.Model):
groups = models.ManyToManyField(Group)
user_permissions = models.ManyToManyField(Permission)

class Meta:
abstract: Literal[True]
Meta: ClassVar[type[_ModelMeta]]
jorenham marked this conversation as resolved.
Show resolved Hide resolved

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

class Meta:
abstract: Literal[True]
Meta: ClassVar[type[_ModelMeta]]

def get_full_name(self) -> str: ...
def get_short_name(self) -> str: ...
Expand Down
9 changes: 7 additions & 2 deletions django-stubs/contrib/flatpages/forms.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import Any
from typing import Any, Literal, Sequence

from django import forms
from django.contrib.flatpages.models import FlatPage

class FlatpageForm(forms.ModelForm[FlatPage]):
class Meta:
model: type[FlatPage] = ...
fields: Sequence[str] | Literal["__all__"] = ...

class FlatpageForm(forms.ModelForm):
url: Any
def clean_url(self) -> str: ...
14 changes: 4 additions & 10 deletions django-stubs/contrib/gis/db/backends/oracle/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ from django.contrib.gis.db.backends.base.models import SpatialRefSysMixin
from django.db.models.manager import Manager
from typing_extensions import Self

from django_stubs_ext.db.models import ModelMeta as _ModelMeta

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

class Meta:
app_label: str
db_table: str
managed: bool
Meta: ClassVar[type[_ModelMeta]]

@classmethod
def table_name_col(cls) -> Any: ...
Expand All @@ -29,11 +27,7 @@ class OracleSpatialRefSys(models.Model, SpatialRefSysMixin):
wktext: Any
cs_bounds: Any
objects: ClassVar[Manager[Self]]

class Meta:
app_label: str
db_table: str
managed: bool
Meta: ClassVar[type[_ModelMeta]]

@property
def wkt(self) -> Any: ...
14 changes: 4 additions & 10 deletions django-stubs/contrib/gis/db/backends/postgis/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ from django.contrib.gis.db.backends.base.models import SpatialRefSysMixin
from django.db import models
from typing_extensions import Self

from django_stubs_ext.db.models import ModelMeta as _ModelMeta

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

class Meta:
app_label: str
db_table: str
managed: bool
Meta: ClassVar[type[_ModelMeta]]

@classmethod
def table_name_col(cls) -> Any: ...
Expand All @@ -31,11 +29,7 @@ class PostGISSpatialRefSys(models.Model, SpatialRefSysMixin):
srtext: Any
proj4text: Any
objects: ClassVar[models.Manager[Self]]

class Meta:
app_label: str
db_table: str
managed: bool
Meta: ClassVar[type[_ModelMeta]]

@property
def wkt(self) -> Any: ...
5 changes: 1 addition & 4 deletions django-stubs/forms/forms.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Iterable, Iterator, Mapping, Sequence
from collections.abc import Iterable, Iterator, Mapping
from typing import Any, ClassVar

from django.core.exceptions import ValidationError
Expand All @@ -13,9 +13,6 @@ from django.utils.safestring import SafeString
class DeclarativeFieldsMetaclass(MediaDefiningClass): ...

class BaseForm(RenderableFormMixin):
class Meta:
fields: Sequence[str]

default_renderer: BaseRenderer | type[BaseRenderer] | None
field_order: Iterable[str] | None
use_required_attribute: bool
Expand Down
136 changes: 131 additions & 5 deletions ext/django_stubs_ext/db/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, ClassVar, Literal, Protocol, Sequence, Tuple, Union

if TYPE_CHECKING:
from typing import ClassVar, List, Literal, Sequence, Tuple, Union
from django.db.models import BaseConstraint, Index, OrderBy
from typing_extensions import TypeAlias

from django.db.models import BaseConstraint, Index, OrderBy
from django_stubs_ext import StrOrPromise

from django_stubs_ext import StrOrPromise
if TYPE_CHECKING:

class TypedModelMeta:
"""
Expand All @@ -15,6 +15,8 @@ class TypedModelMeta:
some values are normalized by Django.
"""

from typing import List

abstract: ClassVar[bool] # default: False
app_label: ClassVar[str]
base_manager_name: ClassVar[str]
Expand Down Expand Up @@ -42,3 +44,127 @@ class TypedModelMeta:

else:
TypedModelMeta = object


class _MetaAbstract(Protocol):
abstract: ClassVar[bool] # default: False


class _MetaAppLabel(Protocol):
app_label: ClassVar[str]


class _MetaBaseManagerName(Protocol):
base_manager_name: ClassVar[str]


class _MetaDbTable(Protocol):
db_table: ClassVar[str]


class _MetaDbTableComment(Protocol):
db_table_comment: ClassVar[str]


class _MetaDbTablespace(Protocol):
db_tablespace: ClassVar[str]


class _MetaDefaultManagerName(Protocol):
default_manager_name: ClassVar[str]


class _MetaDefaultRelatedName(Protocol):
default_related_name: ClassVar[str]


class _MetaGetLatestBy(Protocol):
get_latest_by: ClassVar[Union[str, Sequence[str]]]


class _MetaManaged(Protocol):
managed: ClassVar[bool] # default: True


class _MetaOrderWithRespectTo(Protocol):
order_with_respect_to: ClassVar[str]


class _MetaOrdering(Protocol):
ordering: ClassVar[Sequence[Union[str, OrderBy]]]


class _MetaPermissions(Protocol):
permissions: ClassVar[Sequence[Tuple[str, str]]]


class _MetaDefaultPermissions(Protocol):
default_permissions: ClassVar[Sequence[str]] # default: ("add", "change", "delete", "view")


class _MetaProxy(Protocol):
proxy: ClassVar[bool] # default: False


class _MetaRequiredDbFeatures(Protocol):
required_db_features: ClassVar[Sequence[str]]


class _MetaRequiredDbVendor(Protocol):
required_db_vendor: ClassVar[Literal["sqlite", "postgresql", "mysql", "oracle"]]


class _MetaSelectOnSave(Protocol):
select_on_save: ClassVar[bool] # default: False


class _MetaIndexes(Protocol):
indexes: ClassVar[Sequence[Index]]


class _MetaUniqueTogether(Protocol):
unique_together: ClassVar[Union[Sequence[Sequence[str]], Sequence[str]]]


class _MetaIndexTogether(Protocol):
index_together: ClassVar[Union[Sequence[Sequence[str]], Sequence[str]]] # Deprecated in Django 4.2


class _MetaConstraints(Protocol):
constraints: ClassVar[Sequence[BaseConstraint]]


class _MetaVerboseName(Protocol):
verbose_name: ClassVar[StrOrPromise]


class _MetaVerboseNamePlural(Protocol):
verbose_name_plural: ClassVar[StrOrPromise]


ModelMeta: TypeAlias = Union[
_MetaAbstract,
_MetaAppLabel,
_MetaBaseManagerName,
_MetaDbTable,
_MetaDbTableComment,
_MetaDbTablespace,
_MetaDefaultManagerName,
_MetaDefaultRelatedName,
_MetaGetLatestBy,
_MetaManaged,
_MetaOrderWithRespectTo,
_MetaOrdering,
_MetaPermissions,
_MetaDefaultPermissions,
_MetaProxy,
_MetaRequiredDbFeatures,
_MetaRequiredDbVendor,
_MetaSelectOnSave,
_MetaIndexes,
_MetaUniqueTogether,
_MetaIndexTogether,
_MetaConstraints,
_MetaVerboseName,
_MetaVerboseNamePlural,
]
Loading