From 5d5d5a93afbc78c6a83c8b35efd11c3a1510678f Mon Sep 17 00:00:00 2001 From: Carlos Sousa Date: Mon, 25 Sep 2023 12:15:35 -0300 Subject: [PATCH] Fix type removing Optional for get_one and update if statement --- lib/sqlalchemy/ext/asyncio/session.py | 4 ++-- lib/sqlalchemy/orm/session.py | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/sqlalchemy/ext/asyncio/session.py b/lib/sqlalchemy/ext/asyncio/session.py index f8c5a287d5..14e5a484e6 100644 --- a/lib/sqlalchemy/ext/asyncio/session.py +++ b/lib/sqlalchemy/ext/asyncio/session.py @@ -623,7 +623,7 @@ class AsyncSession(ReversibleProxy[Session]): with_for_update: ForUpdateParameter = None, identity_token: Optional[Any] = None, execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT, - ) -> Optional[_O]: + ) -> _O: """Return an instance based on the given primary key identifier, or raise an exception. @@ -641,7 +641,7 @@ class AsyncSession(ReversibleProxy[Session]): identity_token=identity_token, ) - if not result_obj: + if result_obj is None: raise async_exc.NoResultFound( "No row was found when one was required" ) diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index d825083646..aa7078304c 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -3591,7 +3591,7 @@ class Session(_SessionClassMethods, EventTarget): identity_token: Optional[Any] = None, execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT, bind_arguments: Optional[_BindArguments] = None, - ) -> Optional[_O]: + ) -> _O: """Return exactly one instance based on the given primary key identifier, or raise an exception. @@ -3601,9 +3601,9 @@ class Session(_SessionClassMethods, EventTarget): my_user = session.get_one(User, 5) - some_object = session.get(VersionedFoo, (5, 10)) + some_object = session.get_one(VersionedFoo, (5, 10)) - some_object = session.get( + some_object = session.get_one( VersionedFoo, {"id": 5, "version_id": 10} ) @@ -3635,7 +3635,7 @@ class Session(_SessionClassMethods, EventTarget): the most expedient. If the primary key of a row is the value "5", the call looks like:: - my_object = session.get(SomeClass, 5) + my_object = session.get_one(SomeClass, 5) The tuple form contains primary key values typically in the order in which they correspond to the mapped @@ -3647,14 +3647,14 @@ class Session(_SessionClassMethods, EventTarget): of a row is represented by the integer digits "5, 10" the call would look like:: - my_object = session.get(SomeClass, (5, 10)) + my_object = session.get_one(SomeClass, (5, 10)) The dictionary form should include as keys the mapped attribute names corresponding to each element of the primary key. If the mapped class has the attributes ``id``, ``version_id`` as the attributes which store the object's primary key value, the call would look like:: - my_object = session.get(SomeClass, {"id": 5, "version_id": 10}) + my_object = session.get_one(SomeClass, {"id": 5, "version_id": 10}) :param options: optional sequence of loader options which will be applied to the query, if one is emitted. @@ -3696,7 +3696,7 @@ class Session(_SessionClassMethods, EventTarget): bind_arguments=bind_arguments, ) - if not impl: + if impl is None: raise sa_exc.NoResultFound( "No row was found when one was required" ) -- 2.47.3