Skip to content

Commit e8d9041

Browse files
committed
Introduce DjangoModelType
1 parent e4104ad commit e8d9041

File tree

4 files changed

+14
-26
lines changed

4 files changed

+14
-26
lines changed

styleguide_example/common/services.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
from typing import List, Dict, Any, Tuple
22

3+
from styleguide_example.common.types import DjangoModelType
4+
35

4-
# TODO: Decide typing for the `instance` argument
56
def model_update(
67
*,
7-
instance: Any,
8+
instance: DjangoModelType,
89
fields: List[str],
910
data: Dict[str, Any]
10-
) -> Tuple[Any, bool]:
11+
) -> Tuple[DjangoModelType, bool]:
1112
"""
1213
Generic update service meant to be reused in local update services
1314
1415
For example:
1516
16-
def user_update(*, user: User, **data) -> User:
17+
def user_update(*, user: User, data) -> User:
1718
fields = ['first_name', 'last_name']
18-
user = model_update(instance=user, fields=fields, data=data)
19+
user, has_updated = model_update(instance=user, fields=fields, data=data)
1920
2021
// Do other actions with the user here
2122
@@ -27,9 +28,6 @@ def user_update(*, user: User, **data) -> User:
2728
"""
2829
has_updated = False
2930

30-
if instance is None:
31-
return instance, has_updated
32-
3331
for field in fields:
3432
# Skip if a field is not present in the actual data
3533
if field not in data:

styleguide_example/common/tests/services/test_model_update.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,6 @@ def setUp(self):
1212
field_c=None
1313
)
1414

15-
def test_model_update_does_not_update_if_instance_is_none(self):
16-
updated_instance, has_updated = model_update(
17-
instance=self.instance,
18-
fields=[],
19-
data={}
20-
)
21-
22-
self.assertEqual(updated_instance, self.instance)
23-
self.assertFalse(has_updated)
24-
25-
self.assertIsNone(updated_instance.field_a)
26-
self.assertIsNone(updated_instance.field_b)
27-
self.assertIsNone(updated_instance.field_c)
28-
29-
self.instance.full_clean.assert_not_called()
30-
self.instance.save.assert_not_called()
31-
3215
def test_model_update_does_not_update_if_none_of_the_fields_are_in_the_data(self):
3316
update_fields = ['non_existing_field']
3417
data = {'field_a': 'value_a'}

styleguide_example/common/types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import TypeVar
2+
3+
from django.db import models
4+
5+
# Generic type for a Django model
6+
# Reference: https://mypy.readthedocs.io/en/stable/kinds_of_types.html#the-type-of-class-objects
7+
DjangoModelType = TypeVar('DjangoModelType', bound=models.Model)

styleguide_example/users/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def user_create(
2525

2626

2727
@transaction.atomic
28-
def user_update(*, user: BaseUser, **data) -> BaseUser:
28+
def user_update(*, user: BaseUser, data) -> BaseUser:
2929
non_side_effect_fields = ['first_name', 'last_name']
3030

3131
user, has_updated = model_update(

0 commit comments

Comments
 (0)