Skip to content

[Python AST] AST Viewer shows [Str] but no such type exists #9833

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

Closed
fcasal opened this issue Jul 15, 2022 · 6 comments · Fixed by #9852
Closed

[Python AST] AST Viewer shows [Str] but no such type exists #9833

fcasal opened this issue Jul 15, 2022 · 6 comments · Fixed by #9852
Labels
Python question Further information is requested

Comments

@fcasal
Copy link

fcasal commented Jul 15, 2022

Description of the issue

I expect the type names present on the AST viewer to match a real CodeQL type, but this is not the case here.
The type could be either Str_ or StrConst, both printing "Str" as their toString().

The Str_ class also mentions the Str class which I cannot find:

/** INTERNAL: See the class `Str` for further information. */

@fcasal fcasal added the question Further information is requested label Jul 15, 2022
@aeisenberg
Copy link
Contributor

What is the code snippet that produces this AST?

@fcasal
Copy link
Author

fcasal commented Jul 15, 2022

Hi @aeisenberg,
you can reproduce with a simple file such as

class A:
    NAME = "A"

I created the database with codeql database create test.db --language=python and the AST for that file looks like
image

@aeisenberg
Copy link
Contributor

Thanks. I am asking our python team for a detailed answer.

@tausbn
Copy link
Contributor

tausbn commented Jul 15, 2022

Thank you for your question. In this case, StrConst is the correct class. It's not immediately obvious why this class isn't called Str, and at this point the reason is probably lost to the mists of time.

Unfortunately, because AstGenerated.qll is generated automatically, it's not trivial to make it refer to StrConst instead of Str in the corresponding QLDoc.

In the short term, we'll probably just override the toString method on StrConst to say StrConst (instead of inheriting the string Str from the Str_ class).

@fcasal
Copy link
Author

fcasal commented Jul 15, 2022

In the short term, we'll probably just override the toString method on StrConst to say StrConst (instead of inheriting the string Str from the Str_ class).

This makes total sense to me, and if you'd like I can open a PR that does it.

@tausbn
Copy link
Contributor

tausbn commented Jul 19, 2022

This makes total sense to me, and if you'd like I can open a PR that does it.

I mean, I certainly won't prevent you from doing that, but I think you'll find it's going to be quite a lot of hassle to update all of the tests that currently say Str to instead say StrConst. 🙂

I'm wondering if a less painful solution would be to add a Str class so that StrConst extends Str, and Str extends Str_. Then the QLDoc on Str_ would make sense again, and the QLDoc for Str could just say "placeholder class -- see StrConst" and none of the tests would need to be updated.

That way, a user of the AST viewer would at least have a fighting chance of finding the right class to look at.

I'll make a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Python question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants