-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Line number of docstring in AST #74682
Comments
Since bpo-29463, it is no longer obvious how to get the line number of a docstring in the AST: import ast
x = ast.parse('''\
def foo():
"""This is a docstring."""
''')
# In Python 3.6, the docstring and line number would be:
print(x.body[0].body[0].value.s)
print(x.body[0].body[0].value.lineno)
# In Python 3.7, I don't know what the equivalent would be.
print(x.body[0].docstring)
# Line number? We use this feature in pyflakes (PyCQA/pyflakes#271). |
It is not possible anymore. Why you need the line number of a docstring? The closest approximation is between node.lineno and node.body[0].lineno (if node.body is not empty). |
AST is not designed to be 1-to-1 .py source to AST mapping. If you need the exact line number, you might use your own parser. I don't know what is using pylint for example? There is also https://github.com/PyCQA/redbaron which provides such 1-to-1 mapping but it has a different API than AST. Sadly, I suggest to close this issue as WONTFIX. |
Even if the line number of a docstring is known, it is not easy to determine the line number corresponding to the particular line in a docstring if it contains backslashes following by newline. '''foo and '''\ are equal strings, but the lines 'bar' have different offsets from the start of the strings. The only robust method was parsing the source code starting from the start of a docstring. Now you just need to start parsing from the start of the function/class. |
I think what you guys have brought up makes sense. Now that you mention it, I see that pyflakes gives the wrong line number if an escaped newline appears after the doctests. Though, it works fine if the escaped newline appears before it. This is a non-default feature in pyflakes anyway, so we can live with it. Thanks |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: