Skip to content

Harmonize mypy.ini config with django-stubs #479

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 2 commits into from
Oct 5, 2023
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
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
Comment on lines +12 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will be handled in #480

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]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gives different mypy errors depending on Python version, hence ignoring 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.