diff --git a/mypy.ini b/mypy.ini index 79c93df01..dd9ece64c 100644 --- a/mypy.ini +++ b/mypy.ini @@ -12,8 +12,7 @@ warn_unreachable = true disallow_untyped_defs = true disallow_incomplete_defs = true disable_error_code = empty-body -# TODO: update our test error messages to match new mypy output -show_error_codes = false +enable_error_code = ignore-without-code force_uppercase_builtins = true force_union_syntax = true @@ -27,6 +26,7 @@ django_settings_module = scripts.drf_tests_settings # Suppress errors from site-packages due to https://github.com/typeddjango/pytest-mypy-plugins/issues/134 [mypy-uritemplate.*] warn_unreachable = false +disable_error_code = ignore-without-code [mypy-yaml.*] disallow_untyped_defs = false diff --git a/pyproject.toml b/pyproject.toml index 7532072c1..836fc219f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,6 @@ ignore = ["PYI021", "PYI024", "PYI041", "PYI043"] "F405", "F822", "F821", - "PGH003", # TODO fix these errors ] "rest_framework-stubs/compat.pyi" = ["PYI042"] diff --git a/rest_framework-stubs/decorators.pyi b/rest_framework-stubs/decorators.pyi index 8c1b07570..099c1b163 100644 --- a/rest_framework-stubs/decorators.pyi +++ b/rest_framework-stubs/decorators.pyi @@ -46,7 +46,7 @@ else: class MethodMapper(dict): def __init__(self, action: _View, methods: Sequence[str]) -> None: ... def _map(self, method: str, func: _View) -> _View: ... - def get(self, func: _View) -> _View: ... # type: ignore + def get(self, func: _View) -> _View: ... # type: ignore[explicit-override,override] def post(self, func: _View) -> _View: ... def put(self, func: _View) -> _View: ... def patch(self, func: _View) -> _View: ... diff --git a/rest_framework-stubs/relations.pyi b/rest_framework-stubs/relations.pyi index 86aca090b..cdd9a9e81 100644 --- a/rest_framework-stubs/relations.pyi +++ b/rest_framework-stubs/relations.pyi @@ -57,7 +57,7 @@ class RelatedField(Generic[_MT, _DT, _PT], Field[_MT, _DT, _PT, Any]): style: dict[str, str] | None = ..., ) -> None: ... # mypy doesn't accept the typing below, although its accurate to what this class is doing, hence the ignore - def __new__(cls, *args: Any, **kwargs: Any) -> RelatedField[_MT, _DT, _PT] | ManyRelatedField: ... # type: ignore + def __new__(cls, *args: Any, **kwargs: Any) -> RelatedField[_MT, _DT, _PT] | ManyRelatedField: ... # type: ignore[misc] @classmethod def many_init(cls, *args: Any, **kwargs: Any) -> ManyRelatedField: ... def get_queryset(self) -> QuerySet[_MT]: ... diff --git a/rest_framework-stubs/serializers.pyi b/rest_framework-stubs/serializers.pyi index d35f9aa88..78210efaa 100644 --- a/rest_framework-stubs/serializers.pyi +++ b/rest_framework-stubs/serializers.pyi @@ -196,7 +196,7 @@ class ModelSerializer(Serializer, BaseSerializer[_MT]): instance: _MT | Sequence[_MT] | None class Meta: - model: type[_MT] # type: ignore + model: type[_MT] # type: ignore[valid-type] fields: Sequence[str] | Literal["__all__"] read_only_fields: Sequence[str] | None exclude: Sequence[str] | None diff --git a/tests/typecheck/test_decorators.yml b/tests/typecheck/test_decorators.yml index 666faf799..8b5206e3f 100644 --- a/tests/typecheck/test_decorators.yml +++ b/tests/typecheck/test_decorators.yml @@ -16,18 +16,18 @@ def view_func2(request: Request, arg: str) -> Response: ... reveal_type(view_func2) # N: Revealed type is "rest_framework.views.AsView[def (django.http.request.HttpRequest, arg: builtins.str) -> rest_framework.response.Response]" - view_func2(None, 10) # E: Argument 1 has incompatible type "None"; expected "HttpRequest" # E: Argument 2 has incompatible type "int"; expected "str" + view_func2(None, 10) # E: Argument 1 has incompatible type "None"; expected "HttpRequest" [arg-type] # E: Argument 2 has incompatible type "int"; expected "str" [arg-type] - case: api_view_bare_is_error main: | from typing import Any from rest_framework.decorators import api_view - @api_view # E: Argument 1 to "api_view" has incompatible type "Callable[[Any], Any]"; expected "Optional[Sequence[str]]" + @api_view # E: Argument 1 to "api_view" has incompatible type "Callable[[Any], Any]"; expected "Optional[Sequence[str]]" [arg-type] def view_func2(request: Any) -> Any: ... - case: api_view_incorrect_return main: | from rest_framework.decorators import api_view from rest_framework.request import Request - @api_view() # E: Value of type variable "_RESP" of function cannot be "List[Any]" + @api_view() # E: Value of type variable "_RESP" of function cannot be "List[Any]" [type-var] def view_func2(request: Request) -> list: ... - case: permission_classes diff --git a/tests/typecheck/test_exceptions.yml b/tests/typecheck/test_exceptions.yml index 6cdfc2378..0d2f2697f 100644 --- a/tests/typecheck/test_exceptions.yml +++ b/tests/typecheck/test_exceptions.yml @@ -45,6 +45,6 @@ APIException('I am just a message', code='msg') APIException() APIException(None, None) - APIException(1) # E: Argument 1 to "APIException" has incompatible type "int"; expected "_APIExceptionInput" - APIException({'a': 1}) # E: Dict entry 0 has incompatible type "str": "int"; expected "str": "Union[_StrPromise, Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]" - APIException({'a': ['test', 1]}) # E: List item 1 has incompatible type "int"; expected "Union[_StrPromise, Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]" + APIException(1) # E: Argument 1 to "APIException" has incompatible type "int"; expected "_APIExceptionInput" [arg-type] + APIException({'a': 1}) # E: Dict entry 0 has incompatible type "str": "int"; expected "str": "Union[_StrPromise, Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]" [dict-item] + APIException({'a': ['test', 1]}) # E: List item 1 has incompatible type "int"; expected "Union[_StrPromise, Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]" [list-item] diff --git a/tests/typecheck/test_fields.yml b/tests/typecheck/test_fields.yml index fae186768..288ca9895 100644 --- a/tests/typecheck/test_fields.yml +++ b/tests/typecheck/test_fields.yml @@ -1,11 +1,11 @@ - case: no_positional_args_fields main: | from rest_framework.fields import IntegerField, FloatField, UUIDField, CharField, DurationField - CharField(True) # E: Too many positional arguments for "CharField" - IntegerField(1) # E: Too many positional arguments for "IntegerField" - FloatField(1) # E: Too many positional arguments for "FloatField" - UUIDField('hex_verbose') # E: Too many positional arguments for "UUIDField" - DurationField(1) # E: Too many positional arguments for "DurationField" + CharField(True) # E: Too many positional arguments for "CharField" [misc] + IntegerField(1) # E: Too many positional arguments for "IntegerField" [misc] + FloatField(1) # E: Too many positional arguments for "FloatField" [misc] + UUIDField('hex_verbose') # E: Too many positional arguments for "UUIDField" [misc] + DurationField(1) # E: Too many positional arguments for "DurationField" [misc] - case: some_positional_args_fields main: | @@ -14,36 +14,36 @@ from rest_framework.fields import DecimalField, IPAddressField, SlugField, RegexField, ModelField, SerializerMethodField, ChoiceField, DateTimeField, DateField, TimeField DecimalField(1, 1, False, 1, 1, False, None) - DecimalField(1, 1, False, 1, 1, False, None, True) # E: Too many positional arguments for "DecimalField" + DecimalField(1, 1, False, 1, 1, False, None, True) # E: Too many positional arguments for "DecimalField" [misc] IPAddressField('both') - IPAddressField('both', True) # E: Too many positional arguments for "IPAddressField" + IPAddressField('both', True) # E: Too many positional arguments for "IPAddressField" [misc] SlugField(False) - SlugField(False, True) # E: Too many positional arguments for "SlugField" + SlugField(False, True) # E: Too many positional arguments for "SlugField" [misc] RegexField('^$') - RegexField('^$', True) # E: Too many positional arguments for "RegexField" + RegexField('^$', True) # E: Too many positional arguments for "RegexField" [misc] SerializerMethodField('bla') - SerializerMethodField('bla', True) # E: Too many positional arguments for "SerializerMethodField" + SerializerMethodField('bla', True) # E: Too many positional arguments for "SerializerMethodField" [misc] mf: models.CharField = models.CharField() ModelField(mf) - ModelField(mf, True) # E: Too many positional arguments for "ModelField" + ModelField(mf, True) # E: Too many positional arguments for "ModelField" [misc] ChoiceField([]) - ChoiceField([], False) # E: Too many positional arguments for "ChoiceField" + ChoiceField([], False) # E: Too many positional arguments for "ChoiceField" [misc] d: datetime = datetime.now() DateTimeField('', [], None, read_only=True, write_only=True, allow_null=True) - DateTimeField('', [], None, True) # E: Too many positional arguments for "DateTimeField" + DateTimeField('', [], None, True) # E: Too many positional arguments for "DateTimeField" [misc] DateField('', [], read_only=True, write_only=True, allow_null=True) - DateField('', [], True) # E: Too many positional arguments for "DateField" + DateField('', [], True) # E: Too many positional arguments for "DateField" [misc] TimeField('', [], read_only=True, write_only=True, allow_null=True) - TimeField('', [], True) # E: Too many positional arguments for "TimeField" + TimeField('', [], True) # E: Too many positional arguments for "TimeField" [misc] - case: default_and_inital_args_fields main: | @@ -51,11 +51,11 @@ from typing import Optional, Dict, Any CharField(initial='', default=lambda: '') - CharField(initial=None, default=4) # E: Argument "default" to "CharField" has incompatible type "int"; expected "Union[Union[str, _StrPromise], Callable[[], Union[str, _StrPromise]], None, _Empty]" - CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "Dict[Never, Never]"; expected "Union[str, Callable[[], str], None, _Empty]" + CharField(initial=None, default=4) # E: Argument "default" to "CharField" has incompatible type "int"; expected "Union[Union[str, _StrPromise], Callable[[], Union[str, _StrPromise]], None, _Empty]" [arg-type] + CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "Dict[Never, Never]"; expected "Union[str, Callable[[], str], None, _Empty]" [arg-type] x: Optional[str] = CharField().get_initial() - y: Optional[int] = CharField().get_initial() # E: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Optional[int]") + y: Optional[int] = CharField().get_initial() # E: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Optional[int]") [assignment] - case: float_field_args_fields main: | @@ -76,7 +76,7 @@ ChoiceField(['test'], allow_null=True, default=None) ChoiceField([1], default=int_callback) ChoiceField([1, 'lulz'], default=mixed_callback) - ChoiceField([1], default=lambda: None) # E: Argument "default" to "ChoiceField" has incompatible type "Callable[[], None]"; expected "Union[Union[str, _StrPromise], int, Callable[[], Union[Union[str, _StrPromise], int]], None, _Empty]" # E: Incompatible return value type (got "None", expected "Union[Union[str, _StrPromise], int]") + ChoiceField([1], default=lambda: None) # E: Argument "default" to "ChoiceField" has incompatible type "Callable[[], None]"; expected "Union[Union[str, _StrPromise], int, Callable[[], Union[Union[str, _StrPromise], int]], None, _Empty]" [arg-type] # E: Incompatible return value type (got "None", expected "Union[Union[str, _StrPromise], int]") [return-value] - case: MultipleChoiceField_default main: | @@ -90,13 +90,13 @@ MultipleChoiceField(choices=['test'], allow_null=True, default=None) MultipleChoiceField(choices=[1], default=int_set_callback) MultipleChoiceField(choices=[1, 'lulz'], default=mixed_set_callback) - MultipleChoiceField(choices=[1], default=lambda: [1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "Callable[[], List[int]]"; expected "Union[Set[Union[str, int]], Set[str], Set[int], Callable[[], Union[Set[Union[str, int]], Set[str], Set[int]]], None, _Empty]" # E: Incompatible return value type (got "List[int]", expected "Union[Set[Union[str, int]], Set[str], Set[int]]") + MultipleChoiceField(choices=[1], default=lambda: [1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "Callable[[], List[int]]"; expected "Union[Set[Union[str, int]], Set[str], Set[int], Callable[[], Union[Set[Union[str, int]], Set[str], Set[int]]], None, _Empty]" [arg-type] # E: Incompatible return value type (got "List[int]", expected "Union[Set[Union[str, int]], Set[str], Set[int]]") [return-value] MultipleChoiceField(choices=[(1, "1"), (2, "2")], default={1}) - MultipleChoiceField(choices=[(1, "1"), (2, "2")], default=[1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "List[int]"; expected "Union[Set[Union[str, int]], Set[str], Set[int], Callable[[], Union[Set[Union[str, int]], Set[str], Set[int]]], None, _Empty]" + MultipleChoiceField(choices=[(1, "1"), (2, "2")], default=[1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "List[int]"; expected "Union[Set[Union[str, int]], Set[str], Set[int], Callable[[], Union[Set[Union[str, int]], Set[str], Set[int]]], None, _Empty]" [arg-type] MultipleChoiceField(choices=[(1, "1"), (2, "2")], initial={1}) - MultipleChoiceField(choices=[(1, "1"), (2, "2")], initial=[1]) # E: Argument "initial" to "MultipleChoiceField" has incompatible type "List[int]"; expected "Union[Set[Union[Union[str, _StrPromise], int]], Set[Union[str, _StrPromise]], Set[int], Callable[[], Union[Set[Union[Union[str, _StrPromise], int]], Set[Union[str, _StrPromise]], Set[int]]], None, _Empty]" + MultipleChoiceField(choices=[(1, "1"), (2, "2")], initial=[1]) # E: Argument "initial" to "MultipleChoiceField" has incompatible type "List[int]"; expected "Union[Set[Union[Union[str, _StrPromise], int]], Set[Union[str, _StrPromise]], Set[int], Callable[[], Union[Set[Union[Union[str, _StrPromise], int]], Set[Union[str, _StrPromise]], Set[int]]], None, _Empty]" [arg-type] - case: FileField_default main: | @@ -108,12 +108,12 @@ FileField(allow_null=True, default=None) FileField(allow_null=True, default=file_callback) FileField(allow_null=True, default=file_callback()) - FileField(allow_null=True, default=123) # E: Argument "default" to "FileField" has incompatible type "int"; expected "Union[File[Any], Callable[[], File[Any]], None, _Empty]" + FileField(allow_null=True, default=123) # E: Argument "default" to "FileField" has incompatible type "int"; expected "Union[File[Any], Callable[[], File[Any]], None, _Empty]" [arg-type] ImageField(allow_null=True, default=None) ImageField(default=file_callback) ImageField(default=file_callback()) - ImageField(default='a') # E: Argument "default" to "ImageField" has incompatible type "str"; expected "Union[File[Any], Callable[[], File[Any]], None, _Empty]" + ImageField(default='a') # E: Argument "default" to "ImageField" has incompatible type "str"; expected "Union[File[Any], Callable[[], File[Any]], None, _Empty]" [arg-type] - case: DictField_default main: | @@ -123,13 +123,13 @@ DictField(default={}) DictField(default={'a': 1, 'b': 2}) DictField(default=lambda: {'a': [], 'b': 'str'}) - DictField(default=[]) # E: Argument "default" to "DictField" has incompatible type "List[Never]"; expected "Union[Dict[Any, Any], Callable[[], Dict[Any, Any]], None, _Empty]" + DictField(default=[]) # E: Argument "default" to "DictField" has incompatible type "List[Never]"; expected "Union[Dict[Any, Any], Callable[[], Dict[Any, Any]], None, _Empty]" [arg-type] JSONField(allow_null=True, default=None) JSONField(default={}) JSONField(default={'a': 1, 'b': 2}) JSONField(default=lambda: {'a': [], 'b': 'str'}) - JSONField(default=[]) # E: Argument "default" to "JSONField" has incompatible type "List[Never]"; expected "Union[Mapping[Any, Any], Callable[[], Mapping[Any, Any]], None, _Empty]" + JSONField(default=[]) # E: Argument "default" to "JSONField" has incompatible type "List[Never]"; expected "Union[Mapping[Any, Any], Callable[[], Mapping[Any, Any]], None, _Empty]" [arg-type] - case: ListField_default main: | @@ -139,4 +139,4 @@ ListField(default=[]) ListField(default=[0, 'one']) ListField(default=lambda: []) - ListField(default='wät') # E: Argument "default" to "ListField" has incompatible type "str"; expected "Union[List[Any], Callable[[], List[Any]], None, _Empty]" + ListField(default='wät') # E: Argument "default" to "ListField" has incompatible type "str"; expected "Union[List[Any], Callable[[], List[Any]], None, _Empty]" [arg-type] diff --git a/tests/typecheck/test_serializers.yml b/tests/typecheck/test_serializers.yml index a46725197..cc6915e9f 100644 --- a/tests/typecheck/test_serializers.yml +++ b/tests/typecheck/test_serializers.yml @@ -104,9 +104,9 @@ - case: test_return_list_serializer_argument_is_kw_only parametrized: - arg: "" - err: No overload variant of "ReturnList" matches argument type "TestSerializer" + err: No overload variant of "ReturnList" matches argument type "TestSerializer" [call-overload] - arg: "[]," - err: No overload variant of "ReturnList" matches argument types "List[Never]", "TestSerializer" + err: No overload variant of "ReturnList" matches argument types "List[Never]", "TestSerializer" [call-overload] main: | from rest_framework import serializers from rest_framework.utils.serializer_helpers import ReturnList @@ -123,9 +123,9 @@ - case: test_return_list_serializer_is_required parametrized: - arg: "" - err: All overload variants of "ReturnList" require at least one argument + err: All overload variants of "ReturnList" require at least one argument [call-overload] - arg: "[]" - err: No overload variant of "ReturnList" matches argument type "List[Never]" + err: No overload variant of "ReturnList" matches argument type "List[Never]" [call-overload] main: | from rest_framework import serializers from rest_framework.utils.serializer_helpers import ReturnList @@ -174,13 +174,13 @@ - case: test_return_dict_serializer_argument_is_kw_only parametrized: - arg: "" - err: No overload variant of "ReturnDict" matches argument type "TestSerializer" + err: No overload variant of "ReturnDict" matches argument type "TestSerializer" [call-overload] - arg: "{}," - err: No overload variant of "ReturnDict" matches argument types "Dict[Never, Never]", "TestSerializer" + err: No overload variant of "ReturnDict" matches argument types "Dict[Never, Never]", "TestSerializer" [call-overload] - arg: "[]," - err: No overload variant of "ReturnDict" matches argument types "List[Never]", "TestSerializer" + err: No overload variant of "ReturnDict" matches argument types "List[Never]", "TestSerializer" [call-overload] - arg: "[('a', 'a')]," - err: No overload variant of "ReturnDict" matches argument types "List[Tuple[str, str]]", "TestSerializer" + err: No overload variant of "ReturnDict" matches argument types "List[Tuple[str, str]]", "TestSerializer" [call-overload] main: | from rest_framework import serializers from rest_framework.utils.serializer_helpers import ReturnDict @@ -203,13 +203,13 @@ - case: test_return_dict_serializer_is_required parametrized: - arg: "" - err: All overload variants of "ReturnDict" require at least one argument + err: All overload variants of "ReturnDict" require at least one argument [call-overload] - arg: "{}" - err: No overload variant of "ReturnDict" matches argument type "Dict[Never, Never]" + err: No overload variant of "ReturnDict" matches argument type "Dict[Never, Never]" [call-overload] - arg: "[]" - err: No overload variant of "ReturnDict" matches argument type "List[Never]" + err: No overload variant of "ReturnDict" matches argument type "List[Never]" [call-overload] - arg: "[('a', 'a')]" - err: No overload variant of "ReturnDict" matches argument type "List[Tuple[str, str]]" + err: No overload variant of "ReturnDict" matches argument type "List[Tuple[str, str]]" [call-overload] main: | from rest_framework import serializers from rest_framework.utils.serializer_helpers import ReturnDict