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

ci: add github action that analyzes all python functions #5516

Closed

Conversation

a-dubs
Copy link
Collaborator

@a-dubs a-dubs commented Jul 17, 2024

This new github action runs a check on each the source branch and the target branch and finds all functions that are missing docstrings or functions that are missing type hints. And by checking if the number of either of those two has increased in the source branch, then it knows that new code was added that is missing either a docstring or type hints.

Proposed Commit Message

ci: add github action that analyzes all python functions

This new github action runs a check on each the source branch
and the target branch and finds all functions that are missing
docstrings or functions that are missing type hints. And by
checking if the number of either of those two has increased in the
source branch, then it knows that new code was added that is
missing either a docstring or type hints.

Additional Context

Test Steps

Merge type

  • Squash merge using "Proposed Commit Message"
  • Rebase and merge unique commits. Requires commit messages per-commit each referencing the pull request number (#<PR_NUM>)

This new github action runs a check on each the source branch
and the target branch and finds all functions that are missing
docstrings or functions that are missing type hints. And by
checking if the number of either of those two has increased in the
source branch, then it knows that new code was added that is
missing either a docstring or type hints.

- name: Analyze target branch
run: |
python tools/analyze_functions.py analyze target_functions_info.yml cloudinit tests
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

maybe remove tests dir??

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

todo: add ability to specify dirs per docstrings and type hints check

with:
ref: ${{ github.base_ref }}

- name: Download source analysis result
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add comments for why this is needed

@@ -0,0 +1,50 @@
# .github/workflows/analyze_functions.yml
name: Analyze Python Functions
Copy link
Collaborator

Choose a reason for hiding this comment

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

james: rename request from something vague to better describe what this workflow is doing on CI reports

@@ -0,0 +1,134 @@
import ast
Copy link
Collaborator

Choose a reason for hiding this comment

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

james: add a docstr here to better describe the intent of this module and how/why to run it.

Copy link
Collaborator Author

@a-dubs a-dubs left a comment

Choose a reason for hiding this comment

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

Synchronous review done w/ @TheRealFalcon @blackboxsw @aciba90 @catmsred

@@ -0,0 +1,134 @@
import ast
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add detailed docstring about what it does, and how it is used, give some examples

print(f"Total functions without type hints: {count_functions(functions_info, 'functions_without_type_hints')}")
print(f"Total functions without docstrings: {count_functions(functions_info, 'functions_without_docstrings')}")

def compare(source_file, target_file):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

and docstring here


def has_docstring(node):
"""Check if a node has a docstring."""
return isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Str)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

refactor this to have local variables with descriptive names!!

return isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Str)

def find_functions_info(directories):
total_number_of_functions = 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

move this below docstring

import argparse
import sys

def has_type_hint(node):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add type hints to all functions plz

run: |
python tools/analyze_functions.py analyze target_functions_info.yml cloudinit tests

- name: Compare analysis results
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

break this into two different steps - one for comparing docstrings and one for type hints. this will help with readability and visibility of github action

target_functions_without_docstrings = set(target_info.get(file, {}).get('functions_without_docstrings', []))
new_docstrings = source_functions_without_docstrings - target_functions_without_docstrings
if new_docstrings:
new_functions_without_docstrings.extend([f"{file}: {func}" for func in new_docstrings])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mayhaps break this duplicate code (lines 84-94) into a helper function

parser = argparse.ArgumentParser(description='Analyze Python functions in directories or compare results.')
subparsers = parser.add_subparsers(dest='command')

analyze_parser = subparsers.add_parser('analyze', help='Analyze the functions in directories.')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

split this into two different commands: one for docstrings and one for type hints. this will then make it so that two yml files are generated, one for each. so changes will need to be made to github action as well.

Copy link

Hello! Thank you for this proposed change to cloud-init. This pull request is now marked as stale as it has not seen any activity in 14 days. If no activity occurs within the next 7 days, this pull request will automatically close.

If you are waiting for code review and you are seeing this message, apologies! Please reply, tagging TheRealFalcon, and he will ensure that someone takes a look soon.

(If the pull request is closed and you would like to continue working on it, please do tag TheRealFalcon to reopen it.)

@github-actions github-actions bot added the stale-pr Pull request is stale; will be auto-closed soon label Aug 13, 2024
@github-actions github-actions bot closed this Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale-pr Pull request is stale; will be auto-closed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants