Skip to content
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

Use explicit mypy error codes in ignore comments and output #497

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ ignore = ["PYI021", "PYI024", "PYI041", "PYI043"]
"F405",
"F822",
"F821",
"PGH003", # TODO fix these errors
]
"rest_framework-stubs/compat.pyi" = ["PYI042"]

Expand Down
2 changes: 1 addition & 1 deletion rest_framework-stubs/decorators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
2 changes: 1 addition & 1 deletion rest_framework-stubs/relations.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand Down
2 changes: 1 addition & 1 deletion rest_framework-stubs/serializers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/typecheck/test_decorators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/typecheck/test_exceptions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
54 changes: 27 additions & 27 deletions tests/typecheck/test_fields.yml
Original file line number Diff line number Diff line change
@@ -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: |
Expand All @@ -14,48 +14,48 @@
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: |
from rest_framework.fields import DictField, CharField, empty
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: |
Expand All @@ -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: |
Expand All @@ -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: |
Expand All @@ -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: |
Expand All @@ -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: |
Expand All @@ -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]
24 changes: 12 additions & 12 deletions tests/typecheck/test_serializers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down