Skip to content

Commit c437ff3

Browse files
authored
Enable mypy strict mode (#2517)
1 parent c1ff7b3 commit c437ff3

File tree

5 files changed

+39
-31
lines changed

5 files changed

+39
-31
lines changed

mypy.ini

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
# Regular configuration file (can be used as base in other projects, runs in CI)
22

33
[mypy]
4+
# Modified in `tests.yml`
5+
incremental = true
6+
7+
# Strictness:
48
allow_redefinition = true
59
check_untyped_defs = true
10+
# TODO: add type args to all generics
11+
disallow_any_generics = false
12+
# TODO: fix `Any` subclassing in `typeshed/builtins.pyi`
13+
disallow_subclassing_any = false
614
ignore_missing_imports = false
7-
incremental = true
8-
strict_optional = true
9-
show_traceback = true
10-
warn_unused_ignores = true
11-
warn_redundant_casts = true
12-
warn_unused_configs = true
15+
strict = true
16+
strict_bytes = true
17+
local_partial_types = true
1318
warn_unreachable = true
14-
disallow_untyped_defs = true
15-
disallow_incomplete_defs = true
16-
disable_error_code = empty-body
19+
1720
# TODO: update our output assertions to match a new syntax
1821
force_uppercase_builtins = true
1922
force_union_syntax = true
2023

24+
disable_error_code = empty-body
2125
enable_error_code = deprecated
2226

27+
show_traceback = true
2328

2429
plugins =
2530
mypy_django_plugin.main,
2631
mypy.plugins.proper_plugin
2732

28-
# Ignore incomplete hints in yaml-stubs
33+
# Ignore incomplete hints in 3rd party stubs:
2934
[mypy-yaml.*]
3035
disallow_untyped_defs = false
3136
disallow_incomplete_defs = false
3237

3338
[mypy-cryptography.*]
3439
ignore_errors = true
3540

41+
# Our settings:
3642
[mypy.plugins.django-stubs]
3743
django_settings_module = scripts.django_tests_settings

tests/typecheck/fields/test_related.yml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
- path: myapp/__init__.py
228228
- path: myapp/models/__init__.py
229229
content: |
230-
from .app import App
230+
from .app import App as App
231231
- path: myapp/models/app.py
232232
content: |
233233
from django.db import models
@@ -254,7 +254,7 @@
254254
- path: myapp/__init__.py
255255
- path: myapp/models/__init__.py
256256
content: |
257-
from .user import User
257+
from .user import User as User
258258
- path: myapp/models/user.py
259259
content: |
260260
from django.db import models
@@ -279,9 +279,9 @@
279279
- path: myapp/__init__.py
280280
- path: myapp/models/__init__.py
281281
content: |
282-
from .user import User
283-
from .profile import Profile
284-
from .app import App
282+
from .user import User as User
283+
from .profile import Profile as Profile
284+
from .app import App as App
285285
- path: myapp/models/user.py
286286
content: |
287287
from django.db import models
@@ -466,8 +466,8 @@
466466
- path: myapp/__init__.py
467467
- path: myapp/models/__init__.py
468468
content: |
469-
from .publisher import Publisher
470-
from .book import Book
469+
from .publisher import Publisher as Publisher
470+
from .book import Book as Book
471471
- path: myapp/models/publisher.py
472472
content: |
473473
from django.db import models
@@ -489,7 +489,7 @@
489489
- path: myapp/__init__.py
490490
- path: myapp/models/__init__.py
491491
content: |
492-
from .book import Book
492+
from .book import Book as Book
493493
- path: myapp/models/publisher.py
494494
content: |
495495
from django.db import models
@@ -836,7 +836,8 @@
836836
reveal_type(Order().products) # N: Revealed type is "myapp.models.Product_RelatedManager"
837837
reveal_type(Order().products.get()) # N: Revealed type is "myapp.models.Product"
838838
reveal_type(Order().products.queryset_method()) # N: Revealed type is "builtins.int"
839-
if 1 == 2:
839+
cond: bool
840+
if cond:
840841
manager = User().products
841842
else:
842843
manager = Order().products
@@ -1338,8 +1339,8 @@
13381339
- path: myapp/__init__.py
13391340
- path: myapp/models/__init__.py
13401341
content: |
1341-
from .child import Child
1342-
from .parent import Parent
1342+
from .child import Child as Child
1343+
from .parent import Parent as Parent
13431344
- path: myapp/models/parent.py
13441345
content: |
13451346
from django.db import models

tests/typecheck/managers/querysets/test_annotate.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111
print(annotated_user.asdf) # E: "User@AnnotatedWith[TypedDict({'foo': Any})]" has no attribute "asdf" [attr-defined]
1212
print(unannotated_user.asdf) # E: "User" has no attribute "asdf" [attr-defined]
1313
14-
def func(user: Annotated[User, Annotations]) -> str: # E: Missing required TypedDict parameter for generic type Annotations [misc]
15-
return user.asdf # E: "User" has no attribute "asdf" [attr-defined]
14+
def func(user: Annotated[User, Annotations]) -> None: # E: Missing required TypedDict parameter for generic type Annotations [misc]
15+
user.asdf # E: "User" has no attribute "asdf" [attr-defined]
1616
1717
# Due to the error on 'func' it should fall back to expecting 'User'
18-
reveal_type(func) # N: Revealed type is "def (user: myapp.models.User) -> builtins.str"
18+
reveal_type(func) # N: Revealed type is "def (user: myapp.models.User)"
1919
func(unannotated_user)
2020
func(annotated_user)
2121
22-
def func2(user: WithAnnotations[User]) -> str:
23-
return user.asdf # E: "User" has no attribute "asdf" [attr-defined]
22+
def func2(user: WithAnnotations[User]) -> None:
23+
user.asdf # E: "User" has no attribute "asdf" [attr-defined]
2424
2525
# With only one argument to 'WithAnnotations' it should fall back to expecting 'User'
26-
reveal_type(func2) # N: Revealed type is "def (user: myapp.models.User) -> builtins.str"
26+
reveal_type(func2) # N: Revealed type is "def (user: myapp.models.User)"
2727
func2(unannotated_user)
2828
func2(annotated_user)
2929
30-
def func3(user: WithAnnotations) -> str: ...
30+
def func3(user: WithAnnotations) -> None: ...
3131
3232
# With no arguments to 'WithAnnotations' it should become 'Any'
33-
reveal_type(func3) # N: Revealed type is "def (user: Any) -> builtins.str"
33+
reveal_type(func3) # N: Revealed type is "def (user: Any)"
3434
func3(unannotated_user)
3535
func3(annotated_user)
3636
installed_apps:

tests/typecheck/managers/test_managers.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
- path: myapp/models/__init__.py
157157
content: |
158158
from django.db import models
159-
from .main import Inventory
159+
from .main import Inventory as Inventory
160160
class Band(models.Model):
161161
pass
162162
- path: myapp/models/main.py

tests/typecheck/test_forms.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
8484
class BrokenArticleChoiceField(forms.ModelChoiceField[Article]):
8585
def label_from_instance(self, obj: Article) -> str:
86-
return obj.title # E: "Article" has no attribute "title" [attr-defined]
86+
obj.title # E: "Article" has no attribute "title" [attr-defined]
87+
return 'a'
8788
8889
class ArticleMultipleChoiceField(forms.ModelMultipleChoiceField[Article]):
8990
def label_from_instance(self, obj: Article) -> str:

0 commit comments

Comments
 (0)