Skip to content

Commit

Permalink
Fix get_x_arguments(as_dictionary=True) for args without =
Browse files Browse the repository at this point in the history
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: #1370
Pull-request-sha: 830a690

Change-Id: I610d2d9022a0a08e56e0f62f6890f3f0d5bc169a
  • Loading branch information
Iuri de Silvio authored and zzzeek committed Dec 4, 2023
1 parent 971b0b4 commit 0fc0879
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion alembic/context.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
13 changes: 11 additions & 2 deletions alembic/runtime/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,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::
Expand Down Expand Up @@ -401,7 +405,12 @@ def get_x_argument(
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(
Expand Down
9 changes: 9 additions & 0 deletions docs/build/unreleased/1370.rst
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_x_arg_no_opts_asdict(self):
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")
Expand Down

0 comments on commit 0fc0879

Please sign in to comment.