]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix `get_x_arguments(as_dictionary=True)` for args without `=`
authorIuri de Silvio <iuri.desilvio@channable.com>
Mon, 4 Dec 2023 16:55:24 +0000 (11:55 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 4 Dec 2023 17:04:14 +0000 (12:04 -0500)
Fixed issue where ``get_x_arguments(as_dictionary=True)`` would fail if an
argument key were passed without an equal sign ``=`` or a value.
Behavior is repaired where this condition is detected and will return a
blank string for the given key, consistent with the behavior where the
``=`` sign is present and no value.  Pull request courtesy Iuri de Silvio.

Fixes: #1369
Closes: #1370
Pull-request: https://github.com/sqlalchemy/alembic/pull/1370
Pull-request-sha: 830a69076653d0849f0b3eba2ecbf6ff16700049

Change-Id: I610d2d9022a0a08e56e0f62f6890f3f0d5bc169a

alembic/context.pyi
alembic/runtime/environment.py
docs/build/unreleased/1370.rst [new file with mode: 0644]
tests/test_environment.py

index aef1745555553e682660ed23af96229e54bd7835..e8d98210d08e17cacbef66bc7ce3352130bfa229 100644 (file)
@@ -759,7 +759,11 @@ def get_x_argument(
     The return value is a list, returned directly from the ``argparse``
     structure.  If ``as_dictionary=True`` is passed, the ``x`` arguments
     are parsed using ``key=value`` format into a dictionary that is
-    then returned.
+    then returned. If there is no ``=`` in the argument, value is an empty
+    string.
+
+    .. versionchanged:: 1.13.1 Support ``as_dictionary=True`` when
+       arguments are passed without the ``=`` symbol.
 
     For example, to support passing a database URL on the command line,
     the standard ``env.py`` script can be modified like this::
index 74672d7ac3b1a331a3b449431dcabecf7157cd7e..34ae1847711d27c944947fa8cb98e30551595b47 100644 (file)
@@ -367,7 +367,11 @@ class EnvironmentContext(util.ModuleClsProxy):
         The return value is a list, returned directly from the ``argparse``
         structure.  If ``as_dictionary=True`` is passed, the ``x`` arguments
         are parsed using ``key=value`` format into a dictionary that is
-        then returned.
+        then returned. If there is no ``=`` in the argument, value is an empty
+        string.
+
+        .. versionchanged:: 1.13.1 Support ``as_dictionary=True`` when
+           arguments are passed without the ``=`` symbol.
 
         For example, to support passing a database URL on the command line,
         the standard ``env.py`` script can be modified like this::
@@ -401,7 +405,12 @@ class EnvironmentContext(util.ModuleClsProxy):
         else:
             value = []
         if as_dictionary:
-            value = dict(arg.split("=", 1) for arg in value)
+            dict_value = {}
+            for arg in value:
+                x_key, _, x_value = arg.partition("=")
+                dict_value[x_key] = x_value
+            value = dict_value
+
         return value
 
     def configure(
diff --git a/docs/build/unreleased/1370.rst b/docs/build/unreleased/1370.rst
new file mode 100644 (file)
index 0000000..ce77204
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, environment
+    :tickets: 1369
+
+    Fixed issue where ``get_x_arguments(as_dictionary=True)`` would fail if an
+    argument key were passed without an equal sign ``=`` or a value.
+    Behavior is repaired where this condition is detected and will return a
+    blank string for the given key, consistent with the behavior where the
+    ``=`` sign is present and no value.  Pull request courtesy Iuri de Silvio.
index d9c14ca45cf19cbc6ed47ef700c750ae5c60bce8..5fc70401656c10f71d7ef6df50ccd0d1c7d65905 100644 (file)
@@ -54,6 +54,11 @@ class EnvironmentTest(TestBase):
         env = self._fixture()
         eq_(env.get_x_argument(as_dictionary=True), {})
 
+    def test_x_arg_empty_value(self):
+        env = self._fixture()
+        self.cfg.cmd_opts = mock.Mock(x=["y"])
+        eq_(env.get_x_argument(as_dictionary=True), {"y": ""})
+
     def test_tag_arg(self):
         env = self._fixture(tag="x")
         eq_(env.get_tag_argument(), "x")