]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
See if the future is here
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 28 Aug 2020 21:56:43 +0000 (17:56 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 28 Aug 2020 22:14:11 +0000 (18:14 -0400)
The docs are going to talk a lot about session.execute(select())
for ORM queries, and additionally it's much easier to help
users with queries and such if we can use this new syntax.
I'm hoping to see how hard it is to get a unified tutorial
started that switches to new syntax.  Basically, new syntax
is much easier to explain and less buggy.   But, if we
are starting to present new syntax with the explicit goal
of being easier to explain for less experienced programmers,
the "future" thing is going to just be an impediment
to that.

See if we can remove "future" from session.execute(),
so that ORM-enabled select() statements return ORM results
at that level.  This does not change the presence of the
"future" flag for the Session's construction and for its
transactional behaviors.

The only perceptible change of the future flag for
session.execute() is that session.execute(select()) where the
statement has ORM entities in it now returns ORM new
style tuples rather than old style tuples.   Like
mutating a URL, it's hopefully not very common that people
are doing this.

Change-Id: I0aa10322bb787d554d32772e3bc60548f1bf6206

lib/sqlalchemy/ext/baked.py
lib/sqlalchemy/orm/dynamic.py
lib/sqlalchemy/orm/loading.py
lib/sqlalchemy/orm/persistence.py
lib/sqlalchemy/orm/query.py
lib/sqlalchemy/orm/session.py
lib/sqlalchemy/orm/strategies.py
test/ext/test_baked.py
test/orm/test_query.py

index 97c825f02afe3798b2b8dd263479f48bc723771b..1fad89286052a386e38129bc39fa76552605fbe2 100644 (file)
@@ -411,7 +411,7 @@ class Result(object):
         )
 
         result = self.session.execute(
-            statement, params, execution_options=execution_options, future=True
+            statement, params, execution_options=execution_options
         )
         if result._attributes.get("is_single_entity", False):
             result = result.scalars()
index 7832152a24be1a7711917be2af04fc7307838c0b..41d0fabd23a2b1a4c9110ee771edabcc88b689a4 100644 (file)
@@ -461,7 +461,7 @@ class AppenderQuery(Generative):
                     % (orm_util.instance_str(instance), self.attr.key)
                 )
 
-        result = sess.execute(self._statement, future=True)
+        result = sess.execute(self._statement)
         result = result.scalars()
 
         if result._attributes.get("filtered", False):
index fd3e92055b8bf439b0926a3b4aebb50815ed381e..d3971414719d32d61ec87015dff26e552bb07a16 100644 (file)
@@ -498,7 +498,6 @@ def load_on_pk_identity(
             params=params,
             execution_options=execution_options,
             bind_arguments=bind_arguments,
-            future=True,
         )
         .unique()
         .scalars()
index 676dd438c189dd9d0a797c12a93c3e9df7bcdaa0..49b29a6bc0fa1cb38a899a3f80414f689ba8329a 100644 (file)
@@ -2021,7 +2021,6 @@ class BulkUDCompileState(CompileState):
             execution_options,
             bind_arguments,
             _add_event=skip_for_full_returning,
-            future=True,
         )
         matched_rows = result.fetchall()
 
index 68ca0365b7892b94075e31189544d9ca775e2da3..37ff4896442ef1756bd766bd9b2dfb5edbf8e6a2 100644 (file)
@@ -2726,7 +2726,6 @@ class Query(
             statement,
             params,
             execution_options={"_sa_orm_load_options": self.load_options},
-            future=True,
         )
 
         # legacy: automatically set scalars, unique
@@ -3013,7 +3012,6 @@ class Query(
             delete_,
             self._params,
             execution_options={"synchronize_session": synchronize_session},
-            future=True,
         )
         bulk_del.result = result
         self.session.dispatch.after_bulk_delete(bulk_del)
@@ -3089,7 +3087,6 @@ class Query(
             upd,
             self._params,
             execution_options={"synchronize_session": synchronize_session},
-            future=True,
         )
         bulk_ud.result = result
         self.session.dispatch.after_bulk_update(bulk_ud)
index ed1af0a803ff8529e83dc55908acc9dbfe958978..dc3af80f24cfee4808f218c8da06120f8a6d4794 100644 (file)
@@ -232,7 +232,6 @@ class ORMExecuteState(util.MemoizedSlots):
             _execution_options,
             _bind_arguments,
             _parent_execute_state=self,
-            future=self._future,
         )
 
     @property
@@ -1431,7 +1430,6 @@ class Session(_SessionClassMethods):
         params=None,
         execution_options=util.EMPTY_DICT,
         bind_arguments=None,
-        future=False,
         _parent_execute_state=None,
         _add_event=None,
         **kw
@@ -1538,14 +1536,6 @@ class Session(_SessionClassMethods):
          Contents of this dictionary are passed to the
          :meth:`.Session.get_bind` method.
 
-        :param future:
-            Use future style execution for this statement.  This is
-            the same effect as the :paramref:`_orm.Session.future` flag,
-            except at the level of this single statement execution.  See
-            that flag for details.
-
-            .. versionadded:: 1.4
-
         :param mapper:
           deprecated; use the bind_arguments dictionary
 
@@ -1571,8 +1561,6 @@ class Session(_SessionClassMethods):
         """
         statement = coercions.expect(roles.CoerceTextStatementRole, statement)
 
-        future = future or self.future
-
         if not bind_arguments:
             bind_arguments = kw
         elif kw:
@@ -1605,13 +1593,9 @@ class Session(_SessionClassMethods):
             )
         else:
             bind_arguments.setdefault("clause", statement)
-            if future:
-                # not sure if immutabledict is working w/ this syntax
-                # execution_options =
-                # execution_options.union(future_result=True)
-                execution_options = execution_options.union(
-                    {"future_result": True}
-                )
+            execution_options = execution_options.union(
+                {"future_result": True}
+            )
 
         if _parent_execute_state:
             events_todo = _parent_execute_state._remaining_events()
@@ -1637,7 +1621,6 @@ class Session(_SessionClassMethods):
                 if result:
                     return result
 
-            # TODO: coverage for this pattern
             statement = orm_exec_state.statement
             execution_options = orm_exec_state.local_execution_options
 
index 53166bd9184992b1df09edbd63e79696259fd775..b9826ac87df7e04243900af28c36f0a4b6ccdc9d 100644 (file)
@@ -989,7 +989,7 @@ class LazyLoader(AbstractRelationshipLoader, util.MemoizedSlots):
         )
 
         result = session.execute(
-            stmt, params, future=True, execution_options=execution_options
+            stmt, params, execution_options=execution_options
         )
 
         result = result.unique().scalars().all()
@@ -2794,7 +2794,6 @@ class SelectInLoader(PostLoader, util.MemoizedSlots):
                             for key in chunk
                         ]
                     },
-                    future=True,
                 ).unique()
             }
 
@@ -2839,7 +2838,7 @@ class SelectInLoader(PostLoader, util.MemoizedSlots):
             data = collections.defaultdict(list)
             for k, v in itertools.groupby(
                 context.session.execute(
-                    q, params={"primary_keys": primary_keys}, future=True
+                    q, params={"primary_keys": primary_keys}
                 ).unique(),
                 lambda x: x[0],
             ):
index c8e83bbd74ee77380cb54ae3c2b748b0f1bce102..49529f81ca168783c58b40f89775ad3c0926102e 100644 (file)
@@ -1067,15 +1067,13 @@ class CustomIntegrationTest(testing.AssertsCompiledSQL, BakedTest):
         q = sess.query(User).filter(User.id == 7).set_cache_key("user7")
 
         eq_(
-            sess.execute(q, future=True).all(),
-            [(User(id=7, addresses=[Address(id=1)]),)],
+            sess.execute(q).all(), [(User(id=7, addresses=[Address(id=1)]),)],
         )
 
         eq_(list(q.cache), ["user7"])
 
         eq_(
-            sess.execute(q, future=True).all(),
-            [(User(id=7, addresses=[Address(id=1)]),)],
+            sess.execute(q).all(), [(User(id=7, addresses=[Address(id=1)]),)],
         )
 
     def test_use_w_baked(self):
index e43504d9e2e42df3eb478178405a1060db11f933..a0171295b2384778a1ac8fe8f49042ffb706288c 100644 (file)
@@ -188,7 +188,7 @@ class RowTupleTest(QueryTest):
 
         mapper(User, users)
 
-        s = Session(testing.db, future=True)
+        s = Session(testing.db)
 
         q = testing.resolve_lambda(test_case, **locals())
 
@@ -212,15 +212,8 @@ class RowTupleTest(QueryTest):
 
         row = s.execute(q.order_by(User.id)).first()
 
-        # old style row
-        assert "jack" not in row
-        assert "jack" in tuple(row)
-
-        row = s.execute(q.order_by(User.id), future=True).first()
-
-        # new style row - not sure what to do here w/ future yet
+        # s.execute() is now new style row
         assert "jack" in row
-        assert "jack" in tuple(row)
 
     def test_entity_mapping_access(self):
         User, users = self.classes.User, self.tables.users
@@ -877,7 +870,7 @@ class GetTest(QueryTest):
     def test_populate_existing_future(self):
         User, Address = self.classes.User, self.classes.Address
 
-        s = Session(testing.db, future=True, autoflush=False)
+        s = Session(testing.db, autoflush=False)
 
         userlist = s.query(User).all()
 
@@ -926,7 +919,7 @@ class GetTest(QueryTest):
         stmt = select(User).execution_options(
             populate_existing=True, autoflush=False, yield_per=10
         )
-        s = Session(testing.db, future=True)
+        s = Session(testing.db)
 
         m1 = mock.Mock()
 
@@ -4630,7 +4623,7 @@ class TextTest(QueryTest, AssertsCompiledSQL):
     def test_select_star_future(self):
         User = self.classes.User
 
-        sess = Session(testing.db, future=True)
+        sess = Session(testing.db)
         eq_(
             sess.execute(
                 select(User).from_statement(
@@ -4678,7 +4671,7 @@ class TextTest(QueryTest, AssertsCompiledSQL):
         # ordering doesn't matter
         User = self.classes.User
 
-        s = create_session(testing.db, future=True)
+        s = create_session(testing.db)
         q = select(User).from_statement(
             text(
                 "select name, 27 as foo, id as users_id from users order by id"
@@ -4725,7 +4718,7 @@ class TextTest(QueryTest, AssertsCompiledSQL):
         User = self.classes.User
         Address = self.classes.Address
 
-        s = create_session(testing.db, future=True)
+        s = create_session(testing.db)
         q = select(User, Address).from_statement(
             text(
                 "select users.name AS users_name, users.id AS users_id, "
@@ -4776,7 +4769,7 @@ class TextTest(QueryTest, AssertsCompiledSQL):
         User = self.classes.User
         Address = self.classes.Address
 
-        s = create_session(testing.db, future=True)
+        s = create_session(testing.db)
         q = (
             select(User)
             .from_statement(
@@ -4828,7 +4821,7 @@ class TextTest(QueryTest, AssertsCompiledSQL):
         User = self.classes.User
         Address = self.classes.Address
 
-        s = create_session(testing.db, future=True)
+        s = create_session(testing.db)
         q = (
             select(User)
             .from_statement(
@@ -4935,7 +4928,7 @@ class TextTest(QueryTest, AssertsCompiledSQL):
     def test_whereclause_future(self):
         User = self.classes.User
 
-        s = create_session(testing.db, future=True)
+        s = create_session(testing.db)
         eq_(
             s.execute(select(User).filter(text("id in (8, 9)")))
             .scalars()