jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.59k stars 4.86k forks source link

Curly braces break %time #4140

Open armamut opened 5 years ago

armamut commented 5 years ago

While running this trivial example is fine:

'{"abc", "def", "ghi"}'.replace('{','').replace('}','')

running it with %time magic gives error.

%time '{"abc", "def", "ghi"}'.replace('{','').replace('}','')

Traceback (most recent call last):

  File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3265, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-6-5a0bb0dbe481>", line 1, in <module>
    get_ipython().run_line_magic('time', '\'{"abc", "def", "ghi"}\'.replace(\'{\',\'\').replace(\'}\',\'\')')

  File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2285, in run_line_magic
    result = fn(*args,**kwargs)

  File "<decorator-gen-62>", line 2, in time

  File "/usr/local/lib/python3.7/site-packages/IPython/core/magic.py", line 187, in <lambda>
    call = lambda f, *a, **k: f(*a, **k)

  File "/usr/local/lib/python3.7/site-packages/IPython/core/magics/execution.py", line 1202, in time
    expr_ast = self.shell.compile.ast_parse(expr)

  File "/usr/local/lib/python3.7/site-packages/IPython/core/compilerop.py", line 100, in ast_parse
    return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)

  File "<unknown>", line 1
    '('abc', 'def', 'ghi')'.replace(',).replace(','')
         ^
SyntaxError: invalid syntax

jupyter --version is 4.4.0 jupyter notebook --version is 5.7.0

Carreau commented 5 years ago

That is an IPython feature. When using magics braces get expanded to variable content. You need to double them to actually insert a brace.

armamut commented 5 years ago

That is an IPython feature. When using magics braces get expanded to variable content. You need to double them to actually insert a brace.

Oh, I see. Should I close this issue then?

armamut commented 5 years ago

Sorry, I have accidentally closed it. Thank for the reply :)

Carreau commented 5 years ago

We can try to catch the SyntaxError and see if we can add a warning. Thanks for the report.

mgeier commented 5 years ago

That is an IPython feature.

See https://github.com/ipython/ipython/issues/10920.