Skip to content

Commit 69fceac

Browse files
Tests: Add mypy error codes to typecheck (#1773)
* Remove show error codes * Update test error codes Require [sd](https://github.com/chmln/sd) and python3 ```python import subprocess from collections import defaultdict from pathlib import Path state = file = None rewrite = defaultdict(list) s = Path("./pytest_errors.txt").read_text() # gather necessary rewrites for line in s.splitlines(): line = line.strip() if line.startswith("/"): file = line.strip() state = None continue line = line[4:] if line.startswith("Actual"): state = "actual" counter = 0 elif line.startswith("Expected"): state = "expected" counter = 0 elif line.startswith("Alignment"): state = None file = None elif state =="actual" and "error" in line: cleaned_line = line.split("error: ")[1].split(" (diff)")[0] rewrite[file].append([None, cleaned_line]) elif state =="expected" and "error" in line: cleaned_line = line.split("error: ")[1].split(" (diff)")[0] rewrite[file][counter][0]= cleaned_line counter+=1 # Add error codes inplace for file, errors in rewrite.items(): file = file.split(":")[0] for item in errors: subprocess.call(["sd", "-s", item[0], item[1], file]) ```
1 parent fd0d771 commit 69fceac

39 files changed

+217
-218
lines changed

mypy.ini

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ warn_unused_configs = true
1313
warn_unreachable = true
1414
disallow_untyped_defs = true
1515
disallow_incomplete_defs = true
16-
show_error_codes = false
1716
disable_error_code = empty-body
1817
# TODO: update our output assertions to match a new syntax
1918
force_uppercase_builtins = true

tests/typecheck/contrib/admin/test_decorators.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@admin.display
1818
def display_property(self) -> str: ...
1919
20-
@admin.display # E: Decorators on top of @property are not supported
20+
@admin.display # E: Decorators on top of @property are not supported [misc]
2121
@property
2222
def incorrect_property(self) -> str: ...
2323
@@ -79,10 +79,10 @@
7979
@admin.action
8080
def freestanding_action_file_response(modeladmin: "MyModelAdmin", request: HttpRequest, queryset: QuerySet[MyModel]) -> FileResponse: ...
8181
82-
@admin.action # E: Value of type variable "_ModelAdmin" of "action" cannot be "int"
82+
@admin.action # E: Value of type variable "_ModelAdmin" of "action" cannot be "int" [type-var]
8383
def freestanding_action_invalid_bare(modeladmin: int, request: HttpRequest, queryset: QuerySet[MyModel]) -> None: ...
8484
85-
@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_ModelAdmin" of function cannot be "int"
85+
@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_ModelAdmin" of function cannot be "int" [type-var]
8686
def freestanding_action_invalid_fancy(modeladmin: int, request: HttpRequest, queryset: QuerySet[MyModel]) -> None: ...
8787
8888
@admin.register(MyModel)
@@ -101,10 +101,10 @@
101101
@admin.action(description="Some text here", permissions=["test"])
102102
def method_action_file_response(self, request: HttpRequest, queryset: QuerySet[MyModel]) -> FileResponse: ...
103103
104-
@admin.action # E: Value of type variable "_QuerySet" of "action" cannot be "int"
104+
@admin.action # E: Value of type variable "_QuerySet" of "action" cannot be "int" [type-var]
105105
def method_action_invalid_bare(self, request: HttpRequest, queryset: int) -> None: ...
106106
107-
@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_QuerySet" of function cannot be "int"
107+
@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_QuerySet" of function cannot be "int" [type-var]
108108
def method_action_invalid_fancy(self, request: HttpRequest, queryset: int) -> None: ...
109109
110110
def method(self) -> None:

tests/typecheck/contrib/admin/test_options.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,25 @@
108108
109109
class A(admin.ModelAdmin):
110110
fieldsets = [
111-
(None, {}), # E: Missing key "fields" for TypedDict "_FieldOpts"
111+
(None, {}), # E: Missing key "fields" for TypedDict "_FieldOpts" [typeddict-item]
112112
]
113113
- case: errors_on_invalid_radio_fields
114114
main: |
115115
from django.contrib import admin
116116
117117
class A(admin.ModelAdmin):
118-
radio_fields = {"some_field": 0} # E: Dict entry 0 has incompatible type "str": "Literal[0]"; expected "str": "Literal[1, 2]"
118+
radio_fields = {"some_field": 0} # E: Dict entry 0 has incompatible type "str": "Literal[0]"; expected "str": "Literal[1, 2]" [dict-item]
119119
120120
class B(admin.ModelAdmin):
121-
radio_fields = {1: admin.VERTICAL} # E: Dict entry 0 has incompatible type "int": "Literal[2]"; expected "str": "Literal[1, 2]"
121+
radio_fields = {1: admin.VERTICAL} # E: Dict entry 0 has incompatible type "int": "Literal[2]"; expected "str": "Literal[1, 2]" [dict-item]
122122
- case: errors_for_invalid_formfield_overrides
123123
main: |
124124
from django.contrib import admin
125125
from django.forms import Textarea
126126
127127
class A(admin.ModelAdmin):
128128
formfield_overrides = {
129-
"not a field": { # E: Dict entry 0 has incompatible type "str": "Dict[str, Any]"; expected "Type[Field[Any, Any]]": "Mapping[str, Any]"
129+
"not a field": { # E: Dict entry 0 has incompatible type "str": "Dict[str, Any]"; expected "Type[Field[Any, Any]]": "Mapping[str, Any]" [dict-item]
130130
"widget": Textarea
131131
}
132132
}
@@ -140,7 +140,7 @@
140140
pass
141141
142142
class A(admin.ModelAdmin):
143-
actions = [an_action] # E: List item 0 has incompatible type "Callable[[None], None]"; expected "Union[Callable[[Any, HttpRequest, _QuerySet[Any, Any]], Optional[HttpResponseBase]], str]"
143+
actions = [an_action] # E: List item 0 has incompatible type "Callable[[None], None]"; expected "Union[Callable[[Any, HttpRequest, _QuerySet[Any, Any]], Optional[HttpResponseBase]], str]" [list-item]
144144
- case: errors_for_invalid_model_admin_generic
145145
main: |
146146
from django.contrib.admin import ModelAdmin
@@ -150,4 +150,4 @@
150150
pass
151151
152152
class A(ModelAdmin[TestModel]):
153-
model = int # E: Incompatible types in assignment (expression has type "Type[int]", base class "ModelAdmin" defined the type as "Type[TestModel]")
153+
model = int # E: Incompatible types in assignment (expression has type "Type[int]", base class "ModelAdmin" defined the type as "Type[TestModel]") [assignment]

tests/typecheck/contrib/auth/test_decorators.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
main: |
2727
from typing import Any
2828
from django.contrib.auth.decorators import login_required
29-
@login_required() # E: Value of type variable "_VIEW" of function cannot be "Callable[[Any], str]"
29+
@login_required() # E: Value of type variable "_VIEW" of function cannot be "Callable[[Any], str]" [type-var]
3030
def view_func2(request: Any) -> str: ...
3131
- case: user_passes_test
3232
main: |
@@ -39,7 +39,7 @@
3939
main: |
4040
from django.http import HttpRequest, HttpResponse
4141
from django.contrib.auth.decorators import user_passes_test
42-
@user_passes_test # E: Argument 1 to "user_passes_test" has incompatible type "Callable[[HttpRequest], HttpResponse]"; expected "Callable[[Union[AbstractBaseUser, AnonymousUser]], bool]"
42+
@user_passes_test # E: Argument 1 to "user_passes_test" has incompatible type "Callable[[HttpRequest], HttpResponse]"; expected "Callable[[Union[AbstractBaseUser, AnonymousUser]], bool]" [arg-type]
4343
def view_func(request: HttpRequest) -> HttpResponse: ...
4444
- case: permission_required
4545
main: |

tests/typecheck/contrib/postgres/test_fields.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from typing import List
44
from myapp.models import MyModel
55
array_val: List[int] = [1]
6-
MyModel(array=array_val) # E: Incompatible type for "array" of "MyModel" (got "List[int]", expected "Union[Sequence[str], Combinable]")
6+
MyModel(array=array_val) # E: Incompatible type for "array" of "MyModel" (got "List[int]", expected "Union[Sequence[str], Combinable]") [misc]
77
non_init = MyModel()
8-
non_init.array = array_val # E: Incompatible types in assignment (expression has type "List[int]", variable has type "Union[Sequence[str], Combinable]")
8+
non_init.array = array_val # E: Incompatible types in assignment (expression has type "List[int]", variable has type "Union[Sequence[str], Combinable]") [assignment]
99
installed_apps:
1010
- myapp
1111
files:

tests/typecheck/contrib/sitemaps/test_generic_sitemap.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"myapp:detail-offer",
1919
kwargs={
2020
"provider_name": item.provider,
21-
"offer_name": item.trc, # E: "Offer" has no attribute "trc"
21+
"offer_name": item.trc, # E: "Offer" has no attribute "trc" [attr-defined]
2222
},
2323
)
2424
@@ -44,12 +44,12 @@
4444
),
4545
]
4646
out: |
47-
main:24: error: Return type "str" of "items" incompatible with return type "Iterable[Offer]" in supertype "Sitemap"
48-
main:26: error: Return type "int" of "location" incompatible with return type "str" in supertype "Sitemap"
49-
main:26: error: Argument 1 of "location" is incompatible with supertype "Sitemap"; supertype defines the argument type as "Offer"
47+
main:24: error: Return type "str" of "items" incompatible with return type "Iterable[Offer]" in supertype "Sitemap" [override]
48+
main:26: error: Return type "int" of "location" incompatible with return type "str" in supertype "Sitemap" [override]
49+
main:26: error: Argument 1 of "location" is incompatible with supertype "Sitemap"; supertype defines the argument type as "Offer" [override]
5050
main:26: note: This violates the Liskov substitution principle
5151
main:26: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
52-
main:40: error: Argument 1 to "GenericSitemap" has incompatible type "Dict[str, List[int]]"; expected "Mapping[str, Union[datetime, _QuerySet[Offer, Offer], str]]"
52+
main:40: error: Argument 1 to "GenericSitemap" has incompatible type "Dict[str, List[int]]"; expected "Mapping[str, Union[datetime, _QuerySet[Offer, Offer], str]]" [arg-type]
5353
5454
installed_apps:
5555
- myapp

tests/typecheck/core/test_checks.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
2626
reveal_type(check_baz) # N: Revealed type is "django.core.checks.registry._ProcessedCheckCallable[def (**kwargs: Any) -> typing.Sequence[django.core.checks.messages.CheckMessage]]"
2727
28-
@register() # E: Value of type variable "_C" of function cannot be "Callable[[int], Sequence[CheckMessage]]"
28+
@register() # E: Value of type variable "_C" of function cannot be "Callable[[int], Sequence[CheckMessage]]" [type-var]
2929
def wrong_args(bla: int) -> Sequence[CheckMessage]: ...

tests/typecheck/db/migrations/test_operations.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
1717
RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])], reverse_sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])])
1818
19-
RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[<nothing>, <nothing>]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[()]], None]]], ...], Tuple[()]]]"
20-
RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])]) # E: List item 0 has incompatible type "object"; expected "str"
19+
RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[<nothing>, <nothing>]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[()]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[()]], None]]], ...], Tuple[()]]]" [arg-type]
20+
RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])]) # E: List item 0 has incompatible type "object"; expected "str" [list-item]

tests/typecheck/db/models/test_init.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
assert field.many_to_one is not None
2121
my_bool = field.many_to_one
2222
out: |
23-
main:6: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
24-
main:7: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
25-
main:8: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
26-
main:9: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
23+
main:6: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]
24+
main:7: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]
25+
main:8: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]
26+
main:9: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]

tests/typecheck/db/models/test_query.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class MyModelModelIterable(ModelIterable[MyModel]):
1919
pass
2020
out: |
21-
main:4: error: Type argument "int" of "ModelIterable" must be a subtype of "Model"
21+
main:4: error: Type argument "int" of "ModelIterable" must be a subtype of "Model" [type-var]
2222
2323
2424
- case: django_db_models_query_module_has_ValuesListIterable
@@ -36,7 +36,7 @@
3636
class NonTupleValuesListIterable(ValuesListIterable[int]):
3737
pass
3838
out: |
39-
main:11: error: Type argument "int" of "ValuesListIterable" must be a subtype of "Tuple[Any, ...]"
39+
main:11: error: Type argument "int" of "ValuesListIterable" must be a subtype of "Tuple[Any, ...]" [type-var]
4040
4141
4242
- case: django_db_models_query_module_has_NamedValuesListIterable
@@ -53,4 +53,4 @@
5353
QuerySet._iterable_class = ModelIterable
5454
QuerySet._iterable_class = int
5555
out: |
56-
main:5: error: Incompatible types in assignment (expression has type "Type[int]", variable has type "Type[BaseIterable[Any]]")
56+
main:5: error: Incompatible types in assignment (expression has type "Type[int]", variable has type "Type[BaseIterable[Any]]") [assignment]

tests/typecheck/fields/test_base.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
main: |
6464
from myapp.models import User
6565
reveal_type(User().my_pk) # N: Revealed type is "builtins.int"
66-
User().id # E: "User" has no attribute "id"
66+
User().id # E: "User" has no attribute "id" [attr-defined]
6767
installed_apps:
6868
- myapp
6969
files:
@@ -94,9 +94,9 @@
9494
- case: blank_and_not_null_charfield_does_not_allow_none
9595
main: |
9696
from myapp.models import MyModel
97-
MyModel(notnulltext=None) # E: Incompatible type for "notnulltext" of "MyModel" (got "None", expected "Union[str, int, Combinable]")
97+
MyModel(notnulltext=None) # E: Incompatible type for "notnulltext" of "MyModel" (got "None", expected "Union[str, int, Combinable]") [misc]
9898
MyModel(notnulltext="")
99-
MyModel().notnulltext = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]")
99+
MyModel().notnulltext = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]") [assignment]
100100
reveal_type(MyModel().notnulltext) # N: Revealed type is "builtins.str"
101101
installed_apps:
102102
- myapp
@@ -158,7 +158,7 @@
158158
published = cast(models.Field[Year, Year], models.IntegerField())
159159
book = Book()
160160
reveal_type(book.published) # N: Revealed type is "main.Year"
161-
book.published = 2006 # E: Incompatible types in assignment (expression has type "int", variable has type "Year")
161+
book.published = 2006 # E: Incompatible types in assignment (expression has type "int", variable has type "Year") [assignment]
162162
book.published = Year(2006)
163163
reveal_type(book.published) # N: Revealed type is "main.Year"
164164
def accepts_int(arg: int) -> None: ...
@@ -190,9 +190,9 @@
190190
from myapp.models import RenamedField
191191
instance = RenamedField()
192192
reveal_type(instance.modelname) # N: Revealed type is "builtins.int"
193-
instance.fieldname # E: "RenamedField" has no attribute "fieldname"
193+
instance.fieldname # E: "RenamedField" has no attribute "fieldname" [attr-defined]
194194
instance.modelname = 1
195-
instance.fieldname = 1 # E: "RenamedField" has no attribute "fieldname"
195+
instance.fieldname = 1 # E: "RenamedField" has no attribute "fieldname" [attr-defined]
196196
installed_apps:
197197
- myapp
198198
files:

tests/typecheck/fields/test_nullable.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from myapp.models import MyModel
3737
reveal_type(MyModel().text) # N: Revealed type is "builtins.str"
3838
reveal_type(MyModel().text_nullable) # N: Revealed type is "Union[builtins.str, None]"
39-
MyModel().text = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]")
39+
MyModel().text = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]") [assignment]
4040
MyModel().text_nullable = None
4141
installed_apps:
4242
- myapp
@@ -69,7 +69,7 @@
6969
main: |
7070
from myapp.models import Publisher, Book
7171
reveal_type(Book().publisher) # N: Revealed type is "Union[myapp.models.Publisher, None]"
72-
Book().publisher = 11 # E: Incompatible types in assignment (expression has type "int", variable has type "Union[Publisher, Combinable, None]")
72+
Book().publisher = 11 # E: Incompatible types in assignment (expression has type "int", variable has type "Union[Publisher, Combinable, None]") [assignment]
7373
installed_apps:
7474
- myapp
7575
files:

0 commit comments

Comments
 (0)