Skip to content

Python: Allow absolute imports in directories with scripts #5506

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

Merged

Conversation

tausbn
Copy link
Contributor

@tausbn tausbn commented Mar 23, 2021

Fixes the import logic to account for absolute imports.

We do this by classifying which files and folders may serve as the
entry point for execution, based on a few simple heuristics. If the
file module.py is in the same folder as a file main.py that may be
executed directly, then we allow module to be a valid name for
module.py so that import module will work as expected.


There are still some issues around namespace packages, but the fix for these is beyond the scope of the present PR.

The first commit message goes into quite some detail about the peculiarities of the test setup.

As this is fixing a minor but annoying bug, I don't think a change note is necessary (but I will gladly be convinced otherwise).

tausbn added 2 commits March 23, 2021 18:21
This one will require some explanation...

First, the file structure. This commit adds a test consisting
representing a few different kinds of imports.

- Absolute imports, from `module.py` to `main.py` when the latter is
  executed directly.
- A package (contained in the `package` folder)
- A namespace package (contained in the `namespace_package` folder)

All of these are inside a folder called `code` for reasons I will
detail later.

The file `main.py` is identified as a script, by the presence of the
`!#` comment in its first line.

The files themselves are executable, and `python3 main.py` will print
out all modules in the order they are imported.

The test itself is very simple. It simply lists all modules and their
corresponding names. As is plainly visible, without modification we
only pick up `package` and its component modules as having names. This
is the bit that needs to be fixed.

Convincing the test runner to extract this test in a way that mimics
reality is, unfortunately, a bit complicated. By default, the test
runner itself includes any Python files in the test directory as
modules in the invocation of the extractor, and so we must hide
everything in the `code` subdirectory.

Secondly, a `--path` argument (set to the test directory) is
automatically added, and this would also interfere with extraction,
and hence we must prevent this. Luckily, if we supply our own `--path`
argument -- even if it doesn't make any sense -- then the other
argument is left out.

Finally, we must actually tell the extractor to extract the files (or
it would just happily pass the test with zero files extracted), so the
`-R .` argument ensures that we recurse over the files in the test
directory after all.
Fixes the import logic to account for absolute imports.

We do this by classifying which files and folders may serve as the
entry point for execution, based on a few simple heuristics. If the
file `module.py` is in the same folder as a file `main.py` that may be
executed directly, then we allow `module` to be a valid name for
`module.py` so that `import module` will work as expected.
@tausbn tausbn added the no-change-note-required This PR does not need a change note label Mar 23, 2021
@tausbn tausbn requested a review from a team as a code owner March 23, 2021 17:42
yoff
yoff previously approved these changes Mar 24, 2021
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems very reasonable

Comment on lines 81 to 84
// The file doesn't have the extension `.py` but still contains Python statements
not this.getExtension() = "py" and
exists(Stmt s | s.getLocation().getFile() = this)
or
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we seen this? Can we have a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't personally seen it in the wild, but I based the above on this similar construction in ObjectAPI.qll, so I assume there's a reason for it. (I added the check that the file contains Python statements, because we don't want random non-Python files to affect the status of the Python files in the same directory.)

That being said, I'm not sure we can test this without modifying the extractor. I tried renaming main.py to have a non-.py extension, and I can include it directly using --filter:include=..., but even though the file appears in the database, it seems we're not interpreting it as Python code. My best guess is that it is being filtered out by a more restrictive "is this a script" check in the extractor, which seems unfortunate.

I'm also thinking that the above should read this.getExtension().matches("py%"), since there are many Python-related files that have extensions starting with py, and we certainly don't want to have those affecting the analysis, even if they do contain Python statements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think it is fine to have this structure in place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I do this every now and then if I write a small helper script in Python, that just needs to look like an executable. With the #!/usr/bin/env python everything works out anyway 👍

OHH, but that is covered by the exists(Comment c | case... So I guess this is for python my-script-without-extension, which does seem strange.

I'm also thinking that the above should read this.getExtension().matches("py%"), since there are many Python-related files that have extensions starting with py, and we certainly don't want to have those affecting the analysis, even if they do contain Python statements.

Hmm, is this about .pyc, .pyo or .pyd files? I'm under the impression that since they contain byte-code, that wouldn't be valid Python statements.

(This last point is not super import to me, just curious)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, not all .py? files contain Python code, so those would be filtered out anyway. I was thinking more of stub files like .pyi which do contain valid Python, but not code that is actually executed as a script.

(I also found this random list of Python extensions. There are more than I had expected.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about .pyi 👍 I personally didn't know about .py3 usage either 😄

tausbn added 3 commits March 24, 2021 13:15
Note that the test in `no_py_extension` isn't complete, since we're
not extracting the `main` file there.
Sadly, it seems we're not interpreting this as Python code, even if we
explicitly ask to have it included.
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yoff yoff merged commit 8d15680 into github:main Mar 24, 2021
@yoff
Copy link
Contributor

yoff commented Mar 24, 2021

Merging this now as I believe it fixes a bunch of grievances. We can always refine it later..

@tausbn tausbn deleted the python-allow-absolute-imports-from-source-directory branch March 24, 2021 16:41
RasmusWL added a commit to RasmusWL/codeql that referenced this pull request Mar 29, 2021
…ute-imports-from-source-directory"

This reverts commit 8d15680, reversing
changes made to 63831cc.

This PR caused performance problems, so reverting now to clear up immediate
problems.
calumgrant added a commit that referenced this pull request Mar 31, 2021
Python: Revert #5506 due to bad performance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants