Use ipython's own shell_expand

Fixes #194
pull/214/head
Catherine Devlin 2022-06-10 08:16:29 -05:00
parent 12f9742a0a
commit bb64862303
4 changed files with 35 additions and 11 deletions

4
.gitignore vendored
View File

@ -35,4 +35,6 @@ nosetests.xml
.pydevproject
# Pycharm
/.idea
/.idea
.venv

View File

@ -4,6 +4,9 @@ Development setup
Running nose tests with IPython is tricky, so there's a
run_tests.sh script for it.
pip install -e .
./run_tests.sh
To temporarily insert breakpoints for debugging: `from nose.tools import set_trace; set_trace()`
Tests have requirements not installed by setup.py:

View File

@ -147,16 +147,18 @@ class SqlMagic(Magics, Configurable):
"""
# Parse variables (words wrapped in {}) for %%sql magic (for %sql this is done automatically)
cell_variables = [
fn for _, fn, _, _ in Formatter().parse(cell) if fn is not None
]
cell_params = {}
for variable in cell_variables:
if variable in local_ns:
cell_params[variable] = local_ns[variable]
else:
raise NameError(variable)
cell = cell.format(**cell_params)
cell = self.shell.var_expand(cell)
# cell_variables = [
# fn for _, fn, _, _ in Formatter().parse(cell) if fn is not None
# ]
# cell_params = {}
# for variable in cell_variables:
# if variable in local_ns:
# cell_params[variable] = local_ns[variable]
# else:
# raise NameError(variable)
# cell = cell.format(**cell_params)
line = sql.parse.without_sql_comment(parser=self.execute.parser, line=line)
args = parse_argstring(self.execute, line)

View File

@ -370,3 +370,20 @@ def test_multiline_bracket_var_substitution(ip):
""",
)
assert not result
def test_json_in_select(ip):
# Variable expansion does not work within json, but
# at least the two usages of curly braces do not collide
ip.user_global_ns["person"] = "prince"
result = ip.run_cell_magic(
"sql",
"",
"""
sqlite://
SELECT
'{"greeting": "Farewell sweet {person}"}'
AS json
""",
)
assert ('{"greeting": "Farewell sweet {person}"}',)