From 5a6883bdd94893401565a04c74d2817bf763f512 Mon Sep 17 00:00:00 2001 From: Alex Kanitz Date: Mon, 20 May 2024 02:19:05 +0200 Subject: [PATCH] fix mypy issues --- foca/database/register_mongodb.py | 13 ++++++++----- .../access_control/access_control_server.py | 11 ++++++----- .../access_control/foca_casbin_adapter/adapter.py | 8 +++++--- foca/utils/db.py | 2 +- requirements.txt | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/foca/database/register_mongodb.py b/foca/database/register_mongodb.py index ed8285d4..4433515d 100644 --- a/foca/database/register_mongodb.py +++ b/foca/database/register_mongodb.py @@ -43,16 +43,19 @@ def register_mongodb( db_conf.client = mongo.db # Add collections - if db_conf.collections is not None: + if db_conf.collections is not None and db_conf.client is not None: for coll_name, coll_conf in db_conf.collections.items(): - coll_conf.client = mongo.db[coll_name] + coll_conf.client = db_conf.client[coll_name] logger.info( f"Added database collection '{coll_name}'." ) # Add indexes - if coll_conf.indexes is not None: + if ( + coll_conf.indexes is not None + and coll_conf.client is not None + ): # Remove already created indexes if any coll_conf.client.drop_indexes() for index in coll_conf.indexes: @@ -94,10 +97,10 @@ def add_new_database( db_conf.client = mongo.db # Add collections - if db_conf.collections is not None: + if db_conf.collections is not None and db_conf.client is not None: for coll_name, coll_conf in db_conf.collections.items(): - coll_conf.client = mongo.db[coll_name] + coll_conf.client = db_conf.client[coll_name] logger.info( f"Added database collection '{coll_name}'." ) diff --git a/foca/security/access_control/access_control_server.py b/foca/security/access_control/access_control_server.py index 6521c017..e55474a6 100644 --- a/foca/security/access_control/access_control_server.py +++ b/foca/security/access_control/access_control_server.py @@ -113,11 +113,12 @@ def getAllPermissions(limit=None) -> List[Dict]: if not limit: limit = 0 - permissions = db_coll_permission.find( - filter={}, - projection={'_id': False} - ).sort([('$natural', -1)]).limit(limit) - permissions = list(permissions) + permissions = list( + db_coll_permission.find( + filter={}, + projection={'_id': False} + ).sort([('$natural', -1)]).limit(limit) + ) user_permission_list = [] for _permission in permissions: policy_type = _permission.get("ptype", None) diff --git a/foca/security/access_control/foca_casbin_adapter/adapter.py b/foca/security/access_control/foca_casbin_adapter/adapter.py index 76bcf3ca..26f1fe45 100644 --- a/foca/security/access_control/foca_casbin_adapter/adapter.py +++ b/foca/security/access_control/foca_casbin_adapter/adapter.py @@ -36,8 +36,9 @@ def __init__( self, uri: str, dbname: str, collection: Optional[str] = "casbin_rule" ): """Create an adapter for Mongodb.""" - client = MongoClient(uri) + client: MongoClient = MongoClient(uri) db = client[dbname] + assert collection is not None self._collection = db[collection] def load_policy(self, model: CasbinRule): @@ -100,8 +101,9 @@ def _delete_policy_lines(self, ptype: str, rule: List[str]) -> int: for result in results if line_dict_keys_len == len(result.keys()) - 1 ] - results = self._collection.delete_many({"_id": {"$in": to_delete}}) - return results.deleted_count + return self._collection.delete_many( + {"_id": {"$in": to_delete}} + ).deleted_count def save_policy(self, model: Model) -> bool: """Method to save a casbin model. diff --git a/foca/utils/db.py b/foca/utils/db.py index 2b80d0d5..e44604c5 100644 --- a/foca/utils/db.py +++ b/foca/utils/db.py @@ -3,7 +3,7 @@ from typing import (Any, Mapping, Optional) from bson.objectid import ObjectId -from pymongo import collection as Collection +from pymongo.collection import Collection def find_one_latest(collection: Collection) -> Optional[Mapping[Any, Any]]: diff --git a/requirements.txt b/requirements.txt index 5aac9bbb..2a549a25 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ Flask-Cors==4.0.1 Flask-PyMongo==2.3.0 pydantic==1.10.13 PyJWT==2.4.0 -pymongo==3.12.3 +pymongo==4.7.2 PyYAML==6.0.1 requests==2.31.0 swagger-ui-bundle==0.0.6