Skip to content

Commit a2d3fbf

Browse files
authored
Tests: Cleanup usages of obsolete Union/Optional style (#792)
We can now disable `force_union_syntax` in mypy settings.
1 parent b19ec0b commit a2d3fbf

File tree

9 files changed

+27
-33
lines changed

9 files changed

+27
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MyModelSerializer(serializers.ModelSerializer[MyModel]):
4040
fields = ("id", "example")
4141
```
4242

43-
Which means that methods where the model is being passed around will know the actual type of the model instead of being `Any`. The `instance` attribute on the above serializer will be `Union[MyModel, typing.Sequence[MyModel], None]`.
43+
Which means that methods where the model is being passed around will know the actual type of the model instead of being `Any`. The `instance` attribute on the above serializer will be `MyModel | typing.Sequence[MyModel] | None`.
4444

4545
## To get help
4646

mypy.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ disallow_incomplete_defs = true
1313
disable_error_code = empty-body
1414
# TODO: update our test error messages to match new mypy output
1515
show_error_codes = false
16-
force_union_syntax = true
1716

1817
plugins =
1918
mypy_django_plugin.main,

tests/typecheck/test_decorators.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
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 "Sequence[str] | None"
2525
def view_func2(request: Any) -> Any: ...
2626
- case: api_view_incorrect_return
2727
main: |
@@ -33,7 +33,7 @@
3333
- case: permission_classes
3434
main: |
3535
from rest_framework.decorators import permission_classes
36-
reveal_type(permission_classes) # N: Revealed type is "def (permission_classes: typing.Sequence[Union[type[rest_framework.permissions.BasePermission], rest_framework.permissions.OperandHolder, rest_framework.permissions.SingleOperandHolder]]) -> def [_View <: def (*Any, **Any) -> django.http.response.HttpResponseBase] (_View`-1) -> _View`-1"
36+
reveal_type(permission_classes) # N: Revealed type is "def (permission_classes: typing.Sequence[type[rest_framework.permissions.BasePermission] | rest_framework.permissions.OperandHolder | rest_framework.permissions.SingleOperandHolder]) -> def [_View <: def (*Any, **Any) -> django.http.response.HttpResponseBase] (_View`-1) -> _View`-1"
3737
3838
- case: permission_classes_with_operators
3939
main: |

tests/typecheck/test_exceptions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
APIException(None, None)
4848
APIException(1) # E: Argument 1 to "APIException" has incompatible type "int"; expected "_APIExceptionInput"
4949
APIException({'a': 1}) # E: Argument 1 to "APIException" has incompatible type "dict[str, int]"; expected "_APIExceptionInput"
50-
APIException({'a': ['test', 1]}) # E: Argument 1 to "APIException" has incompatible type "dict[str, list[Union[str, int]]]"; expected "_APIExceptionInput"
50+
APIException({'a': ['test', 1]}) # E: Argument 1 to "APIException" has incompatible type "dict[str, list[str | int]]"; expected "_APIExceptionInput"

tests/typecheck/test_fields.yml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
- case: default_and_inital_args_fields
4949
main: |
5050
from rest_framework.fields import DictField, CharField, empty
51-
from typing import Optional, Dict, Any
51+
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 "Union[Union[str, _StrPromise], Callable[[], Union[str, _StrPromise]], _Empty, None]"
55-
CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "dict[Never, Never]"; expected "Union[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"
55+
CharField(initial={}, default=empty) # E: Argument "initial" to "CharField" has incompatible type "dict[Never, Never]"; expected "str | Callable[[], str] | _Empty | None"
5656
57-
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]")
57+
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")
5959
6060
- case: float_field_args_fields
6161
main: |
@@ -66,37 +66,35 @@
6666
6767
- case: ChoiceField_default
6868
main: |
69-
from typing import Union
7069
from rest_framework.fields import ChoiceField
7170
7271
def int_callback() -> int: ...
73-
def mixed_callback() -> Union[int, str]: ...
72+
def mixed_callback() -> int | str: ...
7473
7574
ChoiceField([1], default=1)
7675
ChoiceField(['test'], allow_null=True, default=None)
7776
ChoiceField([1], default=int_callback)
7877
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]], _Empty, None]" # E: Incompatible return value type (got "None", expected "Union[Union[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" # E: Incompatible return value type (got "None", expected "str | _StrPromise | int")
8079
8180
- case: MultipleChoiceField_default
8281
main: |
83-
from typing import Union
8482
from rest_framework.fields import MultipleChoiceField
8583
8684
def int_set_callback() -> set[int]: ...
87-
def mixed_set_callback() -> set[Union[int, str]]: ...
85+
def mixed_set_callback() -> set[int | str]: ...
8886
8987
MultipleChoiceField(choices=[1], default={1})
9088
MultipleChoiceField(choices=['test'], allow_null=True, default=None)
9189
MultipleChoiceField(choices=[1], default=int_set_callback)
9290
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]]], _Empty, None]" # E: Incompatible return value type (got "list[int]", expected "Union[set[Union[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" # E: Incompatible return value type (got "list[int]", expected "set[str | int] | set[str] | set[int]")
9492
9593
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]]], _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"
9795
9896
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]]], _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"
10098
10199
- case: FileField_default
102100
main: |
@@ -108,12 +106,12 @@
108106
FileField(allow_null=True, default=None)
109107
FileField(allow_null=True, default=file_callback)
110108
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]], _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"
112110
113111
ImageField(allow_null=True, default=None)
114112
ImageField(default=file_callback)
115113
ImageField(default=file_callback())
116-
ImageField(default='a') # E: Argument "default" to "ImageField" has incompatible type "str"; expected "Union[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"
117115
118116
- case: DictField_default
119117
main: |
@@ -123,13 +121,13 @@
123121
DictField(default={})
124122
DictField(default={'a': 1, 'b': 2})
125123
DictField(default=lambda: {'a': [], 'b': 'str'})
126-
DictField(default=[]) # E: Argument "default" to "DictField" has incompatible type "list[Never]"; expected "Union[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"
127125
128126
JSONField(allow_null=True, default=None)
129127
JSONField(default={})
130128
JSONField(default={'a': 1, 'b': 2})
131129
JSONField(default=lambda: {'a': [], 'b': 'str'})
132-
JSONField(default=[]) # E: Argument "default" to "JSONField" has incompatible type "list[Never]"; expected "Union[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"
133131
134132
- case: ListField_default
135133
main: |
@@ -139,4 +137,4 @@
139137
ListField(default=[])
140138
ListField(default=[0, 'one'])
141139
ListField(default=lambda: [])
142-
ListField(default='wät') # E: Argument "default" to "ListField" has incompatible type "str"; expected "Union[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"

tests/typecheck/test_pagination.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
request: Request
1010
queryset: QuerySet[User]
1111
page = paginator.paginate_queryset(queryset, request)
12-
reveal_type(page) # N: Revealed type is "Union[builtins.list[django.contrib.auth.models.User], None]"
12+
reveal_type(page) # N: Revealed type is "builtins.list[django.contrib.auth.models.User] | None"

tests/typecheck/test_routers.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
- case: test_router_urls
22
main: |
3-
from typing import Union
4-
53
from django.urls import path, include
64
from rest_framework.routers import SimpleRouter, DefaultRouter
75
from rest_framework.urlpatterns import _AnyURL
86
97
simple = SimpleRouter()
10-
reveal_type(simple.urls) # N: Revealed type is "builtins.list[Union[django.urls.resolvers.URLPattern, django.urls.resolvers.URLResolver]]"
8+
reveal_type(simple.urls) # N: Revealed type is "builtins.list[django.urls.resolvers.URLPattern | django.urls.resolvers.URLResolver]"
119
default = DefaultRouter()
12-
reveal_type(default.urls) # N: Revealed type is "builtins.list[Union[django.urls.resolvers.URLPattern, django.urls.resolvers.URLResolver]]"
10+
reveal_type(default.urls) # N: Revealed type is "builtins.list[django.urls.resolvers.URLPattern | django.urls.resolvers.URLResolver]"
1311
1412
urlpatterns: list[_AnyURL] = [
1513
path('api/', include(simple.urls)),

tests/typecheck/test_serializers.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
1717
reveal_type(serializers.ModelSerializer.Meta.model) # N: Revealed type is "type[_MT?]"
1818
reveal_type(serializers.ModelSerializer.Meta.fields) # N: Revealed type is "typing.Sequence[builtins.str]"
19-
reveal_type(serializers.ModelSerializer.Meta.read_only_fields) # N: Revealed type is "Union[typing.Sequence[builtins.str], None]"
20-
reveal_type(serializers.ModelSerializer.Meta.exclude) # N: Revealed type is "Union[typing.Sequence[builtins.str], None]"
21-
reveal_type(serializers.ModelSerializer.Meta.depth) # N: Revealed type is "Union[builtins.int, None]"
19+
reveal_type(serializers.ModelSerializer.Meta.read_only_fields) # N: Revealed type is "typing.Sequence[builtins.str] | None"
20+
reveal_type(serializers.ModelSerializer.Meta.exclude) # N: Revealed type is "typing.Sequence[builtins.str] | None"
21+
reveal_type(serializers.ModelSerializer.Meta.depth) # N: Revealed type is "builtins.int | None"
2222
reveal_type(serializers.ModelSerializer.Meta.extra_kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.dict[builtins.str, Any]]"
2323
2424
- case: test_model_serializer_passes_check

tests/typecheck/test_test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
1717
- case: test_testcases_client_api_urlpatterns
1818
main: |
19-
from typing import Union
2019
from django.urls import URLPattern, URLResolver
2120
from rest_framework import test, status
2221
2322
class MyTest(test.URLPatternsTestCase):
24-
urlpatterns : list[Union[URLPattern, URLResolver]] = []
23+
urlpatterns : list[URLPattern | URLResolver] = []
2524
def test_example(self) -> None:
2625
reveal_type(self.client) # N: Revealed type is "django.test.client.Client"
2726
response = self.client.get('/', format="json")

0 commit comments

Comments
 (0)