From 09a8fdfeccf9c87d748b8de2f1068dcc0864b5e1 Mon Sep 17 00:00:00 2001 From: tanshu Date: Tue, 26 Apr 2022 08:59:42 +0530 Subject: [PATCH] Chore: some mypy fixes --- barker/barker/core/security.py | 2 +- barker/barker/printing/kot.py | 4 ++-- barker/barker/routers/modifier_category.py | 3 +-- barker/barker/routers/product.py | 4 ++-- barker/barker/routers/role.py | 3 +-- barker/barker/routers/temporal_product.py | 4 ++-- barker/barker/routers/user.py | 3 +-- barker/barker/routers/voucher/split.py | 8 ++++---- 8 files changed, 14 insertions(+), 17 deletions(-) diff --git a/barker/barker/core/security.py b/barker/barker/core/security.py index bf65d43..6c3838a 100644 --- a/barker/barker/core/security.py +++ b/barker/barker/core/security.py @@ -65,7 +65,7 @@ def authenticate_user(username: str, password: str, db: Session) -> Optional[Use return user -def device_allowed(user: UserModel, device_id: Optional[uuid.UUID], db: Session = None) -> Tuple[bool, Device]: +def device_allowed(user: UserModel, device_id: Optional[uuid.UUID], db: Session) -> Tuple[bool, Device]: device: Device = db.execute(select(Device).where(Device.id == device_id)).scalars().one_or_none() if device is None: device = Device.create(db) diff --git a/barker/barker/printing/kot.py b/barker/barker/printing/kot.py index f4e0a32..ef67025 100644 --- a/barker/barker/printing/kot.py +++ b/barker/barker/printing/kot.py @@ -2,7 +2,7 @@ import asyncio import uuid from datetime import timedelta -from typing import List +from typing import Any, Dict, List, Tuple from arq import ArqRedis, create_pool from barker.core.config import settings @@ -66,7 +66,7 @@ def design_kot(voucher: Voucher, kot: Kot, items: List[Inventory], copy_number: def print_kot(voucher_id: uuid.UUID, db: Session): voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == voucher_id)).scalar_one() - my_hash = {} + my_hash: Dict[Tuple[uuid.UUID, int], Tuple[Printer, List[Any]]] = {} kot: Kot = voucher.kots[-1] product_date = ( voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES) diff --git a/barker/barker/routers/modifier_category.py b/barker/barker/routers/modifier_category.py index 2219203..0c6336c 100644 --- a/barker/barker/routers/modifier_category.py +++ b/barker/barker/routers/modifier_category.py @@ -299,8 +299,7 @@ def modifier_category_blank(date_: date, db: Session) -> schemas.ModifierCategor def add_products(modifier_category: ModifierCategory, menu_categories: List[schemas.MenuCategoryLink], db: Session): for mc in menu_categories: for p in mc.products: - old = [x for x in modifier_category.products if x.id == p.id_] - old = None if len(old) == 0 else old[0] + old = next([x for x in modifier_category.products if x.id == p.id_], None) if p.enabled and old is None: product_object = db.execute(select(Product).where(Product.id == p.id_)).scalar_one() modifier_category.products.append(product_object) diff --git a/barker/barker/routers/product.py b/barker/barker/routers/product.py index 4e6d675..f8cd377 100644 --- a/barker/barker/routers/product.py +++ b/barker/barker/routers/product.py @@ -1,7 +1,7 @@ import uuid from datetime import date, timedelta -from typing import List, Optional +from typing import Dict, List, Optional import barker.schemas.product as schemas @@ -38,7 +38,7 @@ def sort_order( ): try: with SessionFuture() as db: - indexes = {} + indexes: Dict[uuid.UUID, int] = {} for item in data: if item.menu_category.id_ in indexes: indexes[item.menu_category.id_] += 1 diff --git a/barker/barker/routers/role.py b/barker/barker/routers/role.py index 1eeb252..2712d94 100644 --- a/barker/barker/routers/role.py +++ b/barker/barker/routers/role.py @@ -143,8 +143,7 @@ def role_blank(db: Session) -> schemas.RoleBlank: def add_permissions(role: Role, permissions: List[schemas.PermissionItem], db: Session): for permission in permissions: - gp = [p for p in role.permissions if p.id == permission.id_] - gp = None if len(gp) == 0 else gp[0] + gp = next([p for p in role.permissions if p.id == permission.id_], None) if permission.enabled and gp is None: role.permissions.append(db.execute(select(Permission).where(Permission.id == permission.id_)).scalar_one()) elif not permission.enabled and gp: diff --git a/barker/barker/routers/temporal_product.py b/barker/barker/routers/temporal_product.py index 13e00ae..254f051 100644 --- a/barker/barker/routers/temporal_product.py +++ b/barker/barker/routers/temporal_product.py @@ -1,7 +1,7 @@ import uuid from datetime import date, timedelta -from typing import List +from typing import Dict, List import barker.schemas.product as schemas @@ -183,7 +183,7 @@ def show_list(user: UserToken = Security(get_user, scopes=["temporal-products"]) def product_list(db: Session) -> List[List[schemas.Product]]: - dict_ = {} + dict_: Dict[uuid.UUID, List[schemas.Product]] = {} list_ = ( db.execute( select(ProductVersion) diff --git a/barker/barker/routers/user.py b/barker/barker/routers/user.py index e81c3bd..fbf173b 100644 --- a/barker/barker/routers/user.py +++ b/barker/barker/routers/user.py @@ -95,8 +95,7 @@ def update_route( def add_roles(user: User, roles: List[schemas.RoleItem], db: Session): for role in roles: - ug = [g for g in user.roles if g.id == role.id_] - ug = None if len(ug) == 0 else ug[0] + ug = next([g for g in user.roles if g.id == role.id_], None) if role.enabled and ug is None: user.roles.append(db.execute(select(Role).where(Role.id == role.id_)).scalar_one()) elif not role.enabled and ug: diff --git a/barker/barker/routers/voucher/split.py b/barker/barker/routers/voucher/split.py index 088cee5..57f4078 100644 --- a/barker/barker/routers/voucher/split.py +++ b/barker/barker/routers/voucher/split.py @@ -3,7 +3,7 @@ import uuid from collections import defaultdict from datetime import datetime from decimal import Decimal -from typing import List, Optional +from typing import Dict, List, Optional, Tuple import barker.schemas.split as schemas @@ -171,12 +171,12 @@ def happy_hour_items_balanced(inventories: List[Inventory]) -> bool: def are_product_quantities_positive(inventories: List[Inventory]) -> bool: - quantities = defaultdict(Decimal) + quantities: Dict[Tuple[uuid.UUID, bool], Decimal] = defaultdict(Decimal) for i in inventories: key = (i.product_id, i.is_happy_hour) quantities[key] += i.quantity - for i in quantities.values(): - if i < 0: + for j in quantities.values(): + if j < 0: return False return True