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 .pydevproject
# Pycharm # Pycharm
/.idea /.idea
.venv

View File

@ -4,6 +4,9 @@ Development setup
Running nose tests with IPython is tricky, so there's a Running nose tests with IPython is tricky, so there's a
run_tests.sh script for it. 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()` To temporarily insert breakpoints for debugging: `from nose.tools import set_trace; set_trace()`
Tests have requirements not installed by setup.py: 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) # Parse variables (words wrapped in {}) for %%sql magic (for %sql this is done automatically)
cell_variables = [ cell = self.shell.var_expand(cell)
fn for _, fn, _, _ in Formatter().parse(cell) if fn is not None
] # cell_variables = [
cell_params = {} # fn for _, fn, _, _ in Formatter().parse(cell) if fn is not None
for variable in cell_variables: # ]
if variable in local_ns: # cell_params = {}
cell_params[variable] = local_ns[variable] # for variable in cell_variables:
else: # if variable in local_ns:
raise NameError(variable) # cell_params[variable] = local_ns[variable]
cell = cell.format(**cell_params) # else:
# raise NameError(variable)
# cell = cell.format(**cell_params)
line = sql.parse.without_sql_comment(parser=self.execute.parser, line=line) line = sql.parse.without_sql_comment(parser=self.execute.parser, line=line)
args = parse_argstring(self.execute, line) args = parse_argstring(self.execute, line)

View File

@ -370,3 +370,20 @@ def test_multiline_bracket_var_substitution(ip):
""", """,
) )
assert not result 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}"}',)