From 6144003f535bc2ee2fc1dcff3e3d7101ab1849e6 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Fri, 26 May 2023 10:19:55 -0400 Subject: [PATCH 1/2] refactor: remove joinedload of User In refactoring the string-based loader to class-based attributes, I found that we do nothing with the resulting user data, we're only looking for existence in the form that uses it. Signed-off-by: Mike Fiedler --- warehouse/macaroons/services.py | 1 - 1 file changed, 1 deletion(-) diff --git a/warehouse/macaroons/services.py b/warehouse/macaroons/services.py index 7c18dbdb4cf4..b8326b708a72 100644 --- a/warehouse/macaroons/services.py +++ b/warehouse/macaroons/services.py @@ -202,7 +202,6 @@ def get_macaroon_by_description(self, user_id, description): try: dm = ( self.db.query(Macaroon) - .options(joinedload("user")) .filter(Macaroon.description == description) .filter(Macaroon.user_id == user_id) .one() From 0e1c8e2b6c0e4fd8199f345a6dd95583bae3cd8e Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Fri, 26 May 2023 10:28:40 -0400 Subject: [PATCH 2/2] refactor: use built-ins over try/except Simplifies the code a little, since `one_or_none()` does the None-ness for us. Signed-off-by: Mike Fiedler --- warehouse/macaroons/services.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/warehouse/macaroons/services.py b/warehouse/macaroons/services.py index b8326b708a72..040f7956edea 100644 --- a/warehouse/macaroons/services.py +++ b/warehouse/macaroons/services.py @@ -16,7 +16,6 @@ import pymacaroons from pymacaroons.exceptions import MacaroonDeserializationException -from sqlalchemy.exc import NoResultFound from sqlalchemy.orm import joinedload from zope.interface import implementer @@ -199,15 +198,12 @@ def get_macaroon_by_description(self, user_id, description): Returns None if the user doesn't have a macaroon with this description. """ - try: - dm = ( - self.db.query(Macaroon) - .filter(Macaroon.description == description) - .filter(Macaroon.user_id == user_id) - .one() - ) - except NoResultFound: - return None + dm = ( + self.db.query(Macaroon) + .filter(Macaroon.description == description) + .filter(Macaroon.user_id == user_id) + .one_or_none() + ) return dm