Skip to content

Commit 8eac958

Browse files
committed
Improve typing
1 parent ac7878f commit 8eac958

18 files changed

+82
-66
lines changed

backend/app/controller/household/household_controller.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def updateHousehold(args, household_id):
111111
@household.route("/<int:household_id>", methods=["DELETE"])
112112
@jwt_required()
113113
@authorize_household(required=RequiredRights.ADMIN)
114-
def deleteHouseholdById(household_id):
114+
def deleteHouseholdById(household_id: int):
115115
hms = HouseholdMember.find_by_household(household_id)
116116
for hm in hms:
117117
db.session.delete(hm)
@@ -124,7 +124,7 @@ def deleteHouseholdById(household_id):
124124
@jwt_required()
125125
@authorize_household(required=RequiredRights.ADMIN)
126126
@validate_args(UpdateHouseholdMember)
127-
def putHouseholdMember(args, household_id, user_id):
127+
def putHouseholdMember(args, household_id: int, user_id: int):
128128
hm = HouseholdMember.find_by_ids(household_id, user_id)
129129
if not hm:
130130
household = Household.find_by_id(household_id)
@@ -145,7 +145,7 @@ def putHouseholdMember(args, household_id, user_id):
145145
@household.route("/<int:household_id>/member/<int:user_id>", methods=["DELETE"])
146146
@jwt_required()
147147
@authorize_household(required=RequiredRights.ADMIN_OR_SELF)
148-
def deleteHouseholdMember(household_id, user_id):
148+
def deleteHouseholdMember(household_id: int, user_id: int):
149149
hm = HouseholdMember.find_by_ids(household_id, user_id)
150150
if hm:
151151
hm.delete()

backend/app/models/association.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ class Association(Model):
4141
)
4242

4343
@classmethod
44-
def create(cls, antecedent_id, consequent_id, support, confidence, lift):
44+
def create(
45+
cls,
46+
antecedent_id: int,
47+
consequent_id: int,
48+
support: float,
49+
confidence: float,
50+
lift: float,
51+
) -> Self:
4552
return cls(
4653
antecedent_id=antecedent_id,
4754
consequent_id=consequent_id,
@@ -51,7 +58,7 @@ def create(cls, antecedent_id, consequent_id, support, confidence, lift):
5158
).save()
5259

5360
@classmethod
54-
def find_by_antecedent(cls, antecedent_id):
61+
def find_by_antecedent(cls, antecedent_id: int):
5562
return cls.query.filter(cls.antecedent_id == antecedent_id).order_by(
5663
cls.lift.desc()
5764
)

backend/app/models/category.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Self, List, TYPE_CHECKING, cast
2+
from typing import Any, Self, List, TYPE_CHECKING, cast
33
from app import db
44
from app.helpers import DbModelAuthorizeMixin
55
from sqlalchemy.orm import Mapped
@@ -39,8 +39,8 @@ class Category(Model, DbModelAuthorizeMixin):
3939
),
4040
)
4141

42-
def obj_to_full_dict(self) -> dict:
43-
res = super().obj_to_dict()
42+
def obj_to_full_dict(self) -> dict[str, Any]:
43+
res = self.obj_to_dict()
4444
return res
4545

4646
@classmethod
@@ -55,7 +55,7 @@ def all_by_ordering(cls, household_id: int):
5555
def create_by_name(
5656
cls,
5757
household_id: int,
58-
name,
58+
name: str,
5959
default: bool = False,
6060
default_key: str | None = None,
6161
) -> Self:

backend/app/models/expense.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from datetime import datetime
2-
from typing import Optional, Self, List, TYPE_CHECKING, cast
1+
from datetime import datetime, timezone
2+
from typing import Any, Optional, Self, List, TYPE_CHECKING, cast
33
from app import db
44
from app.helpers import DbModelAuthorizeMixin
55
from sqlalchemy.orm import Mapped
@@ -20,7 +20,7 @@ class Expense(Model, DbModelAuthorizeMixin):
2020
amount: Mapped[float] = db.Column(db.Float())
2121
description: Mapped[str] = db.Column(db.String)
2222
date: Mapped[datetime] = db.Column(
23-
db.DateTime, default=datetime.utcnow, nullable=False
23+
db.DateTime, default=lambda: datetime.now(timezone.utc), nullable=False
2424
)
2525
category_id: Mapped[int | None] = db.Column(
2626
db.Integer, db.ForeignKey("expense_category.id")
@@ -72,13 +72,13 @@ def obj_to_dict(
7272
self,
7373
skip_columns: list[str] | None = None,
7474
include_columns: list[str] | None = None,
75-
) -> dict:
75+
) -> dict[str, Any]:
7676
res = super().obj_to_dict(skip_columns, include_columns)
7777
if self.photo_file:
7878
res["photo_hash"] = self.photo_file.blur_hash
7979
return res
8080

81-
def obj_to_full_dict(self) -> dict:
81+
def obj_to_full_dict(self) -> dict[str, Any]:
8282
res = self.obj_to_dict()
8383
paidFor = (
8484
ExpensePaidFor.query.filter(ExpensePaidFor.expense_id == self.id)
@@ -91,8 +91,8 @@ def obj_to_full_dict(self) -> dict:
9191
res["category"] = self.category.obj_to_full_dict()
9292
return res
9393

94-
def obj_to_export_dict(self) -> dict:
95-
res = {
94+
def obj_to_export_dict(self) -> dict[str, Any]:
95+
res: dict[str, Any] = {
9696
"name": self.name,
9797
"amount": self.amount,
9898
"date": self.date,
@@ -107,11 +107,11 @@ def obj_to_export_dict(self) -> dict:
107107
return res
108108

109109
@classmethod
110-
def find_by_name(cls, name) -> Self | None:
110+
def find_by_name(cls, name: str) -> Self | None:
111111
return cls.query.filter(cls.name == name).first()
112112

113113
@classmethod
114-
def find_by_id(cls, id) -> Self | None:
114+
def find_by_id(cls, id: int) -> Self | None:
115115
return (
116116
cls.query.filter(cls.id == id).join(Expense.category, isouter=True).first()
117117
)
@@ -143,15 +143,15 @@ class ExpensePaidFor(Model):
143143
),
144144
)
145145

146-
def obj_to_user_dict(self):
146+
def obj_to_user_dict(self) -> dict[str, Any]:
147147
res = self.user.obj_to_dict()
148148
res["factor"] = getattr(self, "factor")
149149
res["created_at"] = getattr(self, "created_at")
150150
res["updated_at"] = getattr(self, "updated_at")
151151
return res
152152

153153
@classmethod
154-
def find_by_ids(cls, expense_id, user_id) -> Self | None:
154+
def find_by_ids(cls, expense_id: int, user_id: int) -> Self | None:
155155
return cls.query.filter(
156156
cls.expense_id == expense_id, cls.user_id == user_id
157157
).first()

backend/app/models/expense_category.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Self, List, TYPE_CHECKING, cast
2+
from typing import Any, Self, List, TYPE_CHECKING, cast
33
from app import db
44
from app.helpers import DbModelAuthorizeMixin
55
from sqlalchemy.orm import Mapped
@@ -38,11 +38,11 @@ class ExpenseCategory(Model, DbModelAuthorizeMixin):
3838
),
3939
)
4040

41-
def obj_to_full_dict(self) -> dict:
41+
def obj_to_full_dict(self) -> dict[str, Any]:
4242
res = super().obj_to_dict()
4343
return res
4444

45-
def obj_to_export_dict(self) -> dict:
45+
def obj_to_export_dict(self) -> dict[str, Any]:
4646
return {
4747
"name": self.name,
4848
"color": self.color,

backend/app/models/file.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ def isUnused(self) -> bool:
8282
and not self.profile_picture
8383
)
8484

85-
def checkAuthorized(self, requires_admin=False, household_id: int | None = None):
85+
def checkAuthorized(
86+
self, requires_admin: bool = False, household_id: int | None = None
87+
):
8688
if self.created_by and current_user and self.created_by == current_user.id:
8789
pass # created by user can access his pictures
8890
elif self.profile_picture:

backend/app/models/history.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Self, TYPE_CHECKING, cast
1+
from typing import Any, Self, TYPE_CHECKING, cast
22
from app import db
33
from .shoppinglist import ShoppinglistItems
44
from sqlalchemy import func
@@ -50,7 +50,9 @@ class History(Model):
5050
description: Mapped[str] = db.Column("description", db.String())
5151

5252
@classmethod
53-
def create_added_without_save(cls, shoppinglist, item, description="") -> Self:
53+
def create_added_without_save(
54+
cls, shoppinglist, item, description: str = ""
55+
) -> Self:
5456
return cls(
5557
shoppinglist_id=shoppinglist.id,
5658
item_id=item.id,
@@ -59,12 +61,12 @@ def create_added_without_save(cls, shoppinglist, item, description="") -> Self:
5961
)
6062

6163
@classmethod
62-
def create_added(cls, shoppinglist, item, description="") -> Self:
64+
def create_added(cls, shoppinglist, item, description: str = "") -> Self:
6365
return cls.create_added_without_save(shoppinglist, item, description).save()
6466

6567
@classmethod
6668
def create_dropped(
67-
cls, shoppinglist, item, description="", created_at=None
69+
cls, shoppinglist, item, description: str = "", created_at=None
6870
) -> Self:
6971
return cls(
7072
shoppinglist_id=shoppinglist.id,
@@ -74,7 +76,7 @@ def create_dropped(
7476
created_at=created_at,
7577
).save()
7678

77-
def obj_to_item_dict(self) -> dict:
79+
def obj_to_item_dict(self) -> dict[str, Any]:
7880
res = self.item.obj_to_dict()
7981
res["timestamp"] = getattr(self, "created_at")
8082
return res

backend/app/models/household.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Self, List, TYPE_CHECKING, cast
1+
from typing import Any, Self, List, TYPE_CHECKING, cast
22
from app import db
33
from app.helpers.db_list_type import DbListType
44
from sqlalchemy.orm import Mapped
@@ -35,7 +35,7 @@ class Household(Model):
3535
db.Boolean(), nullable=False, default=True
3636
)
3737

38-
view_ordering: Mapped[List] = db.Column(DbListType(), default=list())
38+
view_ordering: Mapped[List[str]] = db.Column(DbListType(), default=list())
3939

4040
items: Mapped[List["Item"]] = cast(
4141
Mapped[List["Item"]],
@@ -114,21 +114,21 @@ def obj_to_dict(
114114
self,
115115
skip_columns: list[str] | None = None,
116116
include_columns: list[str] | None = None,
117-
) -> dict:
117+
) -> dict[str, Any]:
118118
res = super().obj_to_dict(skip_columns, include_columns)
119119
res["member"] = [m.obj_to_user_dict() for m in getattr(self, "member")]
120120
res["default_shopping_list"] = self.shoppinglists[0].obj_to_dict()
121121
if self.photo_file:
122122
res["photo_hash"] = self.photo_file.blur_hash
123123
return res
124124

125-
def obj_to_public_dict(self) -> dict:
125+
def obj_to_public_dict(self) -> dict[str, Any]:
126126
res = super().obj_to_dict(include_columns=["id", "name", "photo", "language"])
127127
if self.photo_file:
128128
res["photo_hash"] = self.photo_file.blur_hash
129129
return res
130130

131-
def obj_to_export_dict(self) -> dict:
131+
def obj_to_export_dict(self) -> dict[str, Any]:
132132
return {
133133
"name": self.name,
134134
"language": self.language,
@@ -173,7 +173,7 @@ class HouseholdMember(Model):
173173
),
174174
)
175175

176-
def obj_to_user_dict(self) -> dict:
176+
def obj_to_user_dict(self) -> dict[str, Any]:
177177
res = self.user.obj_to_dict()
178178
res["owner"] = getattr(self, "owner")
179179
res["admin"] = getattr(self, "admin")

backend/app/models/item.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Optional, Self, List, TYPE_CHECKING, cast
2+
from typing import Any, Optional, Self, List, TYPE_CHECKING, cast
33

44
from sqlalchemy import func
55
from app import db
@@ -87,14 +87,14 @@ def obj_to_dict(
8787
self,
8888
skip_columns: list[str] | None = None,
8989
include_columns: list[str] | None = None,
90-
) -> dict:
90+
) -> dict[str, Any]:
9191
res = super().obj_to_dict(skip_columns, include_columns)
9292
if self.category_id:
9393
category = cast(Category, Category.find_by_id(self.category_id))
9494
res["category"] = category.obj_to_dict()
9595
return res
9696

97-
def obj_to_export_dict(self) -> dict:
97+
def obj_to_export_dict(self) -> dict[str, Any]:
9898
res = {
9999
"name": self.name,
100100
}
@@ -104,7 +104,7 @@ def obj_to_export_dict(self) -> dict:
104104
res["category"] = self.category.name
105105
return res
106106

107-
def save(self, keepDefault=False) -> Self:
107+
def save(self, keepDefault: bool = False) -> Self:
108108
if not keepDefault:
109109
self.default = False
110110
return super().save()
@@ -219,7 +219,7 @@ def search_name(cls, name: str, household_id: int) -> list[Self]:
219219
.all()
220220
)
221221

222-
found = []
222+
found: list[Self] = []
223223

224224
# name is a regex
225225
if "*" in name or "?" in name or "%" in name or "_" in name:
@@ -237,7 +237,7 @@ def search_name(cls, name: str, household_id: int) -> list[Self]:
237237
# name is no regex
238238
starts_with = "{0}%".format(name)
239239
contains = "%{0}%".format(name)
240-
one_error = []
240+
one_error: List[str] = []
241241
for index in range(len(name)):
242242
name_one_error = name[:index] + "_" + name[index + 1 :]
243243
one_error.append("%{0}%".format(name_one_error))

backend/app/models/planner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Self, TYPE_CHECKING, cast
2+
from typing import Any, Self, TYPE_CHECKING, cast
33
from app import db
44
from app.helpers import DbModelAuthorizeMixin
55
from sqlalchemy.orm import Mapped
@@ -41,7 +41,7 @@ class Planner(Model, DbModelAuthorizeMixin):
4141
),
4242
)
4343

44-
def obj_to_full_dict(self) -> dict:
44+
def obj_to_full_dict(self) -> dict[str, Any]:
4545
res = self.obj_to_dict()
4646
res["recipe"] = self.recipe.obj_to_full_dict()
4747
return res

0 commit comments

Comments
 (0)