File: test_inheritance.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 (156 lines) | stat: -rw-r--r-- 5,421 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
-   case: test_meta_nested_class_allows_subclassing_in_multiple_inheritance
    main: |
        from typing import Any
        from django.db import models
        class Mixin1(models.Model):
            class Meta:
                abstract = True
        class Mixin2(models.Model):
            class Meta:
                abstract = True
        class User(Mixin1, Mixin2):
            pass

-   case: test_inheritance_from_abstract_model_does_not_fail_if_field_with_id_exists
    main: |
        from django.db import models
        class Abstract(models.Model):
            class Meta:
                abstract = True
        class User(Abstract):
            id = models.AutoField(primary_key=True)

-   case: test_typechecking_for_model_subclasses
    main: |
        from myapp.models import A, B, C
        def service(a: A) -> int:
            pass
        b_instance = B()
        service(b_instance)  # E: Argument 1 to "service" has incompatible type "B"; expected "A"  [arg-type]
        a_instance = A()
        c_instance = C()
        service(a_instance)
        service(c_instance)
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class A(models.Model):
                    pass
                class B(models.Model):
                    b_attr = 1
                    pass
                class C(A):
                    pass

-   case: fail_if_no_such_attribute_on_model
    main: |
        from typing_extensions import reveal_type
        from myapp.models import B
        b_instance = B()
        reveal_type(b_instance.b_attr) # N: Revealed type is "builtins.int"

        b_instance.non_existent_attribute  # E: "B" has no attribute "non_existent_attribute"  [attr-defined]
        b_instance.non_existent_attribute = 2  # E: "B" has no attribute "non_existent_attribute"  [attr-defined]
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class B(models.Model):
                    b_attr = 1
                    pass


-   case: fields_recognized_if_base_model_is_subclass_of_models_model
    main: |
        from typing_extensions import reveal_type
        from myapp.models import User
        reveal_type(User().username)  # N: Revealed type is "builtins.str"
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                from myapp.utils import MyBaseModel
                class User(MyBaseModel):
                    username = models.CharField(max_length=100)
        -   path: myapp/utils.py
            content: |
                from django.db.models import Model
                class MyBaseModel(Model):
                    pass


-   case: django_contrib_gis_base_model_mixin_inheritance
    main: |
        from typing_extensions import reveal_type
        from myapp.models import User
        reveal_type(User().name)  # N: Revealed type is "builtins.str"
        reveal_type(User().updated_at)  # N: Revealed type is "datetime.datetime"
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                from django.contrib.gis.db import models as gis_models
                class Mixin1(gis_models.Model):
                    class Meta:
                        abstract = True
                class Mixin2(gis_models.Model):
                    updated_at = models.DateTimeField(auto_now=True)
                    class Meta:
                        abstract = True
                class User(Mixin1, Mixin2):
                    name = models.TextField()

-   case: test_manager_typevar_through_bounds
    main: |
        from typing_extensions import reveal_type
        from myapp.models import ResultProcessorConcrete
        reveal_type(ResultProcessorConcrete().f())  # N: Revealed type is "myapp.models.Concrete"
    installed_apps:
        -   myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from __future__ import annotations

                from django.db.models import Model
                from django.db.models.manager import Manager
                from typing import ClassVar, Generic, TypeVar
                from typing_extensions import Self

                M = TypeVar("M", bound=Model, covariant=True)

                class BaseManager(Manager[M]): ...

                class Base(Model):
                    custom_objects: ClassVar[BaseManager[Self]] = BaseManager()

                class Bound(Base): pass

                T = TypeVar("T", bound=Bound)

                class ResultProcessorBase(Generic[T]):
                    @property
                    def model_cls(self) -> type[T]:
                        raise NotImplementedError

                    def f(self) -> T:
                        return self.model_cls.custom_objects.get()

                class Concrete(Bound): pass

                class ResultProcessorConcrete(ResultProcessorBase[Concrete]):
                    pass