Skip to content

style(linting): align code with Black/isort/flake8 rules #1103

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
112 changes: 57 additions & 55 deletions .github/helper/py_presubmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,78 +22,80 @@


def do_checks(changed_files):
"""Runs all presubmit checks. Returns False if any fails."""
checks = [
check_license,
]
return all([check(changed_files) for check in checks])
"""Runs all presubmit checks. Returns False if any fails."""
checks = [
check_license,
]
return all([check(changed_files) for check in checks])


_CHECK_LICENSE_FILENAMES = ['Dockerfile']
_CHECK_LICENSE_FILENAMES = ["Dockerfile"]
_CHECK_LICENSE_EXTENSIONS = [
'.bash',
'.Dockerfile',
'.go',
'.h',
'.htm',
'.html',
'.proto',
'.py',
'.rs',
'.sh',
'.ts',
".bash",
".Dockerfile",
".go",
".h",
".htm",
".html",
".proto",
".py",
".rs",
".sh",
".ts",
]
THIRD_PARTY_DIR_NAME = 'third_party'
THIRD_PARTY_DIR_NAME = "third_party"

_LICENSE_STRING = 'http://www.apache.org/licenses/LICENSE-2.0'
_LICENSE_STRING = "http://www.apache.org/licenses/LICENSE-2.0"


def check_license(paths):
"""Validates license header."""
if not paths:
return True

success = True
for path in paths:
path_parts = str(path).split(os.sep)
if any(path_part == THIRD_PARTY_DIR_NAME for path_part in path_parts):
continue
filename = os.path.basename(path)
extension = os.path.splitext(path)[1]
if (filename not in _CHECK_LICENSE_FILENAMES and
extension not in _CHECK_LICENSE_EXTENSIONS):
continue

with open(path) as file_handle:
if _LICENSE_STRING not in file_handle.read():
print('Missing license header in file %s.' % str(path))
success = False

return success
"""Validates license header."""
if not paths:
return True

success = True
for path in paths:
path_parts = str(path).split(os.sep)
if any(path_part == THIRD_PARTY_DIR_NAME for path_part in path_parts):
continue
filename = os.path.basename(path)
extension = os.path.splitext(path)[1]
if (
filename not in _CHECK_LICENSE_FILENAMES
and extension not in _CHECK_LICENSE_EXTENSIONS
):
continue

with open(path) as file_handle:
if _LICENSE_STRING not in file_handle.read():
print("Missing license header in file %s." % str(path))
success = False

return success


def bool_to_returncode(success):
"""Returns 0 if |success|. Otherwise returns 1."""
if success:
print('Success.')
return 0
"""Returns 0 if |success|. Otherwise returns 1."""
if success:
print("Success.")
return 0

print('Failed.')
return 1
print("Failed.")
return 1


def get_all_files():
"""Returns a list of absolute paths of files in this repo."""
get_all_files_command = ['git', 'ls-files']
output = subprocess.check_output(get_all_files_command).decode().splitlines()
return [os.path.abspath(path) for path in output if os.path.isfile(path)]
"""Returns a list of absolute paths of files in this repo."""
get_all_files_command = ["git", "ls-files"]
output = subprocess.check_output(get_all_files_command).decode().splitlines()
return [os.path.abspath(path) for path in output if os.path.isfile(path)]


def main():
relevant_files = get_all_files()
success = do_checks(relevant_files)
return bool_to_returncode(success)
relevant_files = get_all_files()
success = do_checks(relevant_files)
return bool_to_returncode(success)


if __name__ == '__main__':
sys.exit(main())
if __name__ == "__main__":
sys.exit(main())
44 changes: 44 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
repos:
- repo: https://github.com/psf/black.git
rev: "24.10.0"
hooks:
- id: black

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.9.0
hooks:
- id: nbqa-black
name: nbqa-black
description: Run 'black' on a Jupyter Notebook
entry: nbqa black
language: python
require_serial: true
types_or: [jupyter, markdown]
additional_dependencies: [black]

- repo: https://github.com/pycqa/isort
rev: "5.13.2"
hooks:
- id: isort
entry: isort
args:
- --profile=black
- --float-to-top

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.9.0
hooks:
- id: nbqa-flake8
args:
- --ignore=E501,E712,W291,F632,E203,F821,F403,W391,F401
- --exclude=.*,__init__.py
name: nbqa-flake8
description: Run 'flake8' on a Jupyter Notebook
entry: nbqa flake8
language: python
require_serial: true
types_or: [jupyter, markdown]
additional_dependencies:
- flake8-variables-names
- pep8-naming
- flake8-functions-names
2 changes: 1 addition & 1 deletion agent/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@


class Analyzer(BaseAgent):
pass
pass
Loading