Skip to content

Added support for python scripts. #1415#1419

Merged
rchen152 merged 6 commits intogoogle:mainfrom
tjnangosha:tinker
May 12, 2023
Merged

Added support for python scripts. #1415#1419
rchen152 merged 6 commits intogoogle:mainfrom
tjnangosha:tinker

Conversation

@tjnangosha
Copy link
Copy Markdown
Contributor

Regarding issue #1415, I have added support for Python scripts by checking for a shebang at the start of the file with the path to the Python executable.

One thing to note with this approach is that it will check all files in the directory/those that you provide to see if they are python executable scripts. That is, even if you have a file called main.html, but it has a python shebang line at the top and has python code, then it will be type checked.

This works for me, but I am also looking forward to any opinions that could provide more robust solutions.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 29, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@rchen152
Copy link
Copy Markdown
Contributor

rchen152 commented May 9, 2023

Sorry for the slow response! This looks like it would open and read every file in a directory and subdirectories to find Python scripts? That seems like it would be too slow to be practical, unfortunately.

What do you think of only allowing directly specified files to have non-.py extensions? That is, if you run pytype my_script, then my_script should be analyzed, but pytype will still only search for .py files when looking for dependencies of my_script.

@tjnangosha
Copy link
Copy Markdown
Contributor Author

@rchen152. That makes sense. I'll get on it right away.

@tjnangosha
Copy link
Copy Markdown
Contributor Author

@rchen152, have added another commit to only check for python scripts when a non-.py file is specified.

Copy link
Copy Markdown
Contributor

@rchen152 rchen152 left a comment

Choose a reason for hiding this comment

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

Thanks! A few comments.

Comment thread pytype/file_utils.py Outdated
if path_utils.isfile(file_path):
with open(file_path, 'r') as file:
line = file.readline().rstrip().lower()
return line.startswith('#!') and (line.endswith('python') or line.endswith('python3'))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd recommend using a regex here. Something like return re.fullmatch(r'#!.+python3?', line) is not None should work.

Comment thread pytype/file_utils.py Outdated
# If we have a directory, collect all the .py files within it.
out += recursive_glob(path_utils.join(f, "**", "*.py"))
# If we have a directory, collect all the .py files within it....
out += recursive_glob(path_utils.join(f, "**", "*.py"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
out += recursive_glob(path_utils.join(f, "**", "*.py"))
out += recursive_glob(path_utils.join(f, "**", "*.py"))

Comment thread pytype/file_utils_test.py Outdated
def _test_expand(self, string):
with test_utils.Tempdir() as d:
fs = [d.create_file(f) for f in self.FILES]
fs += [d.create_file("my_script", self.SCRIPT_CODE)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is this line accomplishing? The test still asserts that expand_source_files only finds the .py files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Previously when I was checking every file in the directory to check if it was a script, this line would have made sense to make sure we don't miss a script file. Now that we only check for a file that is passed to pytype, this line looks redundant.

I will remove it. Thanks for pointing that out.

@tjnangosha
Copy link
Copy Markdown
Contributor Author

@rchen152, added changes as requested. Hope that is good.

Copy link
Copy Markdown
Contributor

@rchen152 rchen152 left a comment

Choose a reason for hiding this comment

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

Looking good! There are a couple of lint errors: https://github.com/google/pytype/actions/runs/4945740447/jobs/8861864375

  • There's extra whitespace on line 144.
  • The parameter name dir on line 145 conflicts with the dir builtin.

Once those are fixed, I think this will be ready to merge.

@tjnangosha
Copy link
Copy Markdown
Contributor Author

@rchen152, Done!!!!!!!!

Copy link
Copy Markdown
Contributor

@rchen152 rchen152 left a comment

Choose a reason for hiding this comment

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

Thanks for seeing this through!

@rchen152 rchen152 merged commit 37b7c2d into google:main May 12, 2023
rchen152 added a commit that referenced this pull request May 12, 2023
…1415

I merged the PR directly - it honestly just seems like a better experience for
external contributors. But we still need to copy the changes over so they don't
get overwritten.

PiperOrigin-RevId: 531600356
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants