Skip to content
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

Add nodes.Const.kind for r prefix and Python < 3.8 #1156

Open
DanielNoord opened this issue Aug 31, 2021 · 4 comments
Open

Add nodes.Const.kind for r prefix and Python < 3.8 #1156

DanielNoord opened this issue Aug 31, 2021 · 4 comments
Labels
ast Enhancement ✨ Improvement to a component

Comments

@DanielNoord
Copy link
Collaborator

DanielNoord commented Aug 31, 2021

Current behavior

Following discussion in pylint-dev/pylint#4787 additional functionality for the nodes.Const.kind attribute would be helpful.
See documentation for current behaviour, which only supports the u prefix and Python =< 3.8

Expected behavior

For the following code, inferring a_string and checking its kind should return r.

Note: at the same time making kind work with the f prefix or combined prefixes such as fr might be possible

a_string = r"Test string"
@Pierre-Sassoulas
Copy link
Member

Thank you for creating the issue :)

@cdce8p
Copy link
Member

cdce8p commented Aug 31, 2021

Unfortunately, I don't think this is possible at the moment. The Const.kind value is extracted from the Constant ast node. That is only ever u or None.

import ast
import sys

code = """
a_string = r"Test string"
"""

if __name__ == '__main__':
    ast_node = ast.parse(code)
    if sys.version_info >= (3, 9):
        print(ast.dump(ast_node, indent=2))
    else:
        print(ast.dump(ast_node))

--
python/cpython#12295
https://bugs.python.org/issue36280
#943

@DanielNoord
Copy link
Collaborator Author

I was thinking of looking for the column number of the string and doing columnno - 1 to look for the prefix and handle some edge cases. Is this something worth trying, or do you foresee problems which such a basic approach?

@cdce8p
Copy link
Member

cdce8p commented Aug 31, 2021

I was thinking of looking for the column number of the string and doing columnno - 1 to look for the prefix and handle some edge cases. Is this something worth trying, or do you foresee problems which such a basic approach?

It might work although there might be issues to be aware of. Just to name one: The Const node can contain not just strings, but bytes, ints, bool, None, ... as well. I would need to investigate it further at some point.

If you like, you can take a look at it yourself. The logic should probably be part of nodes.Const.__init__(...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ast Enhancement ✨ Improvement to a component
Projects
None yet
Development

No branches or pull requests

3 participants