Skip to content

Tests: Add mypy error codes to typecheck #1773

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

Merged
merged 2 commits into from
Oct 16, 2023
Merged
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
1 change: 0 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ warn_unused_configs = true
warn_unreachable = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
show_error_codes = false
disable_error_code = empty-body
# TODO: update our output assertions to match a new syntax
force_uppercase_builtins = true
Expand Down
10 changes: 5 additions & 5 deletions tests/typecheck/contrib/admin/test_decorators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@admin.display
def display_property(self) -> str: ...

@admin.display # E: Decorators on top of @property are not supported
@admin.display # E: Decorators on top of @property are not supported [misc]
@property
def incorrect_property(self) -> str: ...

Expand Down Expand Up @@ -79,10 +79,10 @@
@admin.action
def freestanding_action_file_response(modeladmin: "MyModelAdmin", request: HttpRequest, queryset: QuerySet[MyModel]) -> FileResponse: ...

@admin.action # E: Value of type variable "_ModelAdmin" of "action" cannot be "int"
@admin.action # E: Value of type variable "_ModelAdmin" of "action" cannot be "int" [type-var]
def freestanding_action_invalid_bare(modeladmin: int, request: HttpRequest, queryset: QuerySet[MyModel]) -> None: ...

@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_ModelAdmin" of function cannot be "int"
@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_ModelAdmin" of function cannot be "int" [type-var]
def freestanding_action_invalid_fancy(modeladmin: int, request: HttpRequest, queryset: QuerySet[MyModel]) -> None: ...

@admin.register(MyModel)
Expand All @@ -101,10 +101,10 @@
@admin.action(description="Some text here", permissions=["test"])
def method_action_file_response(self, request: HttpRequest, queryset: QuerySet[MyModel]) -> FileResponse: ...

@admin.action # E: Value of type variable "_QuerySet" of "action" cannot be "int"
@admin.action # E: Value of type variable "_QuerySet" of "action" cannot be "int" [type-var]
def method_action_invalid_bare(self, request: HttpRequest, queryset: int) -> None: ...

@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_QuerySet" of function cannot be "int"
@admin.action(description="Some text here", permissions=["test"]) # E: Value of type variable "_QuerySet" of function cannot be "int" [type-var]
def method_action_invalid_fancy(self, request: HttpRequest, queryset: int) -> None: ...

def method(self) -> None:
Expand Down
12 changes: 6 additions & 6 deletions tests/typecheck/contrib/admin/test_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,25 @@

class A(admin.ModelAdmin):
fieldsets = [
(None, {}), # E: Missing key "fields" for TypedDict "_FieldOpts"
(None, {}), # E: Missing key "fields" for TypedDict "_FieldOpts" [typeddict-item]
]
- case: errors_on_invalid_radio_fields
main: |
from django.contrib import admin

class A(admin.ModelAdmin):
radio_fields = {"some_field": 0} # E: Dict entry 0 has incompatible type "str": "Literal[0]"; expected "str": "Literal[1, 2]"
radio_fields = {"some_field": 0} # E: Dict entry 0 has incompatible type "str": "Literal[0]"; expected "str": "Literal[1, 2]" [dict-item]

class B(admin.ModelAdmin):
radio_fields = {1: admin.VERTICAL} # E: Dict entry 0 has incompatible type "int": "Literal[2]"; expected "str": "Literal[1, 2]"
radio_fields = {1: admin.VERTICAL} # E: Dict entry 0 has incompatible type "int": "Literal[2]"; expected "str": "Literal[1, 2]" [dict-item]
- case: errors_for_invalid_formfield_overrides
main: |
from django.contrib import admin
from django.forms import Textarea

class A(admin.ModelAdmin):
formfield_overrides = {
"not a field": { # E: Dict entry 0 has incompatible type "str": "Dict[str, Any]"; expected "Type[Field[Any, Any]]": "Mapping[str, Any]"
"not a field": { # E: Dict entry 0 has incompatible type "str": "Dict[str, Any]"; expected "Type[Field[Any, Any]]": "Mapping[str, Any]" [dict-item]
"widget": Textarea
}
}
Expand All @@ -140,7 +140,7 @@
pass

class A(admin.ModelAdmin):
actions = [an_action] # E: List item 0 has incompatible type "Callable[[None], None]"; expected "Union[Callable[[Any, HttpRequest, _QuerySet[Any, Any]], Optional[HttpResponseBase]], str]"
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]
- case: errors_for_invalid_model_admin_generic
main: |
from django.contrib.admin import ModelAdmin
Expand All @@ -150,4 +150,4 @@
pass

class A(ModelAdmin[TestModel]):
model = int # E: Incompatible types in assignment (expression has type "Type[int]", base class "ModelAdmin" defined the type as "Type[TestModel]")
model = int # E: Incompatible types in assignment (expression has type "Type[int]", base class "ModelAdmin" defined the type as "Type[TestModel]") [assignment]
4 changes: 2 additions & 2 deletions tests/typecheck/contrib/auth/test_decorators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
main: |
from typing import Any
from django.contrib.auth.decorators import login_required
@login_required() # E: Value of type variable "_VIEW" of function cannot be "Callable[[Any], str]"
@login_required() # E: Value of type variable "_VIEW" of function cannot be "Callable[[Any], str]" [type-var]
def view_func2(request: Any) -> str: ...
- case: user_passes_test
main: |
Expand All @@ -39,7 +39,7 @@
main: |
from django.http import HttpRequest, HttpResponse
from django.contrib.auth.decorators import user_passes_test
@user_passes_test # E: Argument 1 to "user_passes_test" has incompatible type "Callable[[HttpRequest], HttpResponse]"; expected "Callable[[Union[AbstractBaseUser, AnonymousUser]], bool]"
@user_passes_test # E: Argument 1 to "user_passes_test" has incompatible type "Callable[[HttpRequest], HttpResponse]"; expected "Callable[[Union[AbstractBaseUser, AnonymousUser]], bool]" [arg-type]
def view_func(request: HttpRequest) -> HttpResponse: ...
- case: permission_required
main: |
Expand Down
4 changes: 2 additions & 2 deletions tests/typecheck/contrib/postgres/test_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from typing import List
from myapp.models import MyModel
array_val: List[int] = [1]
MyModel(array=array_val) # E: Incompatible type for "array" of "MyModel" (got "List[int]", expected "Union[Sequence[str], Combinable]")
MyModel(array=array_val) # E: Incompatible type for "array" of "MyModel" (got "List[int]", expected "Union[Sequence[str], Combinable]") [misc]
non_init = MyModel()
non_init.array = array_val # E: Incompatible types in assignment (expression has type "List[int]", variable has type "Union[Sequence[str], Combinable]")
non_init.array = array_val # E: Incompatible types in assignment (expression has type "List[int]", variable has type "Union[Sequence[str], Combinable]") [assignment]
installed_apps:
- myapp
files:
Expand Down
10 changes: 5 additions & 5 deletions tests/typecheck/contrib/sitemaps/test_generic_sitemap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"myapp:detail-offer",
kwargs={
"provider_name": item.provider,
"offer_name": item.trc, # E: "Offer" has no attribute "trc"
"offer_name": item.trc, # E: "Offer" has no attribute "trc" [attr-defined]
},
)

Expand All @@ -44,12 +44,12 @@
),
]
out: |
main:24: error: Return type "str" of "items" incompatible with return type "Iterable[Offer]" in supertype "Sitemap"
main:26: error: Return type "int" of "location" incompatible with return type "str" in supertype "Sitemap"
main:26: error: Argument 1 of "location" is incompatible with supertype "Sitemap"; supertype defines the argument type as "Offer"
main:24: error: Return type "str" of "items" incompatible with return type "Iterable[Offer]" in supertype "Sitemap" [override]
main:26: error: Return type "int" of "location" incompatible with return type "str" in supertype "Sitemap" [override]
main:26: error: Argument 1 of "location" is incompatible with supertype "Sitemap"; supertype defines the argument type as "Offer" [override]
main:26: note: This violates the Liskov substitution principle
main:26: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
main:40: error: Argument 1 to "GenericSitemap" has incompatible type "Dict[str, List[int]]"; expected "Mapping[str, Union[datetime, _QuerySet[Offer, Offer], str]]"
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]

installed_apps:
- myapp
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/core/test_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@

reveal_type(check_baz) # N: Revealed type is "django.core.checks.registry._ProcessedCheckCallable[def (**kwargs: Any) -> typing.Sequence[django.core.checks.messages.CheckMessage]]"

@register() # E: Value of type variable "_C" of function cannot be "Callable[[int], Sequence[CheckMessage]]"
@register() # E: Value of type variable "_C" of function cannot be "Callable[[int], Sequence[CheckMessage]]" [type-var]
def wrong_args(bla: int) -> Sequence[CheckMessage]: ...
4 changes: 2 additions & 2 deletions tests/typecheck/db/migrations/test_operations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])], reverse_sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])])

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[()]]]"
RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])]) # E: List item 0 has incompatible type "object"; expected "str"
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]
RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])]) # E: List item 0 has incompatible type "object"; expected "str" [list-item]
8 changes: 4 additions & 4 deletions tests/typecheck/db/models/test_init.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
assert field.many_to_one is not None
my_bool = field.many_to_one
out: |
main:6: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
main:7: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
main:8: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
main:9: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool")
main:6: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]
main:7: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]
main:8: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]
main:9: error: Incompatible types in assignment (expression has type "Optional[bool]", variable has type "bool") [assignment]
6 changes: 3 additions & 3 deletions tests/typecheck/db/models/test_query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class MyModelModelIterable(ModelIterable[MyModel]):
pass
out: |
main:4: error: Type argument "int" of "ModelIterable" must be a subtype of "Model"
main:4: error: Type argument "int" of "ModelIterable" must be a subtype of "Model" [type-var]


- case: django_db_models_query_module_has_ValuesListIterable
Expand All @@ -36,7 +36,7 @@
class NonTupleValuesListIterable(ValuesListIterable[int]):
pass
out: |
main:11: error: Type argument "int" of "ValuesListIterable" must be a subtype of "Tuple[Any, ...]"
main:11: error: Type argument "int" of "ValuesListIterable" must be a subtype of "Tuple[Any, ...]" [type-var]


- case: django_db_models_query_module_has_NamedValuesListIterable
Expand All @@ -53,4 +53,4 @@
QuerySet._iterable_class = ModelIterable
QuerySet._iterable_class = int
out: |
main:5: error: Incompatible types in assignment (expression has type "Type[int]", variable has type "Type[BaseIterable[Any]]")
main:5: error: Incompatible types in assignment (expression has type "Type[int]", variable has type "Type[BaseIterable[Any]]") [assignment]
12 changes: 6 additions & 6 deletions tests/typecheck/fields/test_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
main: |
from myapp.models import User
reveal_type(User().my_pk) # N: Revealed type is "builtins.int"
User().id # E: "User" has no attribute "id"
User().id # E: "User" has no attribute "id" [attr-defined]
installed_apps:
- myapp
files:
Expand Down Expand Up @@ -94,9 +94,9 @@
- case: blank_and_not_null_charfield_does_not_allow_none
main: |
from myapp.models import MyModel
MyModel(notnulltext=None) # E: Incompatible type for "notnulltext" of "MyModel" (got "None", expected "Union[str, int, Combinable]")
MyModel(notnulltext=None) # E: Incompatible type for "notnulltext" of "MyModel" (got "None", expected "Union[str, int, Combinable]") [misc]
MyModel(notnulltext="")
MyModel().notnulltext = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]")
MyModel().notnulltext = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]") [assignment]
reveal_type(MyModel().notnulltext) # N: Revealed type is "builtins.str"
installed_apps:
- myapp
Expand Down Expand Up @@ -158,7 +158,7 @@
published = cast(models.Field[Year, Year], models.IntegerField())
book = Book()
reveal_type(book.published) # N: Revealed type is "main.Year"
book.published = 2006 # E: Incompatible types in assignment (expression has type "int", variable has type "Year")
book.published = 2006 # E: Incompatible types in assignment (expression has type "int", variable has type "Year") [assignment]
book.published = Year(2006)
reveal_type(book.published) # N: Revealed type is "main.Year"
def accepts_int(arg: int) -> None: ...
Expand Down Expand Up @@ -190,9 +190,9 @@
from myapp.models import RenamedField
instance = RenamedField()
reveal_type(instance.modelname) # N: Revealed type is "builtins.int"
instance.fieldname # E: "RenamedField" has no attribute "fieldname"
instance.fieldname # E: "RenamedField" has no attribute "fieldname" [attr-defined]
instance.modelname = 1
instance.fieldname = 1 # E: "RenamedField" has no attribute "fieldname"
instance.fieldname = 1 # E: "RenamedField" has no attribute "fieldname" [attr-defined]
installed_apps:
- myapp
files:
Expand Down
4 changes: 2 additions & 2 deletions tests/typecheck/fields/test_nullable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from myapp.models import MyModel
reveal_type(MyModel().text) # N: Revealed type is "builtins.str"
reveal_type(MyModel().text_nullable) # N: Revealed type is "Union[builtins.str, None]"
MyModel().text = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]")
MyModel().text = None # E: Incompatible types in assignment (expression has type "None", variable has type "Union[str, int, Combinable]") [assignment]
MyModel().text_nullable = None
installed_apps:
- myapp
Expand Down Expand Up @@ -69,7 +69,7 @@
main: |
from myapp.models import Publisher, Book
reveal_type(Book().publisher) # N: Revealed type is "Union[myapp.models.Publisher, None]"
Book().publisher = 11 # E: Incompatible types in assignment (expression has type "int", variable has type "Union[Publisher, Combinable, None]")
Book().publisher = 11 # E: Incompatible types in assignment (expression has type "int", variable has type "Union[Publisher, Combinable, None]") [assignment]
installed_apps:
- myapp
files:
Expand Down
Loading