From 0059de6c028996c9b21a833f186ba7df4899fa2d Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Wed, 10 Nov 2021 15:47:20 +0530 Subject: Do not use dangerous default argument []. Default arguments get evaluated only once when the function is defined, and are then shared across all instances of the function. If the argument is then mutated, this can cause hard to find bugs. See https://docs.python.org/3/tutorial/controlflow.html#default-argument-values * gn3/authentication.py (create_group): Do not use [] as the default argument. --- gn3/authentication.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gn3/authentication.py b/gn3/authentication.py index 6719631..1ccffcc 100644 --- a/gn3/authentication.py +++ b/gn3/authentication.py @@ -145,9 +145,13 @@ def get_user_info_by_key(key: str, value: str, def create_group(conn: Redis, group_name: Optional[str], - admin_user_uids: List = [], - member_user_uids: List = []) -> Optional[Dict]: + admin_user_uids: List = None, + member_user_uids: List = None) -> Optional[Dict]: """Create a group given the group name, members and admins of that group.""" + if admin_user_uids is None: + admin_user_uids = [] + if member_user_uids is None: + member_user_uids = [] if group_name and bool(admin_user_uids + member_user_uids): timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') group = { -- cgit v1.2.3 From 83f0e5c2955595de195a6de4a9aa7feeb94df9bb Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Wed, 10 Nov 2021 15:55:45 +0530 Subject: Reformat condition on a single line. * gn3/authentication.py (get_user_info_by_key): Reformat so that condition is on a single line. --- gn3/authentication.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gn3/authentication.py b/gn3/authentication.py index 1ccffcc..7384df0 100644 --- a/gn3/authentication.py +++ b/gn3/authentication.py @@ -132,8 +132,7 @@ def get_user_info_by_key(key: str, value: str, if key != "user_id": for uuid, user_info in conn.hgetall("users").items(): user_info = json.loads(user_info) - if (key in user_info and - user_info.get(key) == value): + if (key in user_info and user_info.get(key) == value): user_info["user_id"] = uuid return user_info elif key == "user_id": -- cgit v1.2.3 From f566d9eb1d4e12d7e7a6ef319c39760f5726578e Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Wed, 10 Nov 2021 15:56:59 +0530 Subject: Do not shadow global symbol uuid. * gn3/authentication.py (get_groups_by_user_uid): Rename local symbol uuid to group_uuid. (get_user_info_by_key): Rename local symbol uuid to user_uuid. --- gn3/authentication.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gn3/authentication.py b/gn3/authentication.py index 7384df0..a6372c1 100644 --- a/gn3/authentication.py +++ b/gn3/authentication.py @@ -113,9 +113,9 @@ def get_groups_by_user_uid(user_uid: str, conn: Redis) -> Dict: """ admin = [] member = [] - for uuid, group_info in conn.hgetall("groups").items(): + for group_uuid, group_info in conn.hgetall("groups").items(): group_info = json.loads(group_info) - group_info["uuid"] = uuid + group_info["uuid"] = group_uuid if user_uid in group_info.get('admins'): admin.append(group_info) if user_uid in group_info.get('members'): @@ -130,10 +130,10 @@ def get_user_info_by_key(key: str, value: str, conn: Redis) -> Optional[Dict]: """Given a key, get a user's information if value is matched""" if key != "user_id": - for uuid, user_info in conn.hgetall("users").items(): + for user_uuid, user_info in conn.hgetall("users").items(): user_info = json.loads(user_info) if (key in user_info and user_info.get(key) == value): - user_info["user_id"] = uuid + user_info["user_id"] = user_uuid return user_info elif key == "user_id": if user_info := conn.hget("users", value): -- cgit v1.2.3