Skip to content

Commit

Permalink
Harmonize mypy.ini config with django-stubs (#479)
Browse files Browse the repository at this point in the history
* Most of the overrides in `tests/plugins.ini` are no longer needed
* Removed lots of unneeded `# type: ignore` comments
* Will create a separate PR to update `disallow_untyped_defs` and `disallow_incomplete_defs` too
  • Loading branch information
intgr authored Oct 5, 2023
1 parent fc62c8e commit adf6975
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 49 deletions.
18 changes: 14 additions & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
[mypy]
strict_optional = true
ignore_missing_imports = true
check_untyped_defs = true
show_traceback = true
allow_redefinition = true
check_untyped_defs = true
ignore_missing_imports = true
incremental = true
strict_optional = true
show_traceback = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unreachable = true
# TODO: enable disallow_*_defs later
disallow_untyped_defs = false
disallow_incomplete_defs = false
disable_error_code = empty-body
# TODO: update our test error messages to match new mypy output
show_error_codes = false
Expand All @@ -17,3 +24,6 @@ plugins =

[mypy.plugins.django-stubs]
django_settings_module = scripts.drf_tests_settings

[mypy-uritemplate.*]
warn_unreachable = false
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ addopts =
-s
-v
--cache-clear
--mypy-ini-file=./tests/plugins.ini
--mypy-ini-file=mypy.ini
--mypy-extension-hook=scripts.tests_extension_hook.django_plugin_hook
10 changes: 5 additions & 5 deletions rest_framework-stubs/compat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ except ImportError:
try:
import coreapi
except ImportError:
coreapi = None # type: ignore
coreapi = None
try:
import uritemplate
except ImportError:
uritemplate = None # type: ignore
try:
import coreschema
except ImportError:
coreschema = None # type: ignore
coreschema = None
try:
import yaml
except ImportError:
Expand All @@ -30,17 +30,17 @@ except ImportError:
try:
import pygments
except ImportError:
pygments = None # type: ignore
pygments = None
try:
import markdown # type: ignore
import markdown
def apply_markdown(text: str): ...

except ImportError:
apply_markdown = None # type: ignore
markdown = None # type: ignore

if markdown is not None and pygments is not None:
from markdown.preprocessors import Preprocessor # type: ignore
from markdown.preprocessors import Preprocessor

class CodeBlockPreprocessor(Preprocessor):
pattern: Any
Expand Down
4 changes: 2 additions & 2 deletions rest_framework-stubs/permissions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class OR(_SupportsHasPermission):
class NOT(_SupportsHasPermission):
def __init__(self, op1: _SupportsHasPermission) -> None: ...

class BasePermissionMetaclass(OperationHolderMixin, type): ... # type: ignore[misc]
class BasePermissionMetaclass(OperationHolderMixin, type): ... # type: ignore[misc,unused-ignore]

class BasePermission(metaclass=BasePermissionMetaclass): # type: ignore[misc]
class BasePermission(metaclass=BasePermissionMetaclass):
def has_permission(self, request: Request, view: APIView) -> bool: ...
def has_object_permission(self, request: Request, view: APIView, obj: Any) -> bool: ...

Expand Down
4 changes: 2 additions & 2 deletions rest_framework-stubs/relations.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class RelatedField(Generic[_MT, _DT, _PT], Field[_MT, _DT, _PT, Any]):
@property
def grouped_choices(self) -> dict: ...
def iter_options(self) -> Iterable[Option]: ...
def get_attribute(self, instance: _MT) -> _PT | None: ... # type: ignore[override]
def get_attribute(self, instance: _MT) -> _PT | None: ...
def display_value(self, instance: _MT) -> str: ...

class StringRelatedField(RelatedField[_MT, _MT, str]): ...
Expand Down Expand Up @@ -180,7 +180,7 @@ class ManyRelatedField(Field[Sequence[Any], Sequence[Any], list[Any], Any]):
allow_empty: bool = ...,
child_relation: RelatedField = ...,
): ...
def get_value(self, dictionary: Mapping[Any, Any]) -> list[Any]: ... # type: ignore[override]
def get_value(self, dictionary: Mapping[Any, Any]) -> list[Any]: ...
def get_choices(self, cutoff: int | None = ...) -> dict: ...
@property
def choices(self) -> dict: ...
Expand Down
4 changes: 2 additions & 2 deletions rest_framework-stubs/request.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class Request(HttpRequest):
def query_params(self) -> _ImmutableQueryDict: ...
@property
def data(self) -> dict[str, Any]: ...
@property # type: ignore[override]
def user(self) -> AbstractBaseUser | AnonymousUser: ... # type: ignore[override]
@property
def user(self) -> AbstractBaseUser | AnonymousUser: ...
@user.setter
def user(self, value: AbstractBaseUser | AnonymousUser) -> None: ...
@property
Expand Down
14 changes: 7 additions & 7 deletions rest_framework-stubs/serializers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class BaseSerializer(Generic[_IN], Field[Any, Any, Any, _IN]):
def update(self, instance: _IN, validated_data: Any) -> _IN: ...
def create(self, validated_data: Any) -> _IN: ...
def save(self, **kwargs: Any) -> _IN: ...
def to_representation(self, instance: _IN) -> Any: ... # type: ignore[override]
def to_representation(self, instance: _IN) -> Any: ...

class SerializerMetaclass(type):
def __new__(cls, name: Any, bases: Any, attrs: Any): ...
Expand Down Expand Up @@ -189,15 +189,15 @@ class ModelSerializer(Serializer, BaseSerializer[_MT]):
serializer_url_field: type[RelatedField]
serializer_choice_field: type[Field]
url_field_name: str | None
instance: _MT | Sequence[_MT] | None # type: ignore[override]
instance: _MT | Sequence[_MT] | None

class Meta:
model: type[_MT] # type: ignore
fields: Sequence[str] | Literal["__all__"]
read_only_fields: Sequence[str] | None
exclude: Sequence[str] | None
depth: int | None
extra_kwargs: dict[str, dict[str, Any]] # type: ignore[override]
extra_kwargs: dict[str, dict[str, Any]]
def __init__(
self,
instance: None | _MT | Sequence[_MT] | QuerySet[_MT] | Manager[_MT] = ...,
Expand All @@ -219,10 +219,10 @@ class ModelSerializer(Serializer, BaseSerializer[_MT]):
allow_null: bool = ...,
allow_empty: bool = ...,
): ...
def update(self, instance: _MT, validated_data: Any) -> _MT: ... # type: ignore[override]
def create(self, validated_data: Any) -> _MT: ... # type: ignore[override]
def save(self, **kwargs: Any) -> _MT: ... # type: ignore[override]
def to_representation(self, instance: _MT) -> Any: ... # type: ignore[override]
def update(self, instance: _MT, validated_data: Any) -> _MT: ...
def create(self, validated_data: Any) -> _MT: ...
def save(self, **kwargs: Any) -> _MT: ...
def to_representation(self, instance: _MT) -> Any: ...
def get_field_names(self, declared_fields: Mapping[str, Field], info: FieldInfo) -> list[str]: ...
def get_default_field_names(self, declared_fields: Mapping[str, Field], model_info: FieldInfo) -> list[str]: ...
def build_field(
Expand Down
2 changes: 1 addition & 1 deletion rest_framework-stubs/views.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ class APIView(View):
def finalize_response(self, request: Request, response: Response, *args: Any, **kwargs: Any) -> Response: ...
def handle_exception(self, exc: Exception) -> Response: ...
def raise_uncaught_exception(self, exc: Exception) -> NoReturn: ...
def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponseBase: ... # type: ignore[override]
def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponseBase: ...
def options(self, request: Request, *args: Any, **kwargs: Any): ... # type: ignore[override]
25 changes: 0 additions & 25 deletions tests/plugins.ini

This file was deleted.

0 comments on commit adf6975

Please sign in to comment.