-
Notifications
You must be signed in to change notification settings - Fork 0
Add linting check #53
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
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1cb7f4d
Dev (#48)
47thomasj 6020950
Stg (#49)
47thomasj 182a061
Merge branch 'dev' into prd
47thomasj 40bb0d8
Initial setup and linter fixes
Emaniacinator 1929fa7
Fixed some odd setting of variables
Emaniacinator 43c8763
Fixed the documentation
Emaniacinator 9e85c34
Running a quick experiment on the empty brackets
Emaniacinator 9a7236e
Merge branch 'prd' into fix-function-ignoring
Emaniacinator 601a13f
Minor type security improvment, standard check fix
Emaniacinator cd5dd34
Fix function ignoring (#51)
47thomasj d569ab2
Added initial go at writing the linting check
Emaniacinator 6909fdb
Minor spelling update so it doesn't bother me
Emaniacinator 3510de1
Finished fixing linting check errors
Emaniacinator cc0ce73
Readme updates from temp branch
Emaniacinator 4bbb15f
Quick fix for mypy call
Emaniacinator 28fb85f
standard check fixes, debugging
Emaniacinator c2f0e92
Docstring for new function
Emaniacinator ec3fa95
Requested changes
Emaniacinator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| **/__pycache__ | ||
| **/__pycache__ | ||
| venv | ||
| .venv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| from argparse import ArgumentParser | ||
| from pathlib import Path | ||
| from sys import stderr, exit | ||
| from typing import Optional | ||
| from pathspec import PathSpec | ||
| from config import load_mypy_arguments, load_ignore_patterns | ||
| from utils.file_utils import should_ignore_file | ||
| from mypy.api import run as mypy_api_run | ||
|
|
||
|
|
||
| def main() -> int: | ||
| """Main entry point for the style checker. | ||
|
|
||
| Returns: | ||
| Exit code (0 for success, 1 for errors found) | ||
| """ | ||
| print("Starting the Linting Check") | ||
|
|
||
| parser = ArgumentParser(description='Modern Python style checker') | ||
| parser.add_argument('paths', nargs='*', default=['.'], help='Paths to check (default: current directory)') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| ignore_patterns = load_ignore_patterns() | ||
| mypy_args = load_mypy_arguments() | ||
|
|
||
| all_errors: list = [] | ||
|
|
||
| for path_str in args.paths: | ||
| path = Path(path_str) | ||
| if path.is_file(): | ||
| if should_ignore_file(path, ignore_patterns): | ||
| continue | ||
| else: | ||
| mypy_return = run_mypy_on_file(path_str, mypy_args) | ||
| all_errors.extend(mypy_return) | ||
| elif path.is_dir(): | ||
| mypy_return = run_mypy_on_directory(path, ignore_patterns, mypy_args) | ||
| all_errors.extend(mypy_return) | ||
| else: | ||
| print(f"Warning: Path not found: {path}", file=stderr) | ||
|
|
||
| if all_errors: | ||
| for mypy_error in all_errors: | ||
| print(mypy_error) | ||
| print(f"\nFound {len(all_errors)} MyPy errors.") | ||
| return 1 | ||
|
|
||
| else: | ||
| print("Passed all MyPy checks!") | ||
| return 0 | ||
|
|
||
|
|
||
| def run_mypy_on_directory(directory: Path, ignore_patterns: Optional[PathSpec], args: list[str] | None = None) -> list: | ||
| """Check all Python files in a directory recursively. | ||
|
|
||
| Args: | ||
| directory: Directory to check | ||
| ignore_patterns: Patterns for files to ignore | ||
| args: A list of all of the arguments to be passed into MyPy | ||
|
|
||
| Returns: | ||
| list of style errors found | ||
| """ | ||
| errors_in_directory: list = [] | ||
|
|
||
| for file_path in directory.rglob('*.py'): | ||
| if should_ignore_file(file_path, ignore_patterns): | ||
| continue | ||
|
|
||
| else: | ||
| mypy_errors = run_mypy_on_file(str(file_path), args) | ||
| errors_in_directory.extend(mypy_errors) | ||
|
|
||
| return errors_in_directory | ||
|
|
||
|
|
||
| def run_mypy_on_file(file_path_string: str, args: list[str] | None = None) -> list: | ||
| """Run mypy on a single python file. | ||
|
|
||
| Args: | ||
| file_path_string: The string representation of the path to the | ||
| file that should be checked | ||
| args: The arguments that should be passed into the mypy call | ||
|
|
||
| Returns: | ||
| The result of calling mypy on the file | ||
| """ | ||
| args_with_file_path_at_start = [] | ||
|
|
||
| if not args: | ||
| args_with_file_path_at_start = [f"{file_path_string}", "--strict"] | ||
| else: | ||
| args_with_file_path_at_start.insert(0, f"{file_path_string}") | ||
|
|
||
| mypy_output_to_standard, mypy_output_to_error, mypy_return_value = mypy_api_run(args_with_file_path_at_start) | ||
|
|
||
| # Note that despite what mypy says, it actually writes the various linting errors to the *standard* | ||
| # output, not the error output. It writes fatal errors caused by odds and ends to it's error output, | ||
| # so it's necessary to check both the standard the the error output to actually find all of the | ||
| # desired errors | ||
|
|
||
| # If mypy returns with no errors, we can return an empty list of errors | ||
| if mypy_return_value == 0: | ||
| return [] | ||
|
|
||
| # Otherwise we need to filter everything mypy prints so that we only have the return errors | ||
| errors_to_return = [] | ||
| for output_source in (mypy_output_to_standard, mypy_output_to_error): | ||
| for line in output_source.splitlines(): | ||
| if "error:" in line: | ||
| errors_to_return.append(line) | ||
|
|
||
| return errors_to_return | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| exit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove the "found a x..." print statements in this file? They seem a little excessive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this! Just made those updates