Skip to content

Commit

Permalink
Fix mypy and black commands in pre-commit githook (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulfatir authored Sep 5, 2022
1 parent 2c946c6 commit cf57f73
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions .devtools/githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ import textwrap
from pathlib import Path
from subprocess import CalledProcessError, check_output

ROOT = Path(__file__).parents[2]
SRC = ROOT / "src"


def type_check() -> int:
mypy_opts = ['--follow-imports=silent', '--ignore-missing-imports']
mypy_args = [
str(p.parent.resolve())
for p in Path(__file__).parents[2].glob('**/.typesafe')
mypy_opts = [
"--allow-redefinition",
"--follow-imports=silent",
"--ignore-missing-imports",
]

mypy_args = [str(p.parent.resolve()) for p in SRC.glob("**/.typesafe")]

try:
check_output(['mypy'] + mypy_opts + mypy_args)
check_output(["mypy"] + mypy_opts + mypy_args)
return 0
except CalledProcessError as e:
print(e.output.decode().strip())

print(
textwrap.dedent(
f'''
f"""
Mypy command
mypy {" ".join(mypy_opts + mypy_args)}
Expand All @@ -34,7 +39,7 @@ def type_check() -> int:
python setup.py type_check
in order to validate your fixes.
'''
"""
).lstrip(),
file=sys.stderr,
)
Expand All @@ -43,30 +48,20 @@ def type_check() -> int:


def style_check() -> int:
git_root = (
check_output(['git', 'rev-parse', '--show-toplevel']).decode().strip()
)
git_files = (
check_output(['git', 'diff', '--name-only', '--cached'])
.decode()
.strip()
.split()
)

black_opts = []
black_args = [
os.path.join(git_root, git_file)
for git_file in git_files
if git_file.endswith('.py') and os.path.isfile(git_file)
str(ROOT / folder)
for folder in ["src", "test", "examples"]
if (ROOT / folder).is_dir()
]

try:
check_output(['black'] + ['--check'] + black_opts + black_args)
check_output(["black"] + ["--check"] + black_opts + black_args)
return 0
except CalledProcessError as e:
print(
textwrap.dedent(
f'''
f"""
Black command
black {" ".join(['--check'] + black_opts + black_args)}
Expand All @@ -80,7 +75,7 @@ def style_check() -> int:
python setup.py style_check
in order to validate your fixes.
'''
"""
).lstrip(),
file=sys.stderr,
)
Expand All @@ -90,10 +85,10 @@ def style_check() -> int:

def license_check() -> int:
git_root = (
check_output(['git', 'rev-parse', '--show-toplevel']).decode().strip()
check_output(["git", "rev-parse", "--show-toplevel"]).decode().strip()
)
git_files = (
check_output(['git', 'diff', '--name-only', '--cached'])
check_output(["git", "diff", "--name-only", "--cached"])
.decode()
.strip()
.split()
Expand All @@ -102,16 +97,16 @@ def license_check() -> int:
py_files = [
os.path.join(git_root, git_file)
for git_file in git_files
if git_file.endswith('.py') and os.path.isfile(git_file)
if git_file.endswith(".py") and os.path.isfile(git_file)
]

try:
check_output(['.devtools/license'] + ['check'] + py_files)
check_output([".devtools/license"] + ["check"] + py_files)
return 0
except CalledProcessError as e:
print(
textwrap.dedent(
f'''
f"""
License check command
.devtools/license {" ".join(['check'] + py_files)}
Expand All @@ -125,12 +120,13 @@ def license_check() -> int:
.devtools/license check src test
in order to validate your fixes.
'''
"""
).lstrip(),
file=sys.stderr,
)

return e.returncode

if __name__ == '__main__':

if __name__ == "__main__":
sys.exit(type_check() | style_check())

0 comments on commit cf57f73

Please sign in to comment.