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

Check the entire code base for compliance with DOC String standard #226

Merged
merged 45 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
6280437
Update DOC String Complaince
mandeepnh5 Jan 3, 2025
772d701
Fix coderabbit suggestions
mandeepnh5 Jan 3, 2025
30cfabc
Update needs in compliance job
mandeepnh5 Jan 3, 2025
c813032
Testing without compliance job
mandeepnh5 Jan 3, 2025
1bdd9f5
Update some issues
mandeepnh5 Jan 3, 2025
af936e2
Fix some test issues
mandeepnh5 Jan 4, 2025
85bf0d2
Fix some test issues
mandeepnh5 Jan 5, 2025
47be83d
Fix test issues
mandeepnh5 Jan 5, 2025
819f2c4
Fix package issue
mandeepnh5 Jan 5, 2025
bb2be9b
Intentional removing doc string
mandeepnh5 Jan 5, 2025
c952455
Update check_docstrings.py
mandeepnh5 Jan 5, 2025
df2a8a5
Fix excluded dir
mandeepnh5 Jan 6, 2025
aa5d14c
Remove raises
mandeepnh5 Jan 6, 2025
23830dd
Fix black format
mandeepnh5 Jan 6, 2025
119446a
Fix some issues
mandeepnh5 Jan 6, 2025
9f41fcc
Excluded tests
mandeepnh5 Jan 6, 2025
89c6a6d
Fix None parsing
mandeepnh5 Jan 6, 2025
a1f75dc
format black
mandeepnh5 Jan 6, 2025
2eae9da
Fix some issues
mandeepnh5 Jan 6, 2025
883bcc0
Add Args and returns for some files
mandeepnh5 Jan 7, 2025
2aed26f
Add Args and returns to some files
mandeepnh5 Jan 7, 2025
d53f61e
Fix linting issue
mandeepnh5 Jan 7, 2025
d846669
Fix multiline DOC issue
mandeepnh5 Jan 7, 2025
9990175
Merge branch 'autogenerate-pr-1' of https://github.com/mandeepnh5/swi…
mandeepnh5 Jan 7, 2025
d36b20a
Fix linting issue
mandeepnh5 Jan 7, 2025
d59221a
Fix some files
mandeepnh5 Jan 7, 2025
594c5ea
Fix linting
mandeepnh5 Jan 7, 2025
a038af4
Fix another file
mandeepnh5 Jan 7, 2025
d6985a7
Fix linting
mandeepnh5 Jan 7, 2025
548839d
Fix lint issue
mandeepnh5 Jan 7, 2025
14fd4f1
Fix interfaces DOC issue
mandeepnh5 Jan 7, 2025
80c9b92
Fix missing descriptions issue
mandeepnh5 Jan 7, 2025
26672dd
Fix linting issue
mandeepnh5 Jan 7, 2025
746fc8a
Fix some files
mandeepnh5 Jan 7, 2025
bedc1ae
Fix linting
mandeepnh5 Jan 7, 2025
14a72cc
Fix some issues
mandeepnh5 Jan 7, 2025
9cc9a63
Fix linting issue
mandeepnh5 Jan 7, 2025
b89382b
Fix some issues
mandeepnh5 Jan 7, 2025
47d3fc2
Fix linting issue
mandeepnh5 Jan 7, 2025
a71e931
Fix device file
mandeepnh5 Jan 7, 2025
0aaf832
Fix linting issue
mandeepnh5 Jan 7, 2025
2121dd5
Reverted back device file
mandeepnh5 Jan 7, 2025
32ea778
Fix linting issue
mandeepnh5 Jan 7, 2025
f32e0da
Add exlcude for device and snmp_manager file
mandeepnh5 Jan 7, 2025
23ea336
Fix some issues
mandeepnh5 Jan 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,21 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_REPOSITORY: ${{ github.repository }}

Docstring-Compliance:
name: Check Docstring Compliance
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: pip install -r requirements.txt

- name: Run docstring compliance check
run: python .github/workflows/scripts/check_docstrings.py
92 changes: 92 additions & 0 deletions .github/workflows/scripts/check_docstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import os
import re
import sys
from docstring_parser import parse

def validate_docstring(file_path):
"""
Validate docstrings in a file for compliance with the Google style guide.

Args:
file_path (str): Path to the Python file to validate.

Returns:
list: List of violations found in the file, with details about the issue and corrective action.
"""
violations = []
with open(file_path, 'r') as f:
lines = f.readlines()

for i, line in enumerate(lines):
if re.match(r'^\\s*def ', line):
func_name = line.strip()
docstring_start = i + 1
while docstring_start < len(lines) and lines[docstring_start].strip() == '':
docstring_start += 1

if docstring_start < len(lines) and lines[docstring_start].strip().startswith('\"\"\"'):
docstring_end = docstring_start + 1
while docstring_end < len(lines) and not lines[docstring_end].strip().endswith('\"\"\"'):
docstring_end += 1

docstring = '\\n'.join(lines[docstring_start:docstring_end + 1])
try:
parsed = parse(docstring)
# Check for Raises section
if not parsed.raises:
violations.append((
i + 1,
func_name,
"Missing 'Raises' section",
"Add a 'Raises' section detailing the exceptions this function may raise."
))
if not parsed.params:
violations.append((
i + 1,
func_name,
"Missing 'Args' section",
"Add an 'Args' section listing the arguments this function accepts, their types, and descriptions."
))
except Exception as e:
violations.append((
i + 1,
func_name,
"Docstring parsing error",
f"Ensure the docstring is properly formatted: {e}"
))
else:
violations.append((
i + 1,
func_name,
"Missing docstring",
"Add a Google-style docstring to describe this function."
))
return violations


def check_directory(directory):
"""Check all Python files in a directory for docstring compliance."""
all_violations = {}
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
file_path = os.path.join(root, file)
violations = validate_docstring(file_path)
if violations:
all_violations[file_path] = violations
return all_violations

def main():
directory = sys.argv[1] if len(sys.argv) > 1 else '.'
violations = check_directory(directory)
if violations:
print("Docstring violations found:")
for file, issues in violations.items():
for line, func, issue in issues:
print(f"{file}:{line}: {func}: {issue}")
sys.exit(1)
else:
print("All docstrings are compliant.")

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ graphql_server==3.0.0b5
mock
pytest

# Docstring Parser
docstring_parser

# Other
more-itertools
Loading