mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 19:46:54 +00:00
Add missing init hook
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-init-py-trap
This commit is contained in:
parent
31853d6c43
commit
513a24cde9
5 changed files with 103 additions and 0 deletions
25
pre_commit_hooks/check_missing_inits.py
Normal file
25
pre_commit_hooks/check_missing_inits.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import os
|
||||
from argparse import ArgumentParser
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
directories = {os.path.dirname(f) for f in args.filenames}
|
||||
missing_dirs = set()
|
||||
for d in directories:
|
||||
if not os.path.exists(os.path.join(d, '__init__.py')):
|
||||
missing_dirs.add(d)
|
||||
|
||||
for d in sorted(missing_dirs):
|
||||
print('No __init__.py file found in: {}'.format(d))
|
||||
|
||||
return 1 if len(missing_dirs) else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue