Skip to content

Commit 04fafaf

Browse files
authored
Use explicit mypy error codes in ignore comments and output (#497)
* Adds mypy error codes to output (affects tests) * Requires explicit error codes in `# type: ignore` comments, e.g. `type: ignore[assignment]` * Enables suppressed Ruff lint PGH003.
1 parent 4738495 commit 04fafaf

File tree

10 files changed

+50
-52
lines changed

10 files changed

+50
-52
lines changed

mypy.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ warn_unreachable = true
1111
disallow_untyped_defs = true
1212
disallow_incomplete_defs = true
1313
disable_error_code = empty-body
14-
# TODO: update our test error messages to match new mypy output
15-
show_error_codes = false
14+
enable_error_code = ignore-without-code
1615

1716
plugins =
1817
mypy_django_plugin.main,

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ ignore = ["PYI021", "PYI024", "PYI041", "PYI043"]
2929
"F405",
3030
"F822",
3131
"F821",
32-
"PGH003", # TODO fix these errors
3332
]
3433
"rest_framework-stubs/compat.pyi" = ["PYI042"]
3534

rest_framework-stubs/authtoken/models.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ class Token(models.Model):
1414

1515
class TokenProxy(Token):
1616
# This is how drf defines this:
17-
@property # type: ignore
17+
@property # type: ignore[no-redef]
1818
def pk(self) -> Any: ... # type: ignore[override]

rest_framework-stubs/decorators.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ else:
4646
class MethodMapper(dict):
4747
def __init__(self, action: _View, methods: Sequence[str]) -> None: ...
4848
def _map(self, method: str, func: _View) -> _View: ...
49-
def get(self, func: _View) -> _View: ... # type: ignore
49+
def get(self, func: _View) -> _View: ... # type: ignore[override]
5050
def post(self, func: _View) -> _View: ...
5151
def put(self, func: _View) -> _View: ...
5252
def patch(self, func: _View) -> _View: ...

rest_framework-stubs/relations.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class RelatedField(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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class ModelSerializer(Serializer[_MT]):
194194
url_field_name: str | 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

Lines changed: 3 additions & 3 deletions
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 "Sequence[str] | None"
24+
@api_view # E: Argument 1 to "api_view" has incompatible type "Callable[[Any], Any]"; expected "Sequence[str] | None" [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

Lines changed: 3 additions & 3 deletions
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(1) # E: Argument 1 to "APIException" has incompatible type "int"; expected "_APIExceptionInput"
49-
APIException({'a': 1}) # E: Dict entry 0 has incompatible type "str": "int"; expected "str": "_StrPromise | Sequence[_APIExceptionInput] | Mapping[str, _APIExceptionInput] | None"
50-
APIException({'a': ['test', 1]}) # E: List item 1 has incompatible type "int"; expected "_StrPromise | Sequence[_APIExceptionInput] | Mapping[str, _APIExceptionInput] | None"
48+
APIException(1) # E: Argument 1 to "APIException" has incompatible type "int"; expected "_APIExceptionInput" [arg-type]
49+
APIException({'a': 1}) # E: Dict entry 0 has incompatible type "str": "int"; expected "str": "_StrPromise | Sequence[_APIExceptionInput] | Mapping[str, _APIExceptionInput] | None" [dict-item]
50+
APIException({'a': ['test', 1]}) # E: List item 1 has incompatible type "int"; expected "_StrPromise | Sequence[_APIExceptionInput] | Mapping[str, _APIExceptionInput] | None" [list-item]

tests/typecheck/test_fields.yml

Lines changed: 27 additions & 27 deletions
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, False)
17-
DecimalField(1, 1, False, 1, 1, False, None, False, True) # E: Too many positional arguments for "DecimalField"
17+
DecimalField(1, 1, False, 1, 1, False, None, False, 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 Dict, Any
5252
5353
CharField(initial='', default=lambda: '')
54-
CharField(initial=None, default=4) # E: Argument "default" to "CharField" has incompatible type "int"; expected "str | _StrPromise | Callable[[], str | _StrPromise] | _Empty | None"
55-
CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "dict[Never, Never]"; expected "str | Callable[[], str] | _Empty | None"
54+
CharField(initial=None, default=4) # E: Argument "default" to "CharField" has incompatible type "int"; expected "str | _StrPromise | Callable[[], str | _StrPromise] | _Empty | None" [arg-type]
55+
CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "dict[Never, Never]"; expected "str | Callable[[], str] | _Empty | None" [arg-type]
5656
5757
x: str | None = CharField().get_initial()
58-
y: int | None = CharField().get_initial() # E: Incompatible types in assignment (expression has type "str | None", variable has type "int | None")
58+
y: int | None = CharField().get_initial() # E: Incompatible types in assignment (expression has type "str | None", variable has type "int | None") [assignment]
5959
6060
- case: float_field_args_fields
6161
main: |
@@ -75,7 +75,7 @@
7575
ChoiceField(['test'], allow_null=True, default=None)
7676
ChoiceField([1], default=int_callback)
7777
ChoiceField([1, 'lulz'], default=mixed_callback)
78-
ChoiceField([1], default=lambda: None) # E: Argument "default" to "ChoiceField" has incompatible type "Callable[[], None]"; expected "str | _StrPromise | int | Callable[[], str | _StrPromise | int] | _Empty | None" # E: Incompatible return value type (got "None", expected "str | _StrPromise | int")
78+
ChoiceField([1], default=lambda: None) # E: Argument "default" to "ChoiceField" has incompatible type "Callable[[], None]"; expected "str | _StrPromise | int | Callable[[], str | _StrPromise | int] | _Empty | None" [arg-type] # E: Incompatible return value type (got "None", expected "str | _StrPromise | int") [return-value]
7979
8080
- case: MultipleChoiceField_default
8181
main: |
@@ -88,13 +88,13 @@
8888
MultipleChoiceField(choices=['test'], allow_null=True, default=None)
8989
MultipleChoiceField(choices=[1], default=int_set_callback)
9090
MultipleChoiceField(choices=[1, 'lulz'], default=mixed_set_callback)
91-
MultipleChoiceField(choices=[1], default=lambda: [1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "Callable[[], list[int]]"; expected "set[str | int] | set[str] | set[int] | Callable[[], set[str | int] | set[str] | set[int]] | _Empty | None" # E: Incompatible return value type (got "list[int]", expected "set[str | int] | set[str] | set[int]")
91+
MultipleChoiceField(choices=[1], default=lambda: [1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "Callable[[], list[int]]"; expected "set[str | int] | set[str] | set[int] | Callable[[], set[str | int] | set[str] | set[int]] | _Empty | None" [arg-type] # E: Incompatible return value type (got "list[int]", expected "set[str | int] | set[str] | set[int]") [return-value]
9292
9393
MultipleChoiceField(choices=[(1, "1"), (2, "2")], default={1})
94-
MultipleChoiceField(choices=[(1, "1"), (2, "2")], default=[1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "list[int]"; expected "set[str | int] | set[str] | set[int] | Callable[[], set[str | int] | set[str] | set[int]] | _Empty | None"
94+
MultipleChoiceField(choices=[(1, "1"), (2, "2")], default=[1]) # E: Argument "default" to "MultipleChoiceField" has incompatible type "list[int]"; expected "set[str | int] | set[str] | set[int] | Callable[[], set[str | int] | set[str] | set[int]] | _Empty | None" [arg-type]
9595
9696
MultipleChoiceField(choices=[(1, "1"), (2, "2")], initial={1})
97-
MultipleChoiceField(choices=[(1, "1"), (2, "2")], initial=[1]) # E: Argument "initial" to "MultipleChoiceField" has incompatible type "list[int]"; expected "set[str | _StrPromise | int] | set[str | _StrPromise] | set[int] | Callable[[], set[str | _StrPromise | int] | set[str | _StrPromise] | set[int]] | _Empty | None"
97+
MultipleChoiceField(choices=[(1, "1"), (2, "2")], initial=[1]) # E: Argument "initial" to "MultipleChoiceField" has incompatible type "list[int]"; expected "set[str | _StrPromise | int] | set[str | _StrPromise] | set[int] | Callable[[], set[str | _StrPromise | int] | set[str | _StrPromise] | set[int]] | _Empty | None" [arg-type]
9898
9999
- case: FileField_default
100100
main: |
@@ -106,12 +106,12 @@
106106
FileField(allow_null=True, default=None)
107107
FileField(allow_null=True, default=file_callback)
108108
FileField(allow_null=True, default=file_callback())
109-
FileField(allow_null=True, default=123) # E: Argument "default" to "FileField" has incompatible type "int"; expected "File[Any] | Callable[[], File[Any]] | _Empty | None"
109+
FileField(allow_null=True, default=123) # E: Argument "default" to "FileField" has incompatible type "int"; expected "File[Any] | Callable[[], File[Any]] | _Empty | None" [arg-type]
110110
111111
ImageField(allow_null=True, default=None)
112112
ImageField(default=file_callback)
113113
ImageField(default=file_callback())
114-
ImageField(default='a') # E: Argument "default" to "ImageField" has incompatible type "str"; expected "File[Any] | Callable[[], File[Any]] | _Empty | None"
114+
ImageField(default='a') # E: Argument "default" to "ImageField" has incompatible type "str"; expected "File[Any] | Callable[[], File[Any]] | _Empty | None" [arg-type]
115115
116116
- case: DictField_default
117117
main: |
@@ -121,13 +121,13 @@
121121
DictField(default={})
122122
DictField(default={'a': 1, 'b': 2})
123123
DictField(default=lambda: {'a': [], 'b': 'str'})
124-
DictField(default=[]) # E: Argument "default" to "DictField" has incompatible type "list[Never]"; expected "dict[Any, Any] | Callable[[], dict[Any, Any]] | _Empty | None"
124+
DictField(default=[]) # E: Argument "default" to "DictField" has incompatible type "list[Never]"; expected "dict[Any, Any] | Callable[[], dict[Any, Any]] | _Empty | None" [arg-type]
125125
126126
JSONField(allow_null=True, default=None)
127127
JSONField(default={})
128128
JSONField(default={'a': 1, 'b': 2})
129129
JSONField(default=lambda: {'a': [], 'b': 'str'})
130-
JSONField(default=[]) # E: Argument "default" to "JSONField" has incompatible type "list[Never]"; expected "Mapping[Any, Any] | Callable[[], Mapping[Any, Any]] | _Empty | None"
130+
JSONField(default=[]) # E: Argument "default" to "JSONField" has incompatible type "list[Never]"; expected "Mapping[Any, Any] | Callable[[], Mapping[Any, Any]] | _Empty | None" [arg-type]
131131
132132
- case: ListField_default
133133
main: |
@@ -137,4 +137,4 @@
137137
ListField(default=[])
138138
ListField(default=[0, 'one'])
139139
ListField(default=lambda: [])
140-
ListField(default='wät') # E: Argument "default" to "ListField" has incompatible type "str"; expected "list[Any] | Callable[[], list[Any]] | _Empty | None"
140+
ListField(default='wät') # E: Argument "default" to "ListField" has incompatible type "str"; expected "list[Any] | Callable[[], list[Any]] | _Empty | None" [arg-type]

tests/typecheck/test_serializers.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@
103103
- case: test_return_list_serializer_argument_is_kw_only
104104
parametrized:
105105
- arg: ""
106-
err: No overload variant of "ReturnList" matches argument type "TestSerializer"
106+
err: No overload variant of "ReturnList" matches argument type "TestSerializer" [call-overload]
107107
- arg: "[],"
108-
err: No overload variant of "ReturnList" matches argument types "list[Never]", "TestSerializer"
108+
err: No overload variant of "ReturnList" matches argument types "list[Never]", "TestSerializer" [call-overload]
109109
main: |
110110
from rest_framework import serializers
111111
from rest_framework.utils.serializer_helpers import ReturnList
@@ -122,9 +122,9 @@
122122
- case: test_return_list_serializer_is_required
123123
parametrized:
124124
- arg: ""
125-
err: All overload variants of "ReturnList" require at least one argument
125+
err: All overload variants of "ReturnList" require at least one argument [call-overload]
126126
- arg: "[]"
127-
err: No overload variant of "ReturnList" matches argument type "list[Never]"
127+
err: No overload variant of "ReturnList" matches argument type "list[Never]" [call-overload]
128128
main: |
129129
from rest_framework import serializers
130130
from rest_framework.utils.serializer_helpers import ReturnList
@@ -173,13 +173,13 @@
173173
- case: test_return_dict_serializer_argument_is_kw_only
174174
parametrized:
175175
- arg: ""
176-
err: No overload variant of "ReturnDict" matches argument type "TestSerializer"
176+
err: No overload variant of "ReturnDict" matches argument type "TestSerializer" [call-overload]
177177
- arg: "{},"
178-
err: No overload variant of "ReturnDict" matches argument types "dict[Never, Never]", "TestSerializer"
178+
err: No overload variant of "ReturnDict" matches argument types "dict[Never, Never]", "TestSerializer" [call-overload]
179179
- arg: "[],"
180-
err: No overload variant of "ReturnDict" matches argument types "list[Never]", "TestSerializer"
180+
err: No overload variant of "ReturnDict" matches argument types "list[Never]", "TestSerializer" [call-overload]
181181
- arg: "[('a', 'a')],"
182-
err: No overload variant of "ReturnDict" matches argument types "list[tuple[str, str]]", "TestSerializer"
182+
err: No overload variant of "ReturnDict" matches argument types "list[tuple[str, str]]", "TestSerializer" [call-overload]
183183
main: |
184184
from rest_framework import serializers
185185
from rest_framework.utils.serializer_helpers import ReturnDict
@@ -202,13 +202,13 @@
202202
- case: test_return_dict_serializer_is_required
203203
parametrized:
204204
- arg: ""
205-
err: All overload variants of "ReturnDict" require at least one argument
205+
err: All overload variants of "ReturnDict" require at least one argument [call-overload]
206206
- arg: "{}"
207-
err: No overload variant of "ReturnDict" matches argument type "dict[Never, Never]"
207+
err: No overload variant of "ReturnDict" matches argument type "dict[Never, Never]" [call-overload]
208208
- arg: "[]"
209-
err: No overload variant of "ReturnDict" matches argument type "list[Never]"
209+
err: No overload variant of "ReturnDict" matches argument type "list[Never]" [call-overload]
210210
- arg: "[('a', 'a')]"
211-
err: No overload variant of "ReturnDict" matches argument type "list[tuple[str, str]]"
211+
err: No overload variant of "ReturnDict" matches argument type "list[tuple[str, str]]" [call-overload]
212212
main: |
213213
from rest_framework import serializers
214214
from rest_framework.utils.serializer_helpers import ReturnDict

0 commit comments

Comments
 (0)