Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get_x_arguments(as_dictionary=True) for args without = #1370

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

iurisilvio marked this conversation as resolved.
Show resolved Hide resolved
.. versionchanged:: 1.13.1 Support to `as_dictionary=True` and args
without `=`, generating an empty string instead of crashing.
CaselIT marked this conversation as resolved.
Show resolved Hide resolved

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.
iurisilvio marked this conversation as resolved.
Show resolved Hide resolved

.. versionchanged:: 1.13.1 Support to `as_dictionary=True` and args
without `=`, generating an empty string instead of crashing.
CaselIT marked this conversation as resolved.
Show resolved Hide resolved

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
7 changes: 7 additions & 0 deletions docs/build/unreleased/1370.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. change::
:tags: bug, autogenerate
:tickets: 1370

Fixed issue with ``get_x_arguments(as_dictionary=True)`` and args without
``=``. Now args without ``=`` are supported and generate an empty string as
their value.
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