Skip to content

Commit 17169be

Browse files
committed
Use explicit mypy error codes in ignore comments and output
1 parent 4216b7e commit 17169be

File tree

9 files changed

+50
-50
lines changed

9 files changed

+50
-50
lines changed

mypy.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ warn_unreachable = true
1212
disallow_untyped_defs = true
1313
disallow_incomplete_defs = true
1414
disable_error_code = empty-body
15+
enable_error_code = ignore-without-code
1516
# TODO: update our test error messages to match new mypy output
16-
show_error_codes = false
1717
force_uppercase_builtins = true
1818
force_union_syntax = true
1919

@@ -27,6 +27,7 @@ django_settings_module = scripts.drf_tests_settings
2727
# Suppress errors from site-packages due to https://github.com/typeddjango/pytest-mypy-plugins/issues/134
2828
[mypy-uritemplate.*]
2929
warn_unreachable = false
30+
disable_error_code = ignore-without-code
3031

3132
[mypy-yaml.*]
3233
disallow_untyped_defs = false

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ ignore = ["PYI021", "PYI024", "PYI041", "PYI043"]
3939
"F405",
4040
"F822",
4141
"F821",
42-
"PGH003", # TODO fix these errors
4342
]
4443
"rest_framework-stubs/compat.pyi" = ["PYI042"]
4544

rest_framework-stubs/decorators.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ _RESP = TypeVar("_RESP", bound=HttpResponseBase)
2020
class MethodMapper(dict):
2121
def __init__(self, action: _View, methods: Sequence[str]) -> None: ...
2222
def _map(self, method: str, func: _View) -> _View: ...
23-
def get(self, func: _View) -> _View: ... # type: ignore
23+
def get(self, func: _View) -> _View: ... # type: ignore[explicit-override,override]
2424
def post(self, func: _View) -> _View: ...
2525
def put(self, func: _View) -> _View: ...
2626
def patch(self, func: _View) -> _View: ...

rest_framework-stubs/relations.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class RelatedField(Generic[_MT, _DT, _PT], Field[_MT, _DT, _PT, Any]):
5757
style: dict[str, str] | None = ...,
5858
) -> None: ...
5959
# mypy doesn't accept the typing below, although its accurate to what this class is doing, hence the ignore
60-
def __new__(cls, *args: Any, **kwargs: Any) -> RelatedField[_MT, _DT, _PT] | ManyRelatedField: ... # type: ignore
60+
def __new__(cls, *args: Any, **kwargs: Any) -> RelatedField[_MT, _DT, _PT] | ManyRelatedField: ... # type: ignore[misc]
6161
@classmethod
6262
def many_init(cls, *args: Any, **kwargs: Any) -> ManyRelatedField: ...
6363
def get_queryset(self) -> QuerySet[_MT]: ...

rest_framework-stubs/serializers.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class ModelSerializer(Serializer, BaseSerializer[_MT]):
194194
instance: _MT | Sequence[_MT] | None
195195

196196
class Meta:
197-
model: type[_MT] # type: ignore
197+
model: type[_MT] # type: ignore[valid-type]
198198
fields: Sequence[str] | Literal["__all__"]
199199
read_only_fields: Sequence[str] | None
200200
exclude: Sequence[str] | None

tests/typecheck/test_decorators.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
def view_func2(request: Request, arg: str) -> Response: ...
1717
reveal_type(view_func2) # N: Revealed type is "rest_framework.views.AsView[def (django.http.request.HttpRequest, arg: builtins.str) -> rest_framework.response.Response]"
1818
19-
view_func2(None, 10) # E: Argument 1 has incompatible type "None"; expected "HttpRequest" # E: Argument 2 has incompatible type "int"; expected "str"
19+
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]
2020
- case: api_view_bare_is_error
2121
main: |
2222
from typing import Any
2323
from rest_framework.decorators import api_view
24-
@api_view # E: Argument 1 to "api_view" has incompatible type "Callable[[Any], Any]"; expected "Optional[Sequence[str]]"
24+
@api_view # E: Argument 1 to "api_view" has incompatible type "Callable[[Any], Any]"; expected "Optional[Sequence[str]]" [arg-type]
2525
def view_func2(request: Any) -> Any: ...
2626
- case: api_view_incorrect_return
2727
main: |
2828
from rest_framework.decorators import api_view
2929
from rest_framework.request import Request
30-
@api_view() # E: Value of type variable "_RESP" of function cannot be "List[Any]"
30+
@api_view() # E: Value of type variable "_RESP" of function cannot be "List[Any]" [type-var]
3131
def view_func2(request: Request) -> list: ...
3232
3333
- case: permission_classes

tests/typecheck/test_exceptions.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@
4545
APIException('I am just a message', code='msg')
4646
APIException()
4747
APIException(None, None)
48-
APIException(...) # E: Argument 1 to "APIException" has incompatible type "ellipsis"; expected "_APIExceptionInput"
49-
APIException({'a': ...}) # E: Dict entry 0 has incompatible type "str": "ellipsis"; expected "str": "Union[Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]"
50-
APIException({'a': ['test', ...]}) # E: List item 1 has incompatible type "ellipsis"; expected "Union[Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]"
48+
APIException(...) # E: Argument 1 to "APIException" has incompatible type "ellipsis"; expected "_APIExceptionInput" [arg-type]
49+
APIException({'a': ...}) # E: Dict entry 0 has incompatible type "str": "ellipsis"; expected "str": "Union[Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]" [dict-item]
50+
APIException({'a': ['test', ...]}) # E: List item 1 has incompatible type "ellipsis"; expected "Union[Sequence[_APIExceptionInput], Mapping[str, _APIExceptionInput], None]" [list-item]

tests/typecheck/test_fields.yml

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
- case: no_positional_args_fields
22
main: |
33
from rest_framework.fields import IntegerField, FloatField, UUIDField, CharField, DurationField
4-
CharField(True) # E: Too many positional arguments for "CharField"
5-
IntegerField(1) # E: Too many positional arguments for "IntegerField"
6-
FloatField(1) # E: Too many positional arguments for "FloatField"
7-
UUIDField('hex_verbose') # E: Too many positional arguments for "UUIDField"
8-
DurationField(1) # E: Too many positional arguments for "DurationField"
4+
CharField(True) # E: Too many positional arguments for "CharField" [misc]
5+
IntegerField(1) # E: Too many positional arguments for "IntegerField" [misc]
6+
FloatField(1) # E: Too many positional arguments for "FloatField" [misc]
7+
UUIDField('hex_verbose') # E: Too many positional arguments for "UUIDField" [misc]
8+
DurationField(1) # E: Too many positional arguments for "DurationField" [misc]
99
1010
- case: some_positional_args_fields
1111
main: |
@@ -14,48 +14,48 @@
1414
from rest_framework.fields import DecimalField, IPAddressField, SlugField, RegexField, ModelField, SerializerMethodField, ChoiceField, DateTimeField, DateField, TimeField
1515
1616
DecimalField(1, 1, False, 1, 1, False, None)
17-
DecimalField(1, 1, False, 1, 1, False, None, True) # E: Too many positional arguments for "DecimalField"
17+
DecimalField(1, 1, False, 1, 1, False, None, True) # E: Too many positional arguments for "DecimalField" [misc]
1818
1919
IPAddressField('both')
20-
IPAddressField('both', True) # E: Too many positional arguments for "IPAddressField"
20+
IPAddressField('both', True) # E: Too many positional arguments for "IPAddressField" [misc]
2121
2222
SlugField(False)
23-
SlugField(False, True) # E: Too many positional arguments for "SlugField"
23+
SlugField(False, True) # E: Too many positional arguments for "SlugField" [misc]
2424
2525
RegexField('^$')
26-
RegexField('^$', True) # E: Too many positional arguments for "RegexField"
26+
RegexField('^$', True) # E: Too many positional arguments for "RegexField" [misc]
2727
2828
SerializerMethodField('bla')
29-
SerializerMethodField('bla', True) # E: Too many positional arguments for "SerializerMethodField"
29+
SerializerMethodField('bla', True) # E: Too many positional arguments for "SerializerMethodField" [misc]
3030
3131
mf: models.CharField = models.CharField()
3232
ModelField(mf)
33-
ModelField(mf, True) # E: Too many positional arguments for "ModelField"
33+
ModelField(mf, True) # E: Too many positional arguments for "ModelField" [misc]
3434
3535
ChoiceField([])
36-
ChoiceField([], False) # E: Too many positional arguments for "ChoiceField"
36+
ChoiceField([], False) # E: Too many positional arguments for "ChoiceField" [misc]
3737
3838
d: datetime = datetime.now()
3939
DateTimeField('', [], None, read_only=True, write_only=True, allow_null=True)
40-
DateTimeField('', [], None, True) # E: Too many positional arguments for "DateTimeField"
40+
DateTimeField('', [], None, True) # E: Too many positional arguments for "DateTimeField" [misc]
4141
4242
DateField('', [], read_only=True, write_only=True, allow_null=True)
43-
DateField('', [], True) # E: Too many positional arguments for "DateField"
43+
DateField('', [], True) # E: Too many positional arguments for "DateField" [misc]
4444
4545
TimeField('', [], read_only=True, write_only=True, allow_null=True)
46-
TimeField('', [], True) # E: Too many positional arguments for "TimeField"
46+
TimeField('', [], True) # E: Too many positional arguments for "TimeField" [misc]
4747
4848
- case: default_and_inital_args_fields
4949
main: |
5050
from rest_framework.fields import DictField, CharField, empty
5151
from typing import Optional, Dict, Any
5252
5353
CharField(initial='', default=lambda: '')
54-
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]"
55-
CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "Dict[<nothing>, <nothing>]"; expected "Union[str, Callable[[], str], None, _Empty]"
54+
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]
55+
CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "Dict[<nothing>, <nothing>]"; expected "Union[str, Callable[[], str], None, _Empty]" [arg-type]
5656
5757
x: Optional[str] = CharField().get_initial()
58-
y: Optional[int] = CharField().get_initial() # E: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Optional[int]")
58+
y: Optional[int] = CharField().get_initial() # E: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Optional[int]") [assignment]
5959
6060
- case: float_field_args_fields
6161
main: |
@@ -76,7 +76,7 @@
7676
ChoiceField(['test'], allow_null=True, default=None)
7777
ChoiceField([1], default=int_callback)
7878
ChoiceField([1, 'lulz'], default=mixed_callback)
79-
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]")
79+
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]") [arg-type]
8080
8181
- case: MultipleChoiceField_default
8282
main: |
@@ -90,13 +90,13 @@
9090
MultipleChoiceField(choices=['test'], allow_null=True, default=None)
9191
MultipleChoiceField(choices=[1], default=int_set_callback)
9292
MultipleChoiceField(choices=[1, 'lulz'], default=mixed_set_callback)
93-
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]]")
93+
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]]") [arg-type]
9494
9595
MultipleChoiceField(choices=[(1, "1"), (2, "2")], default={1})
96-
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]"
96+
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]
9797
9898
MultipleChoiceField(choices=[(1, "1"), (2, "2")], initial={1})
99-
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]"
99+
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]
100100
101101
- case: FileField_default
102102
main: |
@@ -108,12 +108,12 @@
108108
FileField(allow_null=True, default=None)
109109
FileField(allow_null=True, default=file_callback)
110110
FileField(allow_null=True, default=file_callback())
111-
FileField(allow_null=True, default=123) # E: Argument "default" to "FileField" has incompatible type "int"; expected "Union[File[Any], Callable[[], File[Any]], None, _Empty]"
111+
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]
112112
113113
ImageField(allow_null=True, default=None)
114114
ImageField(default=file_callback)
115115
ImageField(default=file_callback())
116-
ImageField(default='a') # E: Argument "default" to "ImageField" has incompatible type "str"; expected "Union[File[Any], Callable[[], File[Any]], None, _Empty]"
116+
ImageField(default='a') # E: Argument "default" to "ImageField" has incompatible type "str"; expected "Union[File[Any], Callable[[], File[Any]], None, _Empty]" [arg-type]
117117
118118
- case: DictField_default
119119
main: |
@@ -123,13 +123,13 @@
123123
DictField(default={})
124124
DictField(default={'a': 1, 'b': 2})
125125
DictField(default=lambda: {'a': [], 'b': 'str'})
126-
DictField(default=[]) # E: Argument "default" to "DictField" has incompatible type "List[<nothing>]"; expected "Union[Dict[Any, Any], Callable[[], Dict[Any, Any]], None, _Empty]"
126+
DictField(default=[]) # E: Argument "default" to "DictField" has incompatible type "List[<nothing>]"; expected "Union[Dict[Any, Any], Callable[[], Dict[Any, Any]], None, _Empty]" [arg-type]
127127
128128
JSONField(allow_null=True, default=None)
129129
JSONField(default={})
130130
JSONField(default={'a': 1, 'b': 2})
131131
JSONField(default=lambda: {'a': [], 'b': 'str'})
132-
JSONField(default=[]) # E: Argument "default" to "JSONField" has incompatible type "List[<nothing>]"; expected "Union[Mapping[Any, Any], Callable[[], Mapping[Any, Any]], None, _Empty]"
132+
JSONField(default=[]) # E: Argument "default" to "JSONField" has incompatible type "List[<nothing>]"; expected "Union[Mapping[Any, Any], Callable[[], Mapping[Any, Any]], None, _Empty]" [arg-type]
133133
134134
- case: ListField_default
135135
main: |
@@ -139,4 +139,4 @@
139139
ListField(default=[])
140140
ListField(default=[0, 'one'])
141141
ListField(default=lambda: [])
142-
ListField(default='wät') # E: Argument "default" to "ListField" has incompatible type "str"; expected "Union[List[Any], Callable[[], List[Any]], None, _Empty]"
142+
ListField(default='wät') # E: Argument "default" to "ListField" has incompatible type "str"; expected "Union[List[Any], Callable[[], List[Any]], None, _Empty]" [arg-type]

tests/typecheck/test_serializers.yml

+12-12
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@
104104
- case: test_return_list_serializer_argument_is_kw_only
105105
parametrized:
106106
- arg: ""
107-
err: No overload variant of "ReturnList" matches argument type "TestSerializer"
107+
err: No overload variant of "ReturnList" matches argument type "TestSerializer" [call-overload]
108108
- arg: "[],"
109-
err: No overload variant of "ReturnList" matches argument types "List[<nothing>]", "TestSerializer"
109+
err: No overload variant of "ReturnList" matches argument types "List[<nothing>]", "TestSerializer" [call-overload]
110110
main: |
111111
from rest_framework import serializers
112112
from rest_framework.utils.serializer_helpers import ReturnList
@@ -123,9 +123,9 @@
123123
- case: test_return_list_serializer_is_required
124124
parametrized:
125125
- arg: ""
126-
err: All overload variants of "ReturnList" require at least one argument
126+
err: All overload variants of "ReturnList" require at least one argument [call-overload]
127127
- arg: "[]"
128-
err: No overload variant of "ReturnList" matches argument type "List[<nothing>]"
128+
err: No overload variant of "ReturnList" matches argument type "List[<nothing>]" [call-overload]
129129
main: |
130130
from rest_framework import serializers
131131
from rest_framework.utils.serializer_helpers import ReturnList
@@ -174,13 +174,13 @@
174174
- case: test_return_dict_serializer_argument_is_kw_only
175175
parametrized:
176176
- arg: ""
177-
err: No overload variant of "ReturnDict" matches argument type "TestSerializer"
177+
err: No overload variant of "ReturnDict" matches argument type "TestSerializer" [call-overload]
178178
- arg: "{},"
179-
err: No overload variant of "ReturnDict" matches argument types "Dict[<nothing>, <nothing>]", "TestSerializer"
179+
err: No overload variant of "ReturnDict" matches argument types "Dict[<nothing>, <nothing>]", "TestSerializer" [call-overload]
180180
- arg: "[],"
181-
err: No overload variant of "ReturnDict" matches argument types "List[<nothing>]", "TestSerializer"
181+
err: No overload variant of "ReturnDict" matches argument types "List[<nothing>]", "TestSerializer" [call-overload]
182182
- arg: "[('a', 'a')],"
183-
err: No overload variant of "ReturnDict" matches argument types "List[Tuple[str, str]]", "TestSerializer"
183+
err: No overload variant of "ReturnDict" matches argument types "List[Tuple[str, str]]", "TestSerializer" [call-overload]
184184
main: |
185185
from rest_framework import serializers
186186
from rest_framework.utils.serializer_helpers import ReturnDict
@@ -203,13 +203,13 @@
203203
- case: test_return_dict_serializer_is_required
204204
parametrized:
205205
- arg: ""
206-
err: All overload variants of "ReturnDict" require at least one argument
206+
err: All overload variants of "ReturnDict" require at least one argument [call-overload]
207207
- arg: "{}"
208-
err: No overload variant of "ReturnDict" matches argument type "Dict[<nothing>, <nothing>]"
208+
err: No overload variant of "ReturnDict" matches argument type "Dict[<nothing>, <nothing>]" [call-overload]
209209
- arg: "[]"
210-
err: No overload variant of "ReturnDict" matches argument type "List[<nothing>]"
210+
err: No overload variant of "ReturnDict" matches argument type "List[<nothing>]" [call-overload]
211211
- arg: "[('a', 'a')]"
212-
err: No overload variant of "ReturnDict" matches argument type "List[Tuple[str, str]]"
212+
err: No overload variant of "ReturnDict" matches argument type "List[Tuple[str, str]]" [call-overload]
213213
main: |
214214
from rest_framework import serializers
215215
from rest_framework.utils.serializer_helpers import ReturnDict

0 commit comments

Comments
 (0)