File: test_init.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 (345 lines) | stat: -rw-r--r-- 13,643 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
-   case: arguments_to_init_unexpected_attributes
    main: |
        from myapp.models import MyUser
        user = MyUser(name=1, age=12)
    out: |
        main:2: error: Unexpected attribute "name" for model "MyUser"  [misc]
        main:2: error: Unexpected attribute "age" 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

-   case: plain_function_which_returns_model
    main: |
        from myapp.models import MyUser
        def func(i: int) -> MyUser:
            pass
        func("hello")  # E: Argument 1 to "func" has incompatible type "str"; expected "int"  [arg-type]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyUser(models.Model):
                    pass

-   case: arguments_to_init_from_class_incompatible_type
    main: |
        from myapp.models import MyUser
        user = MyUser(name='hello', age=[])
    out: |
        main:2: error: Incompatible type for "age" of "MyUser" (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 MyUser(models.Model):
                    name = models.CharField(max_length=100)
                    age = models.IntegerField()

-   case: arguments_to_init_combined_from_base_classes
    main: |
        from myapp.models import BaseUser, ChildUser
        user = ChildUser(name='Max', age=12, lastname='Lastname')
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models

                class BaseUser(models.Model):
                    name = models.CharField(max_length=100)
                    age = models.IntegerField()
                class ChildUser(BaseUser):
                    lastname = models.CharField(max_length=100)

-   case: fields_from_abstract_user_propagate_to_init
    main: |
        from myapp.models import MyUser
        user = MyUser(name='Maxim', number=1)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class AbstractUser(models.Model):
                    class Meta:
                        abstract = True
                    name = models.CharField(max_length=100)
                class MyUser(AbstractUser):
                    number = models.IntegerField()

-   case: pk_refers_to_primary_key_and_could_be_passed_to_init
    main: |
        from myapp.models import MyUser1, MyUser2
        user2 = MyUser1(pk='hello')
        user3 = MyUser2(pk=1, name='maxim')
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models

                class MyUser1(models.Model):
                    mypk = models.CharField(primary_key=True)
                class MyUser2(models.Model):
                    name = models.CharField(max_length=100)

-   case: typechecking_of_pk
    main: |
        from myapp.models import MyUser1
        user = MyUser1(pk=[])  # E: Incompatible type for "pk" of "MyUser1" (got "list[Any]", expected "float | int | str | Combinable | None")  [misc]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models

                class MyUser1(models.Model):
                    mypk = models.IntegerField(primary_key=True)

-   case: set_foreign_key_by_its_primary_key
    main: |
        from datetime import datetime
        now = datetime.now()

        from myapp.models import Publisher, PublisherDatetime, Book
        Book(publisher_id=1, publisher_dt_id=now)
        Book(publisher_id=[], publisher_dt_id=now)  # E: Incompatible type for "publisher_id" of "Book" (got "list[Any]", expected "Combinable | int | str")  [misc]
        Book(publisher_id=1, publisher_dt_id=1)  # E: Incompatible type for "publisher_dt_id" of "Book" (got "int", expected "str | datetime | date | Combinable")  [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 PublisherDatetime(models.Model):
                    dt_pk = models.DateTimeField(primary_key=True)
                class Book(models.Model):
                    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
                    publisher_dt = models.ForeignKey(PublisherDatetime, on_delete=models.CASCADE)

-   case: setting_value_to_an_array_of_ints
    main: |
        from myapp.models import MyModel
        array_val: tuple[int, ...] = (1,)
        MyModel(array=array_val)
        array_val2: list[int] = [1]
        MyModel(array=array_val2)
        class NotAValid:
            pass
        array_val3: list[NotAValid] = [NotAValid()]
        MyModel(array=array_val3)  # E: Incompatible type for "array" of "MyModel" (got "list[NotAValid]", expected "Sequence[float | int | str] | Combinable")  [misc]
        non_init = MyModel()
        non_init.array = array_val
        non_init.array = array_val2
        non_init.array = array_val3  # E: Incompatible types in assignment (expression has type "list[NotAValid]", variable has type "Sequence[float | int | str] | Combinable")  [assignment]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                from django.contrib.postgres.fields import ArrayField

                class MyModel(models.Model):
                    array = ArrayField(base_field=models.IntegerField())

-   case: if_no_explicit_primary_key_id_can_be_passed
    main: |
        from myapp.models import MyModel
        MyModel(id=1, name='maxim')
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyModel(models.Model):
                    name = models.CharField(max_length=100)

-   case: arguments_can_be_passed_as_positionals
    main: |
        from myapp.models import MyModel, MyModel2
        MyModel(1)
        MyModel2(1, 12)
        MyModel2(1, [])  # E: Incompatible type for "name" of "MyModel2" (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 MyModel(models.Model):
                    pass
                class MyModel2(models.Model):
                    name = models.IntegerField()

-   case: charfield_with_integer_choices
    main: |
        from myapp.models import MyModel
        MyModel(day=1)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyModel(models.Model):
                    day = models.CharField(max_length=3, choices=((1, 'Fri'), (2, 'Sat')))

-   case: optional_id_fields_allowed_in_init
    main: |
        from myapp.models import Book, Publisher
        Book(id=None)
        Book(publisher=None)
        Book(publisher_id=None)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class Publisher(models.Model):
                    name = models.CharField(primary_key=True, max_length=100)
                class Book(models.Model):
                    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, null=True)


-   case: init_in_abstract_model_classmethod_should_not_throw_error_for_valid_fields
    main: |
        from myapp.models import MyModel
        MyModel.base_init()
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                from typing_extensions import Self
                class AbstractModel(models.Model):
                    class Meta:
                        abstract = True
                    text = models.CharField(max_length=100)
                    @classmethod
                    def base_init(cls) -> Self:
                        return cls(text='mytext')
                class MyModel(AbstractModel):
                    pass


-   case: field_set_type_honors_type_redefinition
    main: |
        from typing_extensions import reveal_type
        from myapp.models import MyModel
        non_init = MyModel()
        reveal_type(non_init.redefined_set_type)
        reveal_type(non_init.redefined_union_set_type)
        reveal_type(non_init.redefined_array_set_type)
        reveal_type(non_init.default_set_type)
        reveal_type(non_init.unset_set_type)
        non_init.redefined_set_type = "invalid"
        non_init.redefined_union_set_type = "invalid"
        array_val: list[str] = ["invalid"]
        non_init.redefined_array_set_type = array_val
        non_init.default_set_type = []
        non_init.unset_set_type = []
        MyModel(
            redefined_set_type="invalid",
            redefined_union_set_type="invalid",
            redefined_array_set_type=33,
            default_set_type=[],
            unset_set_type=[],
        )
    out: |
        main:4: note: Revealed type is "builtins.int"
        main:5: note: Revealed type is "builtins.int"
        main:6: note: Revealed type is "builtins.list[builtins.int]"
        main:7: note: Revealed type is "builtins.int"
        main:8: note: Revealed type is "Any"
        main:9: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
        main:10: error: Incompatible types in assignment (expression has type "str", variable has type "int | float")  [assignment]
        main:12: error: Incompatible types in assignment (expression has type "list[str]", variable has type "Sequence[int | float]")  [assignment]
        main:13: error: Incompatible types in assignment (expression has type "list[Never]", variable has type "float | int | str | Combinable")  [assignment]
        main:15: error: Incompatible type for "redefined_set_type" of "MyModel" (got "str", expected "int")  [misc]
        main:15: error: Incompatible type for "redefined_union_set_type" of "MyModel" (got "str", expected "int | float")  [misc]
        main:15: error: Incompatible type for "redefined_array_set_type" of "MyModel" (got "int", expected "Sequence[int | float]")  [misc]
        main:15: error: Incompatible type for "default_set_type" of "MyModel" (got "list[Any]", expected "float | int | str | Combinable")  [misc]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.contrib.postgres.fields import ArrayField
                from django.db import models
                from collections.abc import Sequence
                from typing import cast

                class MyModel(models.Model):
                    redefined_set_type = cast("models.Field[int, int]", models.IntegerField())
                    redefined_union_set_type = cast("models.Field[int | float, int]", models.IntegerField())
                    redefined_array_set_type = cast(
                        "ArrayField[Sequence[int | float], list[int]]",
                        ArrayField(base_field=models.IntegerField()),
                    )
                    default_set_type = models.IntegerField()
                    unset_set_type = cast("models.Field", models.IntegerField())

-   case: too_many_positional_arguments_on_init
    main: |
        from myapp.models import MyUser
        default_args: list[int]
        default_kwargs: dict[str, int]

        MyUser(1, "BB")  # E: Too many arguments for "MyUser"  [call-arg]
        MyUser("bbb", id=2)  # E: Too many arguments for "MyUser"  [call-arg]
        MyUser(1, "BB", *default_args)  # E: Too many arguments for "MyUser"  [call-arg]
        MyUser(1, "BB", **default_kwargs)  # E: Too many arguments for "MyUser"  [call-arg]

        # Should be ok because we cannot easily determine arg count
        MyUser(*"BB")
        MyUser(1, *["BB"])
        MyUser(1, *("BB",))
        MyUser(1, *default_args)
        MyUser(1, **default_kwargs)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyUser(models.Model):
                    pass