mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-14 09:27:21 +08:00
pre-commit-hooks: python3.6+
This commit is contained in:
@ -1,30 +1,17 @@
|
||||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import io
|
||||
import tokenize
|
||||
from tokenize import tokenize as tokenize_tokenize
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
|
||||
import six
|
||||
|
||||
if six.PY2: # pragma: no cover (PY2)
|
||||
from tokenize import generate_tokens as tokenize_tokenize
|
||||
OTHER_NON_CODE = ()
|
||||
else: # pragma: no cover (PY3)
|
||||
from tokenize import tokenize as tokenize_tokenize
|
||||
OTHER_NON_CODE = (tokenize.ENCODING,)
|
||||
|
||||
NON_CODE_TOKENS = frozenset(
|
||||
(tokenize.COMMENT, tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL) +
|
||||
OTHER_NON_CODE,
|
||||
)
|
||||
NON_CODE_TOKENS = frozenset((
|
||||
tokenize.COMMENT, tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL,
|
||||
tokenize.ENCODING,
|
||||
))
|
||||
|
||||
|
||||
def check_docstring_first(src, filename='<unknown>'):
|
||||
# type: (bytes, str) -> int
|
||||
def check_docstring_first(src: bytes, filename: str = '<unknown>') -> int:
|
||||
"""Returns nonzero if the source has what looks like a docstring that is
|
||||
not at the beginning of the source.
|
||||
|
||||
@ -40,18 +27,14 @@ def check_docstring_first(src, filename='<unknown>'):
|
||||
if tok_type == tokenize.STRING and scol == 0:
|
||||
if found_docstring_line is not None:
|
||||
print(
|
||||
'{}:{} Multiple module docstrings '
|
||||
'(first docstring on line {}).'.format(
|
||||
filename, sline, found_docstring_line,
|
||||
),
|
||||
f'{filename}:{sline} Multiple module docstrings '
|
||||
f'(first docstring on line {found_docstring_line}).',
|
||||
)
|
||||
return 1
|
||||
elif found_code_line is not None:
|
||||
print(
|
||||
'{}:{} Module docstring appears after code '
|
||||
'(code seen on line {}).'.format(
|
||||
filename, sline, found_code_line,
|
||||
),
|
||||
f'{filename}:{sline} Module docstring appears after code '
|
||||
f'(code seen on line {found_code_line}).',
|
||||
)
|
||||
return 1
|
||||
else:
|
||||
@ -62,7 +45,7 @@ def check_docstring_first(src, filename='<unknown>'):
|
||||
return 0
|
||||
|
||||
|
||||
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
|
||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
args = parser.parse_args(argv)
|
||||
|
Reference in New Issue
Block a user