This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ast.parse parses string literals as docstrings
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Valentin.Lorentz, methane, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-02-25 10:11 by Valentin.Lorentz, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg288553 - (view) Author: ProgVal (Valentin.Lorentz) Date: 2017-02-25 10:11
Since commit cb41b2766de646435743b6af7dd152751b54e73f (Python 3.7a0), string literals are not parsed the same way.

ast.parse("'test'").body used to be a list with one item containing 'test'; but now it is an empty list:


Python 3.5.2+ (default, Dec 13 2016, 14:16:35) 
[GCC 6.2.1 20161124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> print(ast.parse("'test'"))
<_ast.Module object at 0x7fa37ae4c630>
>>> print(ast.parse("'test'").body)
[<_ast.Expr object at 0x7fa37ae4c630>]
>>> print(ast.parse("'test'").docstring)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Module' object has no attribute 'docstring'





Python 3.7.0a0 (default, Feb 24 2017, 21:38:30) 
[GCC 6.3.0 20170205] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> print(ast.parse("'test'"))
<_ast.Module object at 0x7fe2aa415eb8>
>>> print(ast.parse("'test'").body)
[]
>>> print(ast.parse("'test'").docstring)
test
msg288554 - (view) Author: ProgVal (Valentin.Lorentz) Date: 2017-02-25 10:13
(Oops, submitted too soon.)

I understand the rational of this change, so I am not sure if this is actually a bug.

However, if someone wants to parse a simple expression that may be a string, they need to add a special handling in case it's a string interpreted as a module docstring.
msg288555 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-25 10:18
If someone wants to parse a simple expression that may be a string he should use the "eval" mode.

>>> ast.dump(ast.parse("'test'"))
"Module(body=[], docstring='test')"
>>> ast.dump(ast.parse("'test'", mode='eval'))
"Expression(body=Str(s='test'))"
msg288556 - (view) Author: ProgVal (Valentin.Lorentz) Date: 2017-02-25 10:34
Indeed, thanks.

I should have done that when I migrated from compiler.parse.
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73832
2017-02-25 10:34:29Valentin.Lorentzsetstatus: open -> closed
resolution: not a bug
messages: + msg288556

stage: resolved
2017-02-25 10:18:17serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg288555
2017-02-25 10:13:15Valentin.Lorentzsetmessages: + msg288554
2017-02-25 10:11:14Valentin.Lorentzcreate