You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python nightly (3.7 ?) remove bare strings. #11133
Can you use the "single" mode instead of the "exec" (default) mode when parse to AST?
Unfortunately no, as a single cell in ipython/jupyter can be a sequence of statements/expressions.
If we try single we get a syntax errors:
In [1]: print(1+1)
...: print(2+2)
File "<ipython-input-1-2cad5c9dfe23>", line 1
print(1+1)
^
SyntaxError: multiple statements found while compiling a single statement
Well, single would return and Interactive node, while exec would return a Module, so that would require messing with AST anyway. Which not more complicated or simple than what I did in #11135.
I also though of unconditionally prepending an empty string to the source code.
Ideally I'd really like a clean way to compiling a file/module from a sequence of statement.
I opened #11158 as a possibly simpler alternative. It recompiles empty modules with single and wraps that in a Module. It does technically 'mess with the ast' to get back a Module, but with a very minimal ast.Module(interactive.body). I also tested without wrapping it in a Module, and if we left it as Interactive everything would probably work because we really only ever do for node in module.body, but it's perhaps better to preserve the ast.Module assumption.
It's like @serhiy-storchaka's proposal, but the fallback is in the other direction, since most IPython inputs are multi-node inputs from notebooks these days, and currently this is only an issue for truly bare strings. This imposes the double-compile cost on the rarest and cheapest-compilation case, rather than the most-expensive long cells.
I don't view this as a major issue because executing cells that are just a string literal is pretty rare, and not displaying what you have necessarily just typed directly into the input isn't a huge regression for users.
Activity
Carreau commentedon May 12, 2018
cc @minrk , cc @takluyver
@njsmith do you have any idea about what could have caused this ?
Carreau commentedon May 12, 2018
Ok, that seem to be because now the first string in a module is actually tagged as the module docstring. See 3.6:
vs nightly:
So we likely need to special case empty module with docstring ? Or is it something we need to report upstream ?
njsmith commentedon May 12, 2018
You should report it upstream, if only so they have a chance to decide whether to do it on purpose...
serhiy-storchaka commentedon May 17, 2018
Can you use the "single" mode instead of the "exec" (default) mode when parse to AST?
Carreau commentedon May 17, 2018
Unfortunately no, as a single cell in ipython/jupyter can be a sequence of statements/expressions.
If we try
singlewe get a syntax errors:I modified the AST as a workaround in #11135
serhiy-storchaka commentedon May 24, 2018
Would it help if first try
single, and fallback toexec?Carreau commentedon May 24, 2018
Well,
singlewould return andInteractivenode, whileexecwould return aModule, so that would require messing with AST anyway. Which not more complicated or simple than what I did in #11135.I also though of unconditionally prepending an empty string to the source code.
Ideally I'd really like a clean way to compiling a file/module from a sequence of statement.
njsmith commentedon May 25, 2018
For the record, the upstream bug here is bpo-32911, and @ned-deily has delayed 3.7rc1 to allow for more discussion of whether this parsing change should be reverted or otherwise modified.
minrk commentedon May 25, 2018
I opened #11158 as a possibly simpler alternative. It recompiles empty modules with
singleand wraps that in a Module. It does technically 'mess with the ast' to get back a Module, but with a very minimalast.Module(interactive.body). I also tested without wrapping it in a Module, and if we left it as Interactive everything would probably work because we really only ever dofor node in module.body, but it's perhaps better to preserve theast.Moduleassumption.It's like @serhiy-storchaka's proposal, but the fallback is in the other direction, since most IPython inputs are multi-node inputs from notebooks these days, and currently this is only an issue for truly bare strings. This imposes the double-compile cost on the rarest and cheapest-compilation case, rather than the most-expensive long cells.
I don't view this as a major issue because executing cells that are just a string literal is pretty rare, and not displaying what you have necessarily just typed directly into the input isn't a huge regression for users.