question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System assigns result in invalid syntax

See original GitHub issue

If I have a cell with

flake8_version = !flake8 --version
flake8_version

, then this executes fine.

However, the equivalent Python representation of it from jupytext is:

# %%
flake8_version = !flake8 --version
flake8_version

which is invalid syntax (and not something I could run flake8 on 😄 )

I’m bringing this up because in https://github.com/mwouts/jupytext/issues/781 you commented

magic commands are always commented out by default

but this one wasn’t commented out.

From a quick glance at the source code, it looks like magics are detected using regular expressions - have you considered using IPython’s TransformerManager and then ast-parsing instead? I’m happy to help out with this if you want greater support for magic detection (at least, in Python notebooks)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, Jul 14, 2021

Other than the official does, I don’t have any extra documentation (other than the docstrings in src/black/handle_ipynb_magics.py from https://github.com/psf/black/pull/2357/files )

1reaction
MarcoGorellicommented, Jul 12, 2021

Also, maybe changing this for everyone would cause many diffs on existing notebooks.

Agreed - but you can keep the commented out magics using this approach. You just use TransformerManager to detect where the magics are, then you “extract” the magic using ast.parse, then you comment / uncomment - so the script would still look like the notebook

In https://github.com/psf/black/pull/2357 I replace the magics with randomly generated tokens, but they could just as easily be replaced with commented-out versions of themselves

I’ll put something together to demonstrate when I get a chance

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid Syntax in Python: Common Reasons for SyntaxError
Misusing the Assignment Operator ( = ) ... The first example tries to assign the value 5 to the len() call. The SyntaxError...
Read more >
python - Why do I get the syntax error "SyntaxError: invalid ...
, I get an error message: SyntaxError: invalid syntax . Pmin , Pmax , w , fi1 and fi2 have all been assigned...
Read more >
SyntaxError: invalid syntax - Python Morsels
Python's "invalid syntax" error message comes up often, especially when you're first learning Python. What usually causes this error and how can you...
Read more >
Syntaxerror: invalid syntax (solved) - Classical Finance
Why am I getting syntax errors · A missing assignment operator · Python keyword error · Opening and closing symbols · Not escaping...
Read more >
I wrote the following code and got the following error message.
I've entered these two lines multiple times now with the same result. ... To assign a value to a variable, use the following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found