Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing optional types on ClientSessions #441 #446

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions inventory_management_system_api/repositories/catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def __init__(self, database: DatabaseDep) -> None:
self._catalogue_categories_collection: Collection = self._database.catalogue_categories
self._catalogue_items_collection: Collection = self._database.catalogue_items

def create(self, catalogue_category: CatalogueCategoryIn, session: ClientSession = None) -> CatalogueCategoryOut:
def create(
self, catalogue_category: CatalogueCategoryIn, session: Optional[ClientSession] = None
) -> CatalogueCategoryOut:
"""
Create a new catalogue category in a MongoDB database.

Expand Down Expand Up @@ -72,7 +74,9 @@ def create(self, catalogue_category: CatalogueCategoryIn, session: ClientSession
catalogue_category = self.get(str(result.inserted_id), session=session)
return catalogue_category

def get(self, catalogue_category_id: str, session: ClientSession = None) -> Optional[CatalogueCategoryOut]:
def get(
self, catalogue_category_id: str, session: Optional[ClientSession] = None
) -> Optional[CatalogueCategoryOut]:
"""
Retrieve a catalogue category by its ID from a MongoDB database.

Expand All @@ -89,7 +93,9 @@ def get(self, catalogue_category_id: str, session: ClientSession = None) -> Opti
return CatalogueCategoryOut(**catalogue_category)
return None

def get_breadcrumbs(self, catalogue_category_id: str, session: ClientSession = None) -> BreadcrumbsGetSchema:
def get_breadcrumbs(
self, catalogue_category_id: str, session: Optional[ClientSession] = None
) -> BreadcrumbsGetSchema:
"""
Retrieve the breadcrumbs for a specific catalogue category

Expand All @@ -111,7 +117,7 @@ def get_breadcrumbs(self, catalogue_category_id: str, session: ClientSession = N
collection_name="catalogue_categories",
)

def list(self, parent_id: Optional[str], session: ClientSession = None) -> List[CatalogueCategoryOut]:
def list(self, parent_id: Optional[str], session: Optional[ClientSession] = None) -> List[CatalogueCategoryOut]:
"""
Retrieve catalogue categories from a MongoDB database based on the provided filters.

Expand All @@ -126,7 +132,10 @@ def list(self, parent_id: Optional[str], session: ClientSession = None) -> List[
return [CatalogueCategoryOut(**catalogue_category) for catalogue_category in catalogue_categories]

def update(
self, catalogue_category_id: str, catalogue_category: CatalogueCategoryIn, session: ClientSession = None
self,
catalogue_category_id: str,
catalogue_category: CatalogueCategoryIn,
session: Optional[ClientSession] = None,
) -> CatalogueCategoryOut:
"""
Update a catalogue category by its ID in a MongoDB database.
Expand Down Expand Up @@ -183,7 +192,7 @@ def update(
catalogue_category = self.get(str(catalogue_category_id), session=session)
return catalogue_category

def delete(self, catalogue_category_id: str, session: ClientSession = None) -> None:
def delete(self, catalogue_category_id: str, session: Optional[ClientSession] = None) -> None:
"""
Delete a catalogue category by its ID from a MongoDB database.

Expand All @@ -210,8 +219,8 @@ def _is_duplicate_catalogue_category(
self,
parent_id: Optional[str],
code: str,
catalogue_category_id: CustomObjectId = None,
session: ClientSession = None,
catalogue_category_id: Optional[CustomObjectId] = None,
session: Optional[ClientSession] = None,
) -> bool:
"""
Check if a catalogue category with the same code already exists within the parent category.
Expand All @@ -233,7 +242,9 @@ def _is_duplicate_catalogue_category(

return catalogue_category is not None

def has_child_elements(self, catalogue_category_id: CustomObjectId, session: ClientSession = None) -> bool:
def has_child_elements(
self, catalogue_category_id: CustomObjectId, session: Optional[ClientSession] = None
) -> bool:
"""
Check if a catalogue category has child elements based on its ID.

Expand All @@ -259,7 +270,7 @@ def create_property(
self,
catalogue_category_id: str,
property_in: CatalogueCategoryPropertyIn,
session: ClientSession = None,
session: Optional[ClientSession] = None,
) -> CatalogueCategoryPropertyOut:
"""
Create a new a property within a catalogue category given its ID in a MongoDB database
Expand Down Expand Up @@ -293,7 +304,7 @@ def update_property(
catalogue_category_id: str,
property_id: str,
property_in: CatalogueCategoryPropertyIn,
session: ClientSession = None,
session: Optional[ClientSession] = None,
) -> CatalogueCategoryPropertyOut:
"""
Updates a property given its ID and the ID of the catalogue category it's in
Expand Down
20 changes: 11 additions & 9 deletions inventory_management_system_api/repositories/catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, database: DatabaseDep) -> None:
self._catalogue_items_collection: Collection = self._database.catalogue_items
self._items_collection: Collection = self._database.items

def create(self, catalogue_item: CatalogueItemIn, session: ClientSession = None) -> CatalogueItemOut:
def create(self, catalogue_item: CatalogueItemIn, session: Optional[ClientSession] = None) -> CatalogueItemOut:
"""
Create a new catalogue item in a MongoDB database.

Expand All @@ -46,7 +46,7 @@ def create(self, catalogue_item: CatalogueItemIn, session: ClientSession = None)
catalogue_item = self.get(str(result.inserted_id), session=session)
return catalogue_item

def get(self, catalogue_item_id: str, session: ClientSession = None) -> Optional[CatalogueItemOut]:
def get(self, catalogue_item_id: str, session: Optional[ClientSession] = None) -> Optional[CatalogueItemOut]:
"""
Retrieve a catalogue item by its ID from a MongoDB database.

Expand All @@ -61,7 +61,9 @@ def get(self, catalogue_item_id: str, session: ClientSession = None) -> Optional
return CatalogueItemOut(**catalogue_item)
return None

def list(self, catalogue_category_id: Optional[str], session: ClientSession = None) -> List[CatalogueItemOut]:
def list(
self, catalogue_category_id: Optional[str], session: Optional[ClientSession] = None
) -> List[CatalogueItemOut]:
"""
Retrieve all catalogue items from a MongoDB database.

Expand All @@ -85,7 +87,7 @@ def list(self, catalogue_category_id: Optional[str], session: ClientSession = No
return [CatalogueItemOut(**catalogue_item) for catalogue_item in catalogue_items]

def update(
self, catalogue_item_id: str, catalogue_item: CatalogueItemIn, session: ClientSession = None
self, catalogue_item_id: str, catalogue_item: CatalogueItemIn, session: Optional[ClientSession] = None
) -> CatalogueItemOut:
"""
Update a catalogue item by its ID in a MongoDB database.
Expand All @@ -104,7 +106,7 @@ def update(
catalogue_item = self.get(str(catalogue_item_id), session=session)
return catalogue_item

def delete(self, catalogue_item_id: str, session: ClientSession = None) -> None:
def delete(self, catalogue_item_id: str, session: Optional[ClientSession] = None) -> None:
"""
Delete a catalogue item by its ID from a MongoDB database.

Expand All @@ -123,7 +125,7 @@ def delete(self, catalogue_item_id: str, session: ClientSession = None) -> None:
if result.deleted_count == 0:
raise MissingRecordError(f"No catalogue item found with ID: {str(catalogue_item_id)}")

def has_child_elements(self, catalogue_item_id: CustomObjectId, session: ClientSession = None) -> bool:
def has_child_elements(self, catalogue_item_id: CustomObjectId, session: Optional[ClientSession] = None) -> bool:
"""
Check if a catalogue item has child elements based on its ID.

Expand All @@ -137,7 +139,7 @@ def has_child_elements(self, catalogue_item_id: CustomObjectId, session: ClientS
item = self._items_collection.find_one({"catalogue_item_id": catalogue_item_id}, session=session)
return item is not None

def list_ids(self, catalogue_category_id: str, session: ClientSession = None) -> List[ObjectId]:
def list_ids(self, catalogue_category_id: str, session: Optional[ClientSession] = None) -> List[ObjectId]:
"""
Retrieve a list of all catalogue item ids with a specific catalogue_category_id from a MongoDB
database. Performs a projection to only include _id. (Required for mass updates of properties
Expand All @@ -161,7 +163,7 @@ def list_ids(self, catalogue_category_id: str, session: ClientSession = None) ->
).distinct("_id")

def insert_property_to_all_matching(
self, catalogue_category_id: str, property_in: PropertyIn, session: ClientSession = None
self, catalogue_category_id: str, property_in: PropertyIn, session: Optional[ClientSession] = None
):
"""
Inserts a property into every catalogue item with a given catalogue_category_id via an update_many query
Expand All @@ -186,7 +188,7 @@ def insert_property_to_all_matching(
)

def update_names_of_all_properties_with_id(
self, property_id: str, new_property_name: str, session: ClientSession = None
self, property_id: str, new_property_name: str, session: Optional[ClientSession] = None
) -> None:
"""
Updates the name of a property in every catalogue item it is present in
Expand Down
14 changes: 7 additions & 7 deletions inventory_management_system_api/repositories/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, database: DatabaseDep) -> None:
self._items_collection: Collection = self._database.items
self._systems_collection: Collection = self._database.systems

def create(self, item: ItemIn, session: ClientSession = None) -> ItemOut:
def create(self, item: ItemIn, session: Optional[ClientSession] = None) -> ItemOut:
"""
Create a new item in a MongoDB database.

Expand All @@ -51,7 +51,7 @@ def create(self, item: ItemIn, session: ClientSession = None) -> ItemOut:
item = self.get(str(result.inserted_id), session=session)
return item

def get(self, item_id: str, session: ClientSession = None) -> Optional[ItemOut]:
def get(self, item_id: str, session: Optional[ClientSession] = None) -> Optional[ItemOut]:
"""
Retrieve an item by its ID from a MongoDB database.

Expand All @@ -67,7 +67,7 @@ def get(self, item_id: str, session: ClientSession = None) -> Optional[ItemOut]:
return None

def list(
self, system_id: Optional[str], catalogue_item_id: Optional[str], session: ClientSession = None
self, system_id: Optional[str], catalogue_item_id: Optional[str], session: Optional[ClientSession] = None
) -> List[ItemOut]:
"""
Get all items from the MongoDB database
Expand Down Expand Up @@ -98,7 +98,7 @@ def list(
items = self._items_collection.find(query, session=session)
return [ItemOut(**item) for item in items]

def update(self, item_id: str, item: ItemIn, session: ClientSession = None) -> ItemOut:
def update(self, item_id: str, item: ItemIn, session: Optional[ClientSession] = None) -> ItemOut:
"""
Update an item by its ID in a MongoDB database.

Expand All @@ -113,7 +113,7 @@ def update(self, item_id: str, item: ItemIn, session: ClientSession = None) -> I
item = self.get(str(item_id), session=session)
return item

def delete(self, item_id: str, session: ClientSession = None) -> None:
def delete(self, item_id: str, session: Optional[ClientSession] = None) -> None:
"""
Delete an item by its ID from a MongoDB database.

Expand All @@ -128,7 +128,7 @@ def delete(self, item_id: str, session: ClientSession = None) -> None:
raise MissingRecordError(f"No item found with ID: {str(item_id)}")

def insert_property_to_all_in(
self, catalogue_item_ids: List[ObjectId], property_in: PropertyIn, session: ClientSession = None
self, catalogue_item_ids: List[ObjectId], property_in: PropertyIn, session: Optional[ClientSession] = None
):
"""
Inserts a property into every item with one of the given catalogue_item_id's using an update_many query
Expand All @@ -155,7 +155,7 @@ def insert_property_to_all_in(

# pylint:disable=duplicate-code
def update_names_of_all_properties_with_id(
self, property_id: str, new_property_name: str, session: ClientSession = None
self, property_id: str, new_property_name: str, session: Optional[ClientSession] = None
) -> None:
"""
Updates the name of a property in every item it is present in
Expand Down
14 changes: 7 additions & 7 deletions inventory_management_system_api/repositories/manufacturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, database: DatabaseDep) -> None:
self._manufacturers_collection: Collection = self._database.manufacturers
self._catalogue_items_collection: Collection = self._database.catalogue_items

def create(self, manufacturer: ManufacturerIn, session: ClientSession = None) -> ManufacturerOut:
def create(self, manufacturer: ManufacturerIn, session: Optional[ClientSession] = None) -> ManufacturerOut:
"""
Create a new manufacturer in a MongoDB database.

Expand All @@ -52,7 +52,7 @@ def create(self, manufacturer: ManufacturerIn, session: ClientSession = None) ->

return manufacturer

def get(self, manufacturer_id: str, session: ClientSession = None) -> Optional[ManufacturerOut]:
def get(self, manufacturer_id: str, session: Optional[ClientSession] = None) -> Optional[ManufacturerOut]:
"""
Retrieve a manufacturer by its ID from a MondoDB database.

Expand All @@ -67,7 +67,7 @@ def get(self, manufacturer_id: str, session: ClientSession = None) -> Optional[M
return ManufacturerOut(**manufacturer)
return None

def list(self, session: ClientSession = None) -> List[ManufacturerOut]:
def list(self, session: Optional[ClientSession] = None) -> List[ManufacturerOut]:
"""
Retrieve all manufacturers from a MongoDB database.

Expand All @@ -79,7 +79,7 @@ def list(self, session: ClientSession = None) -> List[ManufacturerOut]:
return [ManufacturerOut(**manufacturer) for manufacturer in manufacturers]

def update(
self, manufacturer_id: str, manufacturer: ManufacturerIn, session: ClientSession = None
self, manufacturer_id: str, manufacturer: ManufacturerIn, session: Optional[ClientSession] = None
) -> ManufacturerOut:
"""
Update manufacturer by its ID in a MongoDB database.
Expand All @@ -105,7 +105,7 @@ def update(
manufacturer = self.get(str(manufacturer_id), session=session)
return manufacturer

def delete(self, manufacturer_id: str, session: ClientSession = None) -> None:
def delete(self, manufacturer_id: str, session: Optional[ClientSession] = None) -> None:
"""
Delete a manufacturer by its ID from a MongoDB database.

Expand All @@ -127,7 +127,7 @@ def delete(self, manufacturer_id: str, session: ClientSession = None) -> None:
raise MissingRecordError(f"No manufacturer found with ID: {str(manufacturer_id)}")

def _is_duplicate_manufacturer(
self, code: str, manufacturer_id: CustomObjectId = None, session: ClientSession = None
self, code: str, manufacturer_id: Optional[CustomObjectId] = None, session: Optional[ClientSession] = None
) -> bool:
"""
Check if a manufacturer with the same code already exists.
Expand All @@ -143,7 +143,7 @@ def _is_duplicate_manufacturer(
)
return manufacturer is not None

def _is_manufacturer_in_catalogue_item(self, manufacturer_id: str, session: ClientSession = None) -> bool:
def _is_manufacturer_in_catalogue_item(self, manufacturer_id: str, session: Optional[ClientSession] = None) -> bool:
"""
Check if a manufacturer is part of a catalogue item based on its ID.

Expand Down
6 changes: 4 additions & 2 deletions inventory_management_system_api/repositories/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, database: DatabaseDep) -> None:
self._settings_collection: Collection = self._database.settings

def upsert(
self, setting: SettingInBaseT, out_model_type: Type[SettingOutBaseT], session: ClientSession = None
self, setting: SettingInBaseT, out_model_type: Type[SettingOutBaseT], session: Optional[ClientSession] = None
) -> SettingOutBaseT:
"""
Update or insert a setting in a MongoDB database depending on whether it already exists.
Expand All @@ -83,7 +83,9 @@ def upsert(

return self.get(out_model_type=out_model_type, session=session)

def get(self, out_model_type: Type[SettingOutBaseT], session: ClientSession = None) -> Optional[SettingOutBaseT]:
def get(
self, out_model_type: Type[SettingOutBaseT], session: Optional[ClientSession] = None
) -> Optional[SettingOutBaseT]:
"""
Retrieve a setting from a MongoDB database.

Expand Down
Loading
Loading