File: test_nullable.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 (107 lines) | stat: -rw-r--r-- 4,135 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
-   case: autofield_can_be_set_to_none
    main: |
        from myapp.models import MyModel, MyModelExplicitPK
        m = MyModel()
        m.id = 3
        m.id = None
        m2 = MyModel(id=None)
        MyModel.objects.create(id=None)
        MyModel.objects.all().update(id=None) # Should give an error since there's a not-null constraint

        def foo(a: int) -> bool:
          return True
        m2 = MyModel()
        foo(m2.id)

        # At runtime, this would be an error, unless m.save() was called to populate the `id` field.
        # but the plugin cannot catch this.
        foo(m.id)

        exp = MyModelExplicitPK()
        exp.id = 3
        exp.id = None
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyModel(models.Model):
                    pass
                class MyModelExplicitPK(models.Model):
                    id = models.AutoField(primary_key=True)
-   case: nullable_field_with_strict_optional_true
    main: |
        from typing_extensions import reveal_type
        from myapp.models import MyModel
        reveal_type(MyModel().text)  # N: Revealed type is "builtins.str"
        reveal_type(MyModel().text_nullable)  # N: Revealed type is "builtins.str | None"
        MyModel().text = None  # E: Incompatible types in assignment (expression has type "None", variable has type "str | int | Combinable")  [assignment]
        MyModel().text_nullable = None
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyModel(models.Model):
                    text_nullable = models.CharField(max_length=100, null=True)
                    text = models.CharField(max_length=100)

-   case: nullable_array_field
    main: |
        from typing_extensions import reveal_type
        from myapp.models import MyModel
        reveal_type(MyModel().lst)  # N: Revealed type is "builtins.list[builtins.str] | None"
    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):
                    lst = ArrayField(base_field=models.CharField(max_length=100), null=True)

-   case: nullable_foreign_key
    main: |
        from typing_extensions import reveal_type
        from myapp.models import Publisher, Book
        reveal_type(Book().publisher)  # N: Revealed type is "myapp.models.Publisher | None"
        Book().publisher = 11  # E: Incompatible types in assignment (expression has type "int", variable has type "Publisher | Combinable | None")  [assignment]
    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):
                    publisher = models.ForeignKey(to=Publisher, on_delete=models.CASCADE, null=True)

-   case: nullable_self_foreign_key
    main: |
        from typing_extensions import reveal_type
        from myapp.models import Inventory
        parent = Inventory()
        core = Inventory(parent_id=parent.id)
        reveal_type(core.parent_id)  # N: Revealed type is "builtins.int | None"
        reveal_type(core.parent)  # N: Revealed type is "myapp.models.Inventory | None"
        Inventory(parent=None)
        Inventory(parent_id=None)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class Inventory(models.Model):
                    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True)