Skip to content

Add lots of missing argument & return type hints #1204

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

Merged
merged 10 commits into from
Oct 28, 2022
Merged
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
2 changes: 1 addition & 1 deletion django-stubs/conf/global_settings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ DATABASES: Dict[str, Dict[str, Any]] = ...

# Classes used to implement DB routing behavior.
class Router(Protocol):
def allow_migrate(self, db, app_label, **hints): ...
def allow_migrate(self, db: str, app_label: str, **hints: Any) -> Optional[bool]: ...

DATABASE_ROUTERS: List[Union[str, Router]] = ...

Expand Down
18 changes: 9 additions & 9 deletions django-stubs/contrib/admin/checks.pyi
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from typing import Any, Optional, Sequence
from typing import Any, List, Optional, Sequence

from django.apps.config import AppConfig
from django.contrib.admin.options import BaseModelAdmin
from django.core.checks.messages import CheckMessage, Error

def check_admin_app(app_configs: Optional[Sequence[AppConfig]], **kwargs: Any) -> Sequence[CheckMessage]: ...
def check_dependencies(**kwargs: Any) -> Sequence[CheckMessage]: ...
def check_admin_app(app_configs: Optional[Sequence[AppConfig]], **kwargs: Any) -> List[CheckMessage]: ...
def check_dependencies(**kwargs: Any) -> List[CheckMessage]: ...

class BaseModelAdminChecks:
def check(self, admin_obj: BaseModelAdmin, **kwargs: Any) -> Sequence[CheckMessage]: ...
def check(self, admin_obj: BaseModelAdmin, **kwargs: Any) -> List[CheckMessage]: ...

class ModelAdminChecks(BaseModelAdminChecks):
def check(self, admin_obj: BaseModelAdmin, **kwargs: Any) -> Sequence[CheckMessage]: ...
def check(self, admin_obj: BaseModelAdmin, **kwargs: Any) -> List[CheckMessage]: ...

class InlineModelAdminChecks(BaseModelAdminChecks):
def check(self, inline_obj: BaseModelAdmin, **kwargs: Any) -> Sequence[CheckMessage]: ... # type: ignore
def check(self, inline_obj: BaseModelAdmin, **kwargs: Any) -> List[CheckMessage]: ... # type: ignore

def must_be(type: Any, option: Any, obj: Any, id: Any): ...
def must_inherit_from(parent: Any, option: Any, obj: Any, id: Any): ...
def refer_to_missing_field(field: Any, option: Any, obj: Any, id: Any): ...
def must_be(type: Any, option: Any, obj: Any, id: Any) -> List[CheckMessage]: ...
def must_inherit_from(parent: Any, option: Any, obj: Any, id: Any) -> List[CheckMessage]: ...
def refer_to_missing_field(field: Any, option: Any, obj: Any, id: Any) -> List[CheckMessage]: ...
4 changes: 2 additions & 2 deletions django-stubs/contrib/admin/helpers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from django import forms
from django.contrib.admin.options import ModelAdmin
from django.db.models import Model
from django.db.models.fields import AutoField
from django.forms import BaseForm
from django.forms.boundfield import BoundField
from django.forms.models import ModelForm
from django.forms.utils import ErrorDict, ErrorList
Expand Down Expand Up @@ -143,7 +144,7 @@ class InlineAdminFormSet:
def fields(self) -> Iterator[Dict[str, Union[Dict[str, bool], bool, Widget, str]]]: ...
def inline_formset_data(self) -> str: ...
@property
def forms(self): ...
def forms(self) -> List[BaseForm]: ...
@property
def non_form_errors(self) -> Callable[[], ErrorList]: ...
@property
Expand All @@ -170,7 +171,6 @@ class InlineAdminForm(AdminForm):
def pk_field(self) -> AdminField: ...
def fk_field(self) -> AdminField: ...
def deletion_field(self) -> AdminField: ...
def ordering_field(self): ...

class InlineFieldset(Fieldset):
formset: Any = ...
Expand Down
14 changes: 11 additions & 3 deletions django-stubs/contrib/admin/options.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ from django.forms.fields import Field as FormField
from django.forms.fields import TypedChoiceField
from django.forms.forms import BaseForm
from django.forms.formsets import BaseFormSet
from django.forms.models import BaseInlineFormSet, ModelChoiceField, ModelMultipleChoiceField
from django.forms.models import (
BaseInlineFormSet,
BaseModelFormSet,
ModelChoiceField,
ModelForm,
ModelMultipleChoiceField,
)
from django.forms.widgets import Media
from django.http.request import HttpRequest
from django.http.response import HttpResponse, HttpResponseRedirect, JsonResponse
Expand Down Expand Up @@ -191,8 +197,10 @@ class ModelAdmin(BaseModelAdmin[_ModelT]):
def get_object(
self, request: HttpRequest, object_id: str, from_field: Optional[str] = ...
) -> Optional[_ModelT]: ...
def get_changelist_form(self, request: Any, **kwargs: Any): ...
def get_changelist_formset(self, request: Any, **kwargs: Any): ...
def get_changelist_form(self, request: Any, **kwargs: Any) -> Type[ModelForm[_ModelT]]: ...
def get_changelist_formset(
self, request: Any, **kwargs: Any
) -> Type[BaseModelFormSet[_ModelT, ModelForm[_ModelT]]]: ...
def get_formsets_with_inlines(self, request: HttpRequest, obj: Optional[_ModelT] = ...) -> Iterator[Any]: ...
def get_paginator(
self,
Expand Down
8 changes: 6 additions & 2 deletions django-stubs/contrib/admin/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from django.forms.forms import BaseForm
from django.forms.formsets import BaseFormSet
from django.http.request import HttpRequest
from django.utils.datastructures import _IndexableCollection
from typing_extensions import Literal
from typing_extensions import Literal, TypedDict

class FieldIsAForeignKeyColumnName(Exception): ...

Expand Down Expand Up @@ -45,7 +45,11 @@ class NestedObjects(Collector):
def nested(self, format_callback: Callable = ...) -> List[Any]: ...
def can_fast_delete(self, *args: Any, **kwargs: Any) -> bool: ...

def model_format_dict(obj: Union[Model, Type[Model], QuerySet, Options[Model]]): ...
class ModelFormatDict(TypedDict):
Copy link
Member

Choose a reason for hiding this comment

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

I think that this does not exist in django, so it is _ModelFormatDict

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ooops, sorry! I pushed this fix to the wrong branch. Will be fixed in #1206.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, no problem!

verbose_name: str
verbose_name_plural: str

def model_format_dict(obj: Union[Model, Type[Model], QuerySet, Options[Model]]) -> ModelFormatDict: ...
def model_ngettext(obj: Union[Options, QuerySet], n: Optional[int] = ...) -> str: ...
def lookup_field(
name: Union[Callable, str], obj: Model, model_admin: Optional[BaseModelAdmin] = ...
Expand Down
8 changes: 5 additions & 3 deletions django-stubs/contrib/admindocs/utils.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Any, Callable, Dict, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Tuple

from django.utils.safestring import SafeString

docutils_is_available: bool

def get_view_name(view_func: Callable) -> str: ...
def parse_docstring(docstring: str) -> Tuple[str, str, Dict[str, str]]: ...
def parse_rst(text: str, default_reference_context: Any, thing_being_parsed: Optional[Any] = ...): ...
def parse_rst(text: str, default_reference_context: Any, thing_being_parsed: Optional[Any] = ...) -> SafeString: ...

ROLES: Dict[str, str]

Expand All @@ -17,7 +19,7 @@ def default_reference_role(
inliner: Any,
options: Optional[Any] = ...,
content: Optional[Any] = ...,
): ...
) -> Tuple[List[Any], List[Any]]: ...

named_group_matcher: Any
unnamed_group_matcher: Any
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/contrib/admindocs/views.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ModelIndexView(BaseAdminDocsView): ...
class ModelDetailView(BaseAdminDocsView): ...
class TemplateDetailView(BaseAdminDocsView): ...

def get_return_data_type(func_name: Any): ...
def get_return_data_type(func_name: Any) -> str: ...
def get_readable_field_data_type(field: Union[Field, str]) -> str: ...
def extract_views_from_urlpatterns(
urlpatterns: Iterable[_AnyURL], base: str = ..., namespace: Optional[str] = ...
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/contrib/auth/backends.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Any, Optional, Set, TypeVar, Union

from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import AnonymousUser, Permission
from django.db.models import QuerySet
from django.db.models.base import Model
from django.http.request import HttpRequest

Expand Down Expand Up @@ -33,7 +34,7 @@ class ModelBackend(BaseBackend):
is_active: bool = ...,
include_superusers: bool = ...,
obj: Optional[Model] = ...,
): ...
) -> QuerySet[AbstractBaseUser]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be with _AnyUser?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Won't work

django-stubs/contrib/auth/backends.pyi:37: error: Type argument "Union[AbstractBaseUser, AnonymousUser]" of "_QuerySet" must be a subtype of "Model"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This method does a DB query, so it only returns user models -- it can't return AnonymousUser.

Similar to how authenticate() returns AbstractBaseUser.


class AllowAllUsersModelBackend(ModelBackend): ...

Expand Down
3 changes: 2 additions & 1 deletion django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from django.contrib.auth.base_user import BaseUserManager as BaseUserManager
from django.contrib.auth.validators import UnicodeUsernameValidator
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import QuerySet
from django.db.models.base import Model
from django.db.models.manager import EmptyManager
from typing_extensions import Literal
Expand Down Expand Up @@ -51,7 +52,7 @@ class UserManager(BaseUserManager[_T]):
include_superusers: bool = ...,
backend: Optional[str] = ...,
obj: Optional[Model] = ...,
): ...
) -> QuerySet[_T]: ...

class PermissionsMixin(models.Model):
is_superuser = models.BooleanField()
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/contrib/auth/views.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional, Set
from typing import Any, Dict, Optional, Set

from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.forms import AuthenticationForm
Expand Down Expand Up @@ -35,7 +35,7 @@ def redirect_to_login(

class PasswordContextMixin:
extra_context: Any = ...
def get_context_data(self, **kwargs: Any): ...
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: ...

class PasswordResetView(PasswordContextMixin, FormView):
email_template_name: str = ...
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/contrib/contenttypes/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ class GenericRelation(ForeignObject):

class ReverseGenericManyToOneDescriptor(ReverseManyToOneDescriptor): ...

def create_generic_related_manager(superclass: Any, rel: Any): ...
def create_generic_related_manager(superclass: Any, rel: Any) -> Type[Any]: ... # GenericRelatedObjectManager
22 changes: 13 additions & 9 deletions django-stubs/contrib/contenttypes/forms.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import Any, Optional
from typing import Any, Optional, Type, TypeVar

from django.forms.models import BaseModelFormSet
from django.db.models import Model
from django.forms.models import BaseModelFormSet, ModelForm

class BaseGenericInlineFormSet(BaseModelFormSet):
_M = TypeVar("_M", bound=Model)
_ModelFormT = TypeVar("_ModelFormT", bound=ModelForm)

class BaseGenericInlineFormSet(BaseModelFormSet[_M, _ModelFormT]):
Copy link
Member

Choose a reason for hiding this comment

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

Should we add it to our monkeypatching? Since it is generic now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

BaseModelFormSet doesn't seem to be patched either.

instance: Any = ...
rel_name: Any = ...
save_as_new: Any = ...
Expand All @@ -16,14 +20,14 @@ class BaseGenericInlineFormSet(BaseModelFormSet):
queryset: Optional[Any] = ...,
**kwargs: Any
) -> None: ...
def initial_form_count(self): ...
def initial_form_count(self) -> int: ...
@classmethod
def get_default_prefix(cls): ...
def save_new(self, form: Any, commit: bool = ...): ...
def get_default_prefix(cls) -> str: ...
def save_new(self, form: Any, commit: bool = ...) -> _M: ...

def generic_inlineformset_factory(
model: Any,
form: Any = ...,
model: Type[_M],
form: Type[_ModelFormT] = ...,
formset: Any = ...,
ct_field: str = ...,
fk_field: str = ...,
Expand All @@ -40,4 +44,4 @@ def generic_inlineformset_factory(
validate_min: bool = ...,
absolute_max: Optional[int] = ...,
can_delete_extra: bool = ...,
): ...
) -> Type[BaseGenericInlineFormSet[_M, _ModelFormT]]: ...
24 changes: 12 additions & 12 deletions django-stubs/contrib/gis/db/backends/base/features.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ class BaseSpatialFeatures:
supports_raster: bool = ...
supports_geometry_field_unique_index: bool = ...
@property
def supports_bbcontains_lookup(self): ...
def supports_bbcontains_lookup(self) -> bool: ...
@property
def supports_contained_lookup(self): ...
def supports_contained_lookup(self) -> bool: ...
@property
def supports_crosses_lookup(self): ...
def supports_crosses_lookup(self) -> bool: ...
@property
def supports_distances_lookups(self): ...
def supports_distances_lookups(self) -> bool: ...
@property
def supports_dwithin_lookup(self): ...
def supports_dwithin_lookup(self) -> bool: ...
@property
def supports_relate_lookup(self): ...
def supports_relate_lookup(self) -> bool: ...
@property
def supports_isvalid_lookup(self): ...
def supports_isvalid_lookup(self) -> bool: ...
@property
def supports_collect_aggr(self): ...
def supports_collect_aggr(self) -> bool: ...
@property
def supports_extent_aggr(self): ...
def supports_extent_aggr(self) -> bool: ...
@property
def supports_make_line_aggr(self): ...
def supports_make_line_aggr(self) -> bool: ...
@property
def supports_union_aggr(self): ...
def __getattr__(self, name: Any): ...
def supports_union_aggr(self) -> bool: ...
def __getattr__(self, name: Any) -> bool: ...
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ from django.db.backends.sqlite3.features import DatabaseFeatures as SQLiteDataba
class DatabaseFeatures(BaseSpatialFeatures, SQLiteDatabaseFeatures):
supports_3d_storage: bool = ...
@property
def supports_area_geodetic(self): ...
def supports_area_geodetic(self) -> bool: ... # type: ignore[override]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Set

from django.contrib.gis.db.backends.base.operations import BaseSpatialOperations
from django.contrib.gis.db.backends.utils import SpatialOperator as SpatialOperator
Expand All @@ -19,7 +19,7 @@ class SpatiaLiteOperations(BaseSpatialOperations, DatabaseOperations):
select: str = ...
function_names: Any = ...
@property
def unsupported_functions(self): ...
def unsupported_functions(self) -> Set[str]: ... # type: ignore[override]
@property
def spatial_version(self): ...
def geo_db_type(self, f: Any) -> None: ...
Expand Down
6 changes: 3 additions & 3 deletions django-stubs/contrib/messages/storage/base.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List, Optional
from typing import Any, Iterator, List, Optional

from django.http.request import HttpRequest
from django.http.response import HttpResponseBase
Expand All @@ -21,8 +21,8 @@ class BaseStorage:
added_new: bool = ...
def __init__(self, request: HttpRequest, *args: Any, **kwargs: Any) -> None: ...
def __len__(self) -> int: ...
def __iter__(self): ...
def __contains__(self, item: Any): ...
def __iter__(self) -> Iterator[Message]: ...
def __contains__(self, item: Any) -> bool: ...
def update(self, response: HttpResponseBase) -> Optional[List[Message]]: ...
def add(self, level: int, message: str, extra_tags: Optional[str] = ...) -> None: ...
level: Any = ...
6 changes: 3 additions & 3 deletions django-stubs/contrib/postgres/fields/hstore.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ from django.db.models import Field, Transform
from django.db.models.fields.mixins import CheckFieldDefaultMixin

class HStoreField(CheckFieldDefaultMixin, Field):
def get_transform(self, name) -> Any: ...
def get_transform(self, name: str) -> Any: ...

class KeyTransform(Transform):
def __init__(self, key_name: str, *args: Any, **kwargs: Any): ...
def __init__(self, key_name: str, *args: Any, **kwargs: Any) -> None: ...

class KeyTransformFactory:
def __init__(self, key_name: str): ...
def __init__(self, key_name: str) -> None: ...
def __call__(self, *args, **kwargs) -> KeyTransform: ...

class KeysTransform(Transform): ...
Expand Down
4 changes: 3 additions & 1 deletion django-stubs/contrib/postgres/serializers.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Set, Tuple

from django.db.migrations.serializer import BaseSerializer as BaseSerializer

class RangeSerializer(BaseSerializer):
def serialize(self): ...
def serialize(self) -> Tuple[str, Set[str]]: ...
4 changes: 3 additions & 1 deletion django-stubs/contrib/postgres/utils.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

def prefix_validation_error(error: Any, prefix: Any, code: Any, params: Any): ...
from django.core.exceptions import ValidationError

def prefix_validation_error(error: Any, prefix: Any, code: Any, params: Any) -> ValidationError: ...
4 changes: 2 additions & 2 deletions django-stubs/core/cache/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Type
from typing import Any, List, Type

from django.utils.connection import BaseConnectionHandler, ConnectionProxy

Expand All @@ -12,7 +12,7 @@ class CacheHandler(BaseConnectionHandler):
settings_name: str = ...
exception_class: Type[Exception] = ...
def create_connection(self, alias: str) -> BaseCache: ...
def all(self, initialized_only: bool = ...): ...
def all(self, initialized_only: bool = ...) -> List[BaseCache]: ...

def close_caches(**kwargs: Any) -> None: ...

Expand Down
15 changes: 12 additions & 3 deletions django-stubs/core/cache/backends/memcached.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
from types import ModuleType
from typing import Any, Dict, Sequence, Type, Union

from django.core.cache.backends.base import BaseCache

class BaseMemcachedCache(BaseCache):
def __init__(self, server, params, library, value_not_found_exception) -> None: ...
def __init__(
self,
server: Union[str, Sequence[str]],
params: Dict[str, Any],
library: ModuleType,
value_not_found_exception: Type[BaseException],
) -> None: ...

class MemcachedCache(BaseMemcachedCache):
def __init__(self, server, params): ...
def __init__(self, server: Union[str, Sequence[str]], params: Dict[str, Any]) -> None: ...

class PyLibMCCache(BaseMemcachedCache):
def __init__(self, server, params): ...
def __init__(self, server: Union[str, Sequence[str]], params: Dict[str, Any]) -> None: ...
2 changes: 1 addition & 1 deletion django-stubs/core/management/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ManagementUtility:
prog_name: str = ...
settings_exception: Optional[Exception] = ...
def __init__(self, argv: Optional[List[str]] = ...) -> None: ...
def main_help_text(self, commands_only: bool = ...): ...
def main_help_text(self, commands_only: bool = ...) -> str: ...
def fetch_command(self, subcommand: str) -> BaseCommand: ...
def autocomplete(self) -> None: ...
def execute(self) -> None: ...
Expand Down
Loading