-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
ENH: Lint checks for PR diffs #18423
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
9 commits
Select commit
Hold shift + click to select a range
0e1fea2
ENH: Added Linter script
ganesh-k13 37ce99a
ENH: Added uncommitted changes support
ganesh-k13 f43ba6d
BLD: Added linting check
ganesh-k13 8e51b5b
MAINT: Added exception handling and docs
ganesh-k13 135963d
MAINT: Changed branch argument to take target branch
ganesh-k13 4e11afb
ENH: Added github actions
ganesh-k13 6464b4b
MAINT: Use linter_requirements
ganesh-k13 11cfa1c
ENH, MAINT: Added runtest options | Added unified diff
ganesh-k13 eb9bc0e
MAINT: Replaced master with main
ganesh-k13 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pycodestyle==2.5.0 | ||
GitPython==3.1.13 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[pycodestyle] | ||
max_line_length = 79 | ||
statistics = True | ||
ignore = E121,E122,E123,E125,E126,E127,E128,E226,E251,E265,E266,E302,E402,E712,E721,E731,E741,W291,W293,W391,W503,W504 | ||
exclude = numpy/__config__.py |
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,71 @@ | ||
import os | ||
import sys | ||
import subprocess | ||
from argparse import ArgumentParser | ||
from git import Repo, exc | ||
|
||
CONFIG = os.path.join( | ||
os.path.abspath(os.path.dirname(__file__)), | ||
'lint_diff.ini', | ||
) | ||
|
||
|
||
class DiffLinter: | ||
def __init__(self, branch): | ||
self.branch = branch | ||
self.repo = Repo('.') | ||
self.head = self.repo.head.commit | ||
|
||
def get_branch_diff(self, uncommitted = False): | ||
""" | ||
Determine the first common ancestor commit. | ||
Find diff between branch and FCA commit. | ||
Note: if `uncommitted` is set, check only | ||
uncommitted changes | ||
""" | ||
try: | ||
commit = self.repo.merge_base(self.branch, self.head)[0] | ||
except exc.GitCommandError: | ||
print(f"Branch with name `{self.branch}` does not exist") | ||
sys.exit(1) | ||
|
||
if uncommitted: | ||
diff = self.repo.git.diff(self.head, '--unified=0', '***.py') | ||
else: | ||
diff = self.repo.git.diff(commit, self.head, | ||
'--unified=0', '***.py') | ||
return diff | ||
|
||
def run_pycodestyle(self, diff): | ||
""" | ||
Original Author: Josh Wilson (@person142) | ||
Source: | ||
https://github.com/scipy/scipy/blob/master/tools/lint_diff.py | ||
Run pycodestyle on the given diff. | ||
""" | ||
res = subprocess.run( | ||
['pycodestyle', '--diff', '--config', CONFIG], | ||
input=diff, | ||
stdout=subprocess.PIPE, | ||
encoding='utf-8', | ||
) | ||
return res.returncode, res.stdout | ||
|
||
def run_lint(self, uncommitted): | ||
diff = self.get_branch_diff(uncommitted) | ||
retcode, errors = self.run_pycodestyle(diff) | ||
|
||
errors and print(errors) | ||
|
||
sys.exit(retcode) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = ArgumentParser() | ||
parser.add_argument("--branch", type=str, default='main', | ||
help="The branch to diff against") | ||
parser.add_argument("--uncommitted", action='store_true', | ||
help="Check only uncommitted changes") | ||
args = parser.parse_args() | ||
|
||
DiffLinter(args.branch).run_lint(args.uncommitted) |
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.
Uh oh!
There was an error while loading. Please reload this page.