Skip to content

Commit deb5da9

Browse files
committed
Rename QueryType to QuerySetType. Make django_unicorn.components exports explicit.
1 parent 1e24553 commit deb5da9

File tree

9 files changed

+41
-31
lines changed

9 files changed

+41
-31
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .types import *
2-
from .unicorn_view import *
3-
from .updaters import *
1+
# Explicitly expose what should be available when importing from `django_unicorn.component`
2+
from .typing import QuerySetType
3+
from .unicorn_view import UnicornField, UnicornView
4+
from .updaters import HashUpdate, LocationUpdate, PollUpdate

django_unicorn/components/types.py

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import Generic, Iterator, TypeVar
2+
3+
from django.db.models import Model, QuerySet
4+
5+
6+
M_co = TypeVar("M_co", bound=Model, covariant=True)
7+
8+
9+
class QuerySetType(Generic[M_co], QuerySet):
10+
"""
11+
Type for QuerySet that can be used for a typehint in components.
12+
"""
13+
14+
# This is based on https://github.com/Vieolo/django-hint/blob/97e22bf/django_hint/typehint.py#L167,
15+
# although https://github.com/typeddjango/django-stubs/blob/2a732fd/django-stubs/db/models/manager.pyi#L28
16+
# might be a better long-term solution.
17+
18+
def __iter__(self) -> Iterator[M_co]:
19+
...

django_unicorn/views/utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from django.db.models import Model, QuerySet
66

7-
from django_unicorn.components import QueryType, UnicornField, UnicornView
7+
from django_unicorn.components import UnicornField, UnicornView
8+
from django_unicorn.components.typing import QuerySetType
89
from django_unicorn.decorators import timed
910
from django_unicorn.utils import get_type_hints
1011

@@ -138,21 +139,21 @@ def _is_queryset(field, type_hint, value):
138139

139140
return (
140141
isinstance(field, QuerySet)
141-
or (type_hint and get_origin(type_hint) is QueryType)
142+
or (type_hint and get_origin(type_hint) is QuerySetType)
142143
) and isinstance(value, list)
143144

144145

145146
def _create_queryset(field, type_hint, value) -> QuerySet:
146147
"""
147-
Create a queryset based on the `value`. If needed, the queryset will be created based on the `QueryType`.
148+
Create a queryset based on the `value`. If needed, the queryset will be created based on the `QuerySetType`.
148149
149150
For example, all of these ways fields are equivalent:
150151
151152
```
152153
class TestComponent(UnicornView):
153-
queryset_with_empty_list: QueryType[SomeModel] = []
154-
queryset_with_none: QueryType[SomeModel] = None
155-
queryset_with_empty_queryset: QueryType[SomeModel] = SomeModel.objects.none()
154+
queryset_with_empty_list: QuerySetType[SomeModel] = []
155+
queryset_with_none: QuerySetType[SomeModel] = None
156+
queryset_with_empty_queryset: QuerySetType[SomeModel] = SomeModel.objects.none()
156157
queryset_with_no_typehint = SomeModel.objects.none()
157158
```
158159

example/unicorn/components/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from django_unicorn.components import QueryType, UnicornView
1+
from django_unicorn.components import QuerySetType, UnicornView
22
from example.coffee.models import Flavor
33

44

55
class ModelsView(UnicornView):
66
flavor: Flavor = Flavor()
7-
flavors: QueryType[Flavor] = Flavor.objects.none()
7+
flavors: QuerySetType[Flavor] = Flavor.objects.none()
88

99
def mount(self):
1010
self.refresh_flavors()

tests/components/test_convert_to_pascal_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django_unicorn.components import convert_to_pascal_case
1+
from django_unicorn.components.unicorn_view import convert_to_pascal_case
22

33

44
def test_convert_to_pascal_case():

tests/components/test_convert_to_snake_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django_unicorn.components import convert_to_snake_case
1+
from django_unicorn.components.unicorn_view import convert_to_snake_case
22

33

44
def test_convert_to_snake_case():

tests/components/test_get_locations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from django_unicorn.components import get_locations
3+
from django_unicorn.components.unicorn_view import get_locations
44

55

66
def test_get_locations_kebab_case():

tests/views/utils/test_set_property_from_data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from django_unicorn.components import QueryType, UnicornView
8+
from django_unicorn.components import QuerySetType, UnicornView
99
from django_unicorn.views.utils import set_property_from_data
1010
from example.coffee.models import Flavor
1111

@@ -17,11 +17,11 @@ class FakeComponent(UnicornView):
1717
array: List[str] = []
1818
model = Flavor(name="test-initial")
1919
queryset = Flavor.objects.none()
20-
queryset_with_typehint: QueryType[Flavor] = []
20+
queryset_with_typehint: QuerySetType[Flavor] = []
2121

2222

2323
class FakeQuerySetComponent(UnicornView):
24-
queryset_with_typehint: QueryType[Flavor] = None
24+
queryset_with_typehint: QuerySetType[Flavor] = None
2525

2626

2727
class FakeDbComponent(UnicornView):
@@ -35,9 +35,9 @@ def __init__(self, *args, **kwargs):
3535

3636

3737
class FakeAllQuerySetComponent(UnicornView):
38-
queryset_with_empty_list: QueryType[Flavor] = []
39-
queryset_with_none: QueryType[Flavor] = None
40-
queryset_with_empty_queryset: QueryType[Flavor] = Flavor.objects.none()
38+
queryset_with_empty_list: QuerySetType[Flavor] = []
39+
queryset_with_none: QuerySetType[Flavor] = None
40+
queryset_with_empty_queryset: QuerySetType[Flavor] = Flavor.objects.none()
4141
queryset_with_no_typehint = Flavor.objects.none()
4242

4343

0 commit comments

Comments
 (0)