From: Iuri de Silvio Date: Mon, 4 Dec 2023 16:55:24 +0000 (-0500) Subject: Fix `get_x_arguments(as_dictionary=True)` for args without `=` X-Git-Tag: rel_1_13_1~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0fc08799d62f32553ff0e4b629a0d4371ece6a09;p=thirdparty%2Fsqlalchemy%2Falembic.git Fix `get_x_arguments(as_dictionary=True)` for args without `=` 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 --- diff --git a/alembic/context.pyi b/alembic/context.pyi index aef17455..e8d98210 100644 --- a/alembic/context.pyi +++ b/alembic/context.pyi @@ -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:: diff --git a/alembic/runtime/environment.py b/alembic/runtime/environment.py index 74672d7a..34ae1847 100644 --- a/alembic/runtime/environment.py +++ b/alembic/runtime/environment.py @@ -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 index 00000000..ce77204c --- /dev/null +++ b/docs/build/unreleased/1370.rst @@ -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. diff --git a/tests/test_environment.py b/tests/test_environment.py index d9c14ca4..5fc70401 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -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")