mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-11 14:31:56 +08:00
Improves edit.py and its forms (#648)
* Improves edit.py and its forms * Adds tests
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
wheel
|
wheel
|
||||||
|
black==21.6b0
|
||||||
requests==2.24.0
|
requests==2.24.0
|
||||||
coreapi==2.3.3
|
coreapi==2.3.3
|
||||||
gitpython==3.1.9
|
gitpython==3.1.9
|
||||||
pre-commit==2.7.1
|
pre-commit==2.7.1
|
||||||
pytest==6.1.1
|
pytest==6.1.1
|
||||||
pytest-mypy-plugins==1.6.1
|
pytest-mypy-plugins==1.7.0
|
||||||
psycopg2-binary
|
psycopg2-binary
|
||||||
types-toml==0.1.1
|
types-toml==0.1.1
|
||||||
-e ./django_stubs_ext
|
-e ./django_stubs_ext
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ from django.db.models.query_utils import Q
|
|||||||
from django.db.models import Index, Func
|
from django.db.models import Index, Func
|
||||||
from django.db.models.expressions import BaseExpression, Combinable
|
from django.db.models.expressions import BaseExpression, Combinable
|
||||||
|
|
||||||
|
|
||||||
class PostgresIndex(Index): ...
|
class PostgresIndex(Index): ...
|
||||||
|
|
||||||
class BrinIndex(PostgresIndex):
|
class BrinIndex(PostgresIndex):
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from typing import (
|
|||||||
Collection,
|
Collection,
|
||||||
Dict,
|
Dict,
|
||||||
Iterator,
|
Iterator,
|
||||||
|
Generic,
|
||||||
List,
|
List,
|
||||||
Mapping,
|
Mapping,
|
||||||
MutableMapping,
|
MutableMapping,
|
||||||
@@ -78,8 +79,8 @@ class ModelFormOptions:
|
|||||||
|
|
||||||
class ModelFormMetaclass(DeclarativeFieldsMetaclass): ...
|
class ModelFormMetaclass(DeclarativeFieldsMetaclass): ...
|
||||||
|
|
||||||
class BaseModelForm(BaseForm):
|
class BaseModelForm(Generic[_M], BaseForm):
|
||||||
instance: Any = ...
|
instance: _M
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
data: Optional[Mapping[str, Any]] = ...,
|
data: Optional[Mapping[str, Any]] = ...,
|
||||||
@@ -95,10 +96,10 @@ class BaseModelForm(BaseForm):
|
|||||||
renderer: Any = ...,
|
renderer: Any = ...,
|
||||||
) -> None: ...
|
) -> None: ...
|
||||||
def validate_unique(self) -> None: ...
|
def validate_unique(self) -> None: ...
|
||||||
save_m2m: Any = ...
|
def save(self, commit: bool = ...) -> _M: ...
|
||||||
def save(self, commit: bool = ...) -> Any: ...
|
def save_m2m(self) -> None: ...
|
||||||
|
|
||||||
class ModelForm(BaseModelForm, metaclass=ModelFormMetaclass):
|
class ModelForm(BaseModelForm[_M], metaclass=ModelFormMetaclass):
|
||||||
base_fields: ClassVar[Dict[str, Field]] = ...
|
base_fields: ClassVar[Dict[str, Field]] = ...
|
||||||
|
|
||||||
def modelform_factory(
|
def modelform_factory(
|
||||||
|
|||||||
@@ -5,14 +5,17 @@ from django.forms.models import BaseModelForm
|
|||||||
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
|
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
|
||||||
from django.views.generic.detail import BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin
|
from django.views.generic.detail import BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
from django.http import HttpRequest, HttpResponse
|
from django.http import HttpRequest, HttpResponse
|
||||||
|
|
||||||
_FormT = TypeVar("_FormT", bound=BaseForm)
|
_FormT = TypeVar("_FormT", bound=BaseForm)
|
||||||
|
_ModelFormT = TypeVar("_ModelFormT", bound=BaseModelForm)
|
||||||
|
_T = TypeVar("_T", bound=models.Model)
|
||||||
|
|
||||||
class AbstractFormMixin(ContextMixin):
|
class AbstractFormMixin(Generic[_FormT], ContextMixin):
|
||||||
initial: Dict[str, Any] = ...
|
initial: Dict[str, Any] = ...
|
||||||
form_class: Optional[Type[BaseForm]] = ...
|
form_class: Optional[Type[_FormT]] = ...
|
||||||
success_url: Optional[Union[str, Callable[..., Any]]] = ...
|
success_url: Optional[Union[str, Callable[..., Any]]] = ...
|
||||||
prefix: Optional[str] = ...
|
prefix: Optional[str] = ...
|
||||||
def get_initial(self) -> Dict[str, Any]: ...
|
def get_initial(self) -> Dict[str, Any]: ...
|
||||||
@@ -26,12 +29,12 @@ class FormMixin(Generic[_FormT], AbstractFormMixin):
|
|||||||
def form_valid(self, form: _FormT) -> HttpResponse: ...
|
def form_valid(self, form: _FormT) -> HttpResponse: ...
|
||||||
def form_invalid(self, form: _FormT) -> HttpResponse: ...
|
def form_invalid(self, form: _FormT) -> HttpResponse: ...
|
||||||
|
|
||||||
class ModelFormMixin(AbstractFormMixin, SingleObjectMixin):
|
class ModelFormMixin(Generic[_T, _ModelFormT], AbstractFormMixin, SingleObjectMixin[_T]):
|
||||||
fields: Optional[Union[Sequence[str], Literal["__all__"]]] = ...
|
fields: Optional[Union[Sequence[str], Literal["__all__"]]] = ...
|
||||||
def get_form_class(self) -> Type[BaseModelForm]: ...
|
def get_form_class(self) -> Type[_ModelFormT]: ...
|
||||||
def get_form(self, form_class: Optional[Type[BaseModelForm]] = ...) -> BaseModelForm: ...
|
def get_form(self, form_class: Optional[Type[_ModelFormT]] = ...) -> BaseModelForm: ...
|
||||||
def form_valid(self, form: BaseModelForm) -> HttpResponse: ...
|
def form_valid(self, form: _ModelFormT) -> HttpResponse: ...
|
||||||
def form_invalid(self, form: BaseModelForm) -> HttpResponse: ...
|
def form_invalid(self, form: _ModelFormT) -> HttpResponse: ...
|
||||||
|
|
||||||
class ProcessFormView(View):
|
class ProcessFormView(View):
|
||||||
def get(self, request: HttpRequest, *args: str, **kwargs: Any) -> HttpResponse: ...
|
def get(self, request: HttpRequest, *args: str, **kwargs: Any) -> HttpResponse: ...
|
||||||
@@ -40,10 +43,10 @@ class ProcessFormView(View):
|
|||||||
|
|
||||||
class BaseFormView(FormMixin[_FormT], ProcessFormView): ...
|
class BaseFormView(FormMixin[_FormT], ProcessFormView): ...
|
||||||
class FormView(TemplateResponseMixin, BaseFormView[_FormT]): ...
|
class FormView(TemplateResponseMixin, BaseFormView[_FormT]): ...
|
||||||
class BaseCreateView(ModelFormMixin, ProcessFormView): ...
|
class BaseCreateView(ModelFormMixin[_T, _ModelFormT], ProcessFormView): ...
|
||||||
class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView): ...
|
class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView[_T, _ModelFormT]): ...
|
||||||
class BaseUpdateView(ModelFormMixin, ProcessFormView): ...
|
class BaseUpdateView(ModelFormMixin[_T, _ModelFormT], ProcessFormView): ...
|
||||||
class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView): ...
|
class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView[_T, _ModelFormT]): ...
|
||||||
|
|
||||||
class DeletionMixin:
|
class DeletionMixin:
|
||||||
success_url: Optional[str] = ...
|
success_url: Optional[str] = ...
|
||||||
|
|||||||
@@ -10,6 +10,8 @@
|
|||||||
"""Ensure that form can have type AuthenticationForm."""
|
"""Ensure that form can have type AuthenticationForm."""
|
||||||
form.get_user()
|
form.get_user()
|
||||||
return HttpResponseRedirect(self.get_success_url())
|
return HttpResponseRedirect(self.get_success_url())
|
||||||
|
|
||||||
|
|
||||||
- case: dispatch_http_response
|
- case: dispatch_http_response
|
||||||
main: |
|
main: |
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
@@ -19,6 +21,8 @@
|
|||||||
def dispatch(self, request, *args, **kwargs) -> HttpResponse:
|
def dispatch(self, request, *args, **kwargs) -> HttpResponse:
|
||||||
response: HttpResponse
|
response: HttpResponse
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
- case: dispatch_streaming_http_response
|
- case: dispatch_streaming_http_response
|
||||||
main: |
|
main: |
|
||||||
from django.http import StreamingHttpResponse
|
from django.http import StreamingHttpResponse
|
||||||
@@ -28,3 +32,32 @@
|
|||||||
def dispatch(self, request, *args, **kwargs) -> StreamingHttpResponse:
|
def dispatch(self, request, *args, **kwargs) -> StreamingHttpResponse:
|
||||||
response: StreamingHttpResponse
|
response: StreamingHttpResponse
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
- case: generic_form_views
|
||||||
|
main: |
|
||||||
|
from django.views.generic.edit import CreateView, UpdateView
|
||||||
|
from django import forms
|
||||||
|
from myapp.models import Article
|
||||||
|
|
||||||
|
class ArticleModelForm(forms.ModelForm[Article]):
|
||||||
|
class Meta:
|
||||||
|
model = Article
|
||||||
|
|
||||||
|
class MyCreateView(CreateView[Article, ArticleModelForm]):
|
||||||
|
def some(self) -> None:
|
||||||
|
reveal_type(self.get_form_class()) # N: Revealed type is "Type[main.ArticleModelForm*]"
|
||||||
|
|
||||||
|
class MyUpdateView(UpdateView[Article, ArticleModelForm]):
|
||||||
|
def some(self) -> None:
|
||||||
|
reveal_type(self.get_form_class()) # N: Revealed type is "Type[main.ArticleModelForm*]"
|
||||||
|
installed_apps:
|
||||||
|
- myapp
|
||||||
|
files:
|
||||||
|
- path: myapp/__init__.py
|
||||||
|
- path: myapp/models.py
|
||||||
|
content: |
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
class Article(models.Model):
|
||||||
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user