Browse Source

Rename enginefacade.async to enginefacade.async_

Python 3.7 now makes "async" a keyword, so deprecate the
"async" name and replace with "async_".  Additionally,
use attribute name access only in order to implement and
test "async" as it won't work under Python 3.7.

Closes-Bug: #1775866
Change-Id: Ifb2c59c2cfbd615b04570b31fcfe868f4ffea3c6
Mike Bayer 10 months ago
parent
commit
df6bf34012
2 changed files with 39 additions and 6 deletions
  1. 13
    1
      oslo_db/sqlalchemy/enginefacade.py
  2. 26
    5
      oslo_db/tests/sqlalchemy/test_enginefacade.py

+ 13
- 1
oslo_db/sqlalchemy/enginefacade.py View File

@@ -17,6 +17,7 @@ import operator
17 17
 import threading
18 18
 import warnings
19 19
 
20
+import debtcollector.moves
20 21
 import debtcollector.removals
21 22
 import debtcollector.renames
22 23
 from oslo_config import cfg
@@ -965,7 +966,7 @@ class _TransactionContextManager(object):
965 966
         return self._clone(connection=True)
966 967
 
967 968
     @property
968
-    def async(self):
969
+    def async_(self):
969 970
         """Modifier to set a READER operation to ASYNC_READER."""
970 971
 
971 972
         if self._mode is _WRITER:
@@ -1049,6 +1050,17 @@ class _TransactionContextManager(object):
1049 1050
                 transaction_contexts_by_thread.current = restore
1050 1051
 
1051 1052
 
1053
+@property
1054
+@debtcollector.moves.moved_property("async_")
1055
+def async_compat(self):
1056
+    return self.async_
1057
+
1058
+setattr(
1059
+    _TransactionContextManager,
1060
+    "async", async_compat
1061
+)
1062
+
1063
+
1052 1064
 def _context_descriptor(attr=None):
1053 1065
     getter = operator.attrgetter(attr)
1054 1066
 

+ 26
- 5
oslo_db/tests/sqlalchemy/test_enginefacade.py View File

@@ -15,6 +15,7 @@ import contextlib
15 15
 import copy
16 16
 import fixtures
17 17
 import pickle
18
+import sys
18 19
 import warnings
19 20
 
20 21
 import mock
@@ -607,7 +608,7 @@ class MockFacadeTest(oslo_test_base.BaseTestCase):
607 608
 
608 609
     def test_async_on_writer_raises(self):
609 610
         exc = self.assertRaises(
610
-            TypeError, getattr, enginefacade.writer, "async"
611
+            TypeError, getattr, enginefacade.writer, "async_"
611 612
         )
612 613
         self.assertEqual(
613 614
             "Setting async on a WRITER makes no sense",
@@ -626,7 +627,7 @@ class MockFacadeTest(oslo_test_base.BaseTestCase):
626 627
     def test_reader_nested_in_async_reader_raises(self):
627 628
         context = oslo_context.RequestContext()
628 629
 
629
-        @enginefacade.reader.async
630
+        @enginefacade.reader.async_
630 631
         def go1(context):
631 632
             context.session.execute("test1")
632 633
             go2(context)
@@ -647,7 +648,7 @@ class MockFacadeTest(oslo_test_base.BaseTestCase):
647 648
     def test_reader_allow_async_nested_in_async_reader(self):
648 649
         context = oslo_context.RequestContext()
649 650
 
650
-        @enginefacade.reader.async
651
+        @enginefacade.reader.async_
651 652
         def go1(context):
652 653
             context.session.execute("test1")
653 654
             go2(context)
@@ -701,7 +702,7 @@ class MockFacadeTest(oslo_test_base.BaseTestCase):
701 702
     def test_writer_nested_in_async_reader_raises(self):
702 703
         context = oslo_context.RequestContext()
703 704
 
704
-        @enginefacade.reader.async
705
+        @enginefacade.reader.async_
705 706
         def go1(context):
706 707
             context.session.execute("test1")
707 708
             go2(context)
@@ -741,10 +742,30 @@ class MockFacadeTest(oslo_test_base.BaseTestCase):
741 742
                 with self._assert_writer_session(makers) as session:
742 743
                     session.execute("test2")
743 744
 
745
+    def test_deprecated_async_reader_name(self):
746
+        if sys.version_info >= (3, 7):
747
+            self.skipTest("Test only runs on Python < 3.7")
748
+
749
+        context = oslo_context.RequestContext()
750
+
751
+        old = getattr(enginefacade.reader, "async")
752
+
753
+        @old
754
+        def go1(context):
755
+            context.session.execute("test1")
756
+
757
+        go1(context)
758
+
759
+        with self._assert_engines() as engines:
760
+            with self._assert_makers(engines) as makers:
761
+                with self._assert_async_reader_session(
762
+                        makers, assert_calls=False) as session:
763
+                    session.execute("test1")
764
+
744 765
     def test_async_reader_then_reader_ok(self):
745 766
         context = oslo_context.RequestContext()
746 767
 
747
-        @enginefacade.reader.async
768
+        @enginefacade.reader.async_
748 769
         def go1(context):
749 770
             context.session.execute("test1")
750 771
 

Loading…
Cancel
Save