Skip to content

Commit

Permalink
Fixed: sqlalchemy#1369 get_x_arguments(as_dictionary=True) for args…
Browse files Browse the repository at this point in the history
… without `=`
  • Loading branch information
Iuri de Silvio committed Dec 4, 2023
1 parent 971b0b4 commit 3494023
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion alembic/context.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,8 @@ 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.
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,8 @@ 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.
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 +402,15 @@ def get_x_argument(
else:
value = []
if as_dictionary:
value = dict(arg.split("=", 1) for arg in value)
dict_value = {}
for arg in value:
try:
x_key, x_value = arg.split("=", 1)
except ValueError:
x_key, x_value = arg, ""
dict_value[x_key] = x_value
value = dict_value

return value

def configure(
Expand Down
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 3494023

Please sign in to comment.