File: test_create.yml

package info (click to toggle)
python-django-stubs 5.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,832 kB
  • sloc: python: 5,185; makefile: 15; sh: 8
file content (190 lines) | stat: -rw-r--r-- 8,004 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
-   case: default_manager_create_is_typechecked
    main: |
        from myapp.models import User
        User.objects.create(pk=1, name='Max', age=10)
        User.objects.create(age=[])  # E: Incompatible type for "age" of "User" (got "list[Any]", expected "float | int | str | Combinable")  [misc]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models

                class User(models.Model):
                    name = models.CharField(max_length=100)
                    age = models.IntegerField()

-   case: model_recognises_parent_attributes
    main: |
        from typing_extensions import reveal_type
        from myapp.models import Child
        c = Child.objects.create(name='Maxim', lastname='Maxim2')
        reveal_type(c.id)  # N: Revealed type is "builtins.int"
        reveal_type(c.name)  # N: Revealed type is "builtins.str"
        reveal_type(c.lastname)  # N: Revealed type is "builtins.str"
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models

                class Parent(models.Model):
                    name = models.CharField(max_length=100)
                class Child(Parent):
                    lastname = models.CharField(max_length=100)

-   case: deep_multiple_inheritance_with_create
    main: |
        from myapp.models import Child4
        Child4.objects.create(name1='n1', name2='n2', value=1, value4=4)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models

                class Parent1(models.Model):
                    name1 = models.CharField(max_length=50)
                class Parent2(models.Model):
                    id2 = models.AutoField(primary_key=True)
                    name2 = models.CharField(max_length=50)

                class Child1(Parent1, Parent2):
                    value = models.IntegerField()
                class Child4(Child1):
                    value4 = models.IntegerField()

-   case: mro_is_walked_for_field_annotations
    main: |
        from myapp.models import Child
        Child.objects.create(dct={"hello": "world"})  # no errors
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from __future__ import annotations

                from django.db import models

                class JSONField(models.TextField): pass  # incomplete

                class Base(models.Model):
                    dct: models.Field[dict[str, str], dict[str, str]] = JSONField()

                class Child(Base): pass

-   case: optional_id_fields_for_create_is_error_if_not_autofield
    main: |
        from myapp.models import Publisher, Book

        Book.objects.create(id=None)  # E: Incompatible type for "id" of "Book" (got "None", expected "float | int | str | Combinable")  [misc]
        Book.objects.create(publisher=None)  # E: Incompatible type for "publisher" of "Book" (got "None", expected "Publisher | Combinable")  [misc]
        Book.objects.create(publisher_id=None)  # E: Incompatible type for "publisher_id" of "Book" (got "None", expected "Combinable | int | str")  [misc]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class Publisher(models.Model):
                    pass
                class Book(models.Model):
                    id = models.IntegerField(primary_key=True)
                    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)

-   case: none_for_primary_key_is_allowed_if_field_is_autogenerated
    main: |
        from myapp.models import Book
        Book.objects.create(id=None)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class Book(models.Model):
                    pass

-   case: when_default_for_primary_key_is_specified_allow_none_to_be_set
    main: |
        from typing_extensions import reveal_type
        from myapp.models import MyModel
        first = MyModel(id=None)
        reveal_type(first.id)  # N: Revealed type is "builtins.int"
        first = MyModel.objects.create(id=None)
        reveal_type(first.id)  # N: Revealed type is "builtins.int"
        first = MyModel()
        first.id = None
        reveal_type(first.id)  # N: Revealed type is "builtins.int"

        from myapp.models import MyModel2
        MyModel2(id=None)  # E: Incompatible type for "id" of "MyModel2" (got "None", expected "float | int | str | Combinable")  [misc]
        MyModel2.objects.create(id=None)  # E: Incompatible type for "id" of "MyModel2" (got "None", expected "float | int | str | Combinable")  [misc]
        second = MyModel2()
        second.id = None  # E: Incompatible types in assignment (expression has type "None", variable has type "float | int | str | Combinable")  [assignment]
        reveal_type(second.id)  # N: Revealed type is "builtins.int"

        # default set but no primary key doesn't allow None
        from myapp.models import MyModel3
        MyModel3(default=None)  # E: Incompatible type for "default" of "MyModel3" (got "None", expected "float | int | str | Combinable")  [misc]
        MyModel3.objects.create(default=None)  # E: Incompatible type for "default" of "MyModel3" (got "None", expected "float | int | str | Combinable")  [misc]
        third = MyModel3()
        third.default = None  # E: Incompatible types in assignment (expression has type "None", variable has type "float | int | str | Combinable")  [assignment]
        reveal_type(third.default)  # N: Revealed type is "builtins.int"
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                def return_int() -> int:
                    return 0
                class MyModel(models.Model):
                    id = models.IntegerField(primary_key=True, default=return_int)
                class MyModel2(models.Model):
                    id = models.IntegerField(primary_key=True)
                class MyModel3(models.Model):
                    default = models.IntegerField(default=return_int)

-   case: default_manager_acreate_is_typechecked
    main: |
        import asyncio
        from typing_extensions import reveal_type
        from myapp.models import User
        async def amain() -> None:
            reveal_type(await User.objects.acreate(pk=1, name='Max', age=10))  # N: Revealed type is "myapp.models.User"
            await User.objects.acreate(age=[])  # E: Incompatible type for "age" of "User" (got "list[Any]", expected "float | int | str | Combinable")  [misc]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class User(models.Model):
                    name = models.CharField(max_length=100)
                    age = models.IntegerField()

-   case: too_many_keyword_arguments_on_create
    main: |
        from myapp.models import MyUser
        MyUser.objects.create(id=1, foo="bbb")  # E: Unexpected attribute "foo" for model "MyUser"  [misc]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyUser(models.Model):
                    pass