Skip to content

Commit a18c5af

Browse files
authored
Merge pull request #447 from pre-commit/pyupgrade
pre-commit-hooks: python3.6+
2 parents 551d1a0 + f5c42a0 commit a18c5af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+291
-493
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ repos:
2828
rev: v1.9.0
2929
hooks:
3030
- id: reorder-python-imports
31-
language_version: python3
31+
args: [--py3-plus]
3232
- repo: https://github.com/asottile/pyupgrade
3333
rev: v1.26.2
3434
hooks:
3535
- id: pyupgrade
36+
args: [--py36-plus]
3637
- repo: https://github.com/asottile/add-trailing-comma
3738
rev: v1.5.0
3839
hooks:
3940
- id: add-trailing-comma
41+
args: [--py36-plus]
42+
- repo: https://github.com/asottile/setup-cfg-fmt
43+
rev: v1.6.0
44+
hooks:
45+
- id: setup-cfg-fmt
4046
- repo: https://github.com/pre-commit/mirrors-mypy
4147
rev: v0.761
4248
hooks:
4349
- id: mypy
44-
language_version: python3

azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ jobs:
1616
- template: job--pre-commit.yml@asottile
1717
- template: job--python-tox.yml@asottile
1818
parameters:
19-
toxenvs: [py27, py37]
19+
toxenvs: [py38]
2020
os: windows
2121
- template: job--python-tox.yml@asottile
2222
parameters:
23-
toxenvs: [pypy, pypy3, py27, py36, py37]
23+
toxenvs: [pypy3, py36, py37, py38]
2424
os: linux

pre_commit_hooks/autopep8_wrapper.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
from __future__ import absolute_import
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
5-
6-
def main(): # type: () -> int
1+
def main() -> int:
72
raise SystemExit(
83
'autopep8-wrapper is deprecated. Instead use autopep8 directly via '
94
'https://github.com/pre-commit/mirrors-autopep8',

pre_commit_hooks/check_added_large_files.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
4-
from __future__ import unicode_literals
5-
61
import argparse
72
import json
83
import math
94
import os
10-
from typing import Iterable
115
from typing import Optional
126
from typing import Sequence
137
from typing import Set
@@ -17,7 +11,7 @@
1711
from pre_commit_hooks.util import cmd_output
1812

1913

20-
def lfs_files(): # type: () -> Set[str]
14+
def lfs_files() -> Set[str]:
2115
try:
2216
# Introduced in git-lfs 2.2.0, first working in 2.2.1
2317
lfs_ret = cmd_output('git', 'lfs', 'status', '--json')
@@ -27,23 +21,20 @@ def lfs_files(): # type: () -> Set[str]
2721
return set(json.loads(lfs_ret)['files'])
2822

2923

30-
def find_large_added_files(filenames, maxkb):
31-
# type: (Iterable[str], int) -> int
24+
def find_large_added_files(filenames: Sequence[str], maxkb: int) -> int:
3225
# Find all added files that are also in the list of files pre-commit tells
3326
# us about
34-
filenames = (added_files() & set(filenames)) - lfs_files()
35-
3627
retv = 0
37-
for filename in filenames:
28+
for filename in (added_files() & set(filenames)) - lfs_files():
3829
kb = int(math.ceil(os.stat(filename).st_size / 1024))
3930
if kb > maxkb:
40-
print('{} ({} KB) exceeds {} KB.'.format(filename, kb, maxkb))
31+
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
4132
retv = 1
4233

4334
return retv
4435

4536

46-
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
37+
def main(argv: Optional[Sequence[str]] = None) -> int:
4738
parser = argparse.ArgumentParser()
4839
parser.add_argument(
4940
'filenames', nargs='*',

pre_commit_hooks/check_ast.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from __future__ import absolute_import
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
51
import argparse
62
import ast
73
import platform
@@ -11,7 +7,7 @@
117
from typing import Sequence
128

139

14-
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
10+
def main(argv: Optional[Sequence[str]] = None) -> int:
1511
parser = argparse.ArgumentParser()
1612
parser.add_argument('filenames', nargs='*')
1713
args = parser.parse_args(argv)
@@ -23,15 +19,11 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
2319
with open(filename, 'rb') as f:
2420
ast.parse(f.read(), filename=filename)
2521
except SyntaxError:
26-
print(
27-
'{}: failed parsing with {} {}:'.format(
28-
filename,
29-
platform.python_implementation(),
30-
sys.version.partition(' ')[0],
31-
),
32-
)
22+
impl = platform.python_implementation()
23+
version = sys.version.split()[0]
24+
print(f'{filename}: failed parsing with {impl} {version}:')
3325
tb = ' ' + traceback.format_exc().replace('\n', '\n ')
34-
print('\n{}'.format(tb))
26+
print(f'\n{tb}')
3527
retval = 1
3628
return retval
3729

pre_commit_hooks/check_builtin_literals.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from __future__ import unicode_literals
2-
31
import argparse
42
import ast
5-
import collections
6-
import sys
73
from typing import List
4+
from typing import NamedTuple
85
from typing import Optional
96
from typing import Sequence
107
from typing import Set
@@ -21,23 +18,26 @@
2118
}
2219

2320

24-
Call = collections.namedtuple('Call', ['name', 'line', 'column'])
21+
class Call(NamedTuple):
22+
name: str
23+
line: int
24+
column: int
2525

2626

2727
class Visitor(ast.NodeVisitor):
28-
def __init__(self, ignore=None, allow_dict_kwargs=True):
29-
# type: (Optional[Sequence[str]], bool) -> None
30-
self.builtin_type_calls = [] # type: List[Call]
28+
def __init__(
29+
self,
30+
ignore: Optional[Sequence[str]] = None,
31+
allow_dict_kwargs: bool = True,
32+
) -> None:
33+
self.builtin_type_calls: List[Call] = []
3134
self.ignore = set(ignore) if ignore else set()
3235
self.allow_dict_kwargs = allow_dict_kwargs
3336

34-
def _check_dict_call(self, node): # type: (ast.Call) -> bool
35-
return (
36-
self.allow_dict_kwargs and
37-
(getattr(node, 'kwargs', None) or getattr(node, 'keywords', None))
38-
)
37+
def _check_dict_call(self, node: ast.Call) -> bool:
38+
return self.allow_dict_kwargs and bool(node.keywords)
3939

40-
def visit_Call(self, node): # type: (ast.Call) -> None
40+
def visit_Call(self, node: ast.Call) -> None:
4141
if not isinstance(node.func, ast.Name):
4242
# Ignore functions that are object attributes (`foo.bar()`).
4343
# Assume that if the user calls `builtins.list()`, they know what
@@ -54,20 +54,23 @@ def visit_Call(self, node): # type: (ast.Call) -> None
5454
)
5555

5656

57-
def check_file(filename, ignore=None, allow_dict_kwargs=True):
58-
# type: (str, Optional[Sequence[str]], bool) -> List[Call]
57+
def check_file(
58+
filename: str,
59+
ignore: Optional[Sequence[str]] = None,
60+
allow_dict_kwargs: bool = True,
61+
) -> List[Call]:
5962
with open(filename, 'rb') as f:
6063
tree = ast.parse(f.read(), filename=filename)
6164
visitor = Visitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
6265
visitor.visit(tree)
6366
return visitor.builtin_type_calls
6467

6568

66-
def parse_ignore(value): # type: (str) -> Set[str]
69+
def parse_ignore(value: str) -> Set[str]:
6770
return set(value.split(','))
6871

6972

70-
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
73+
def main(argv: Optional[Sequence[str]] = None) -> int:
7174
parser = argparse.ArgumentParser()
7275
parser.add_argument('filenames', nargs='*')
7376
parser.add_argument('--ignore', type=parse_ignore, default=set())
@@ -93,15 +96,11 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
9396
rc = rc or 1
9497
for call in calls:
9598
print(
96-
'{filename}:{call.line}:{call.column}: '
97-
'replace {call.name}() with {replacement}'.format(
98-
filename=filename,
99-
call=call,
100-
replacement=BUILTIN_TYPES[call.name],
101-
),
99+
f'{filename}:{call.line}:{call.column}: '
100+
f'replace {call.name}() with {BUILTIN_TYPES[call.name]}',
102101
)
103102
return rc
104103

105104

106105
if __name__ == '__main__':
107-
sys.exit(main())
106+
exit(main())

pre_commit_hooks/check_byte_order_marker.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
from __future__ import absolute_import
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
51
import argparse
62
from typing import Optional
73
from typing import Sequence
84

95

10-
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
6+
def main(argv: Optional[Sequence[str]] = None) -> int:
117
parser = argparse.ArgumentParser()
128
parser.add_argument('filenames', nargs='*', help='Filenames to check')
139
args = parser.parse_args(argv)
@@ -18,7 +14,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
1814
with open(filename, 'rb') as f:
1915
if f.read(3) == b'\xef\xbb\xbf':
2016
retv = 1
21-
print('{}: Has a byte-order marker'.format(filename))
17+
print(f'{filename}: Has a byte-order marker')
2218

2319
return retv
2420

pre_commit_hooks/check_case_conflict.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from __future__ import absolute_import
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
51
import argparse
62
from typing import Iterable
73
from typing import Optional
@@ -12,11 +8,11 @@
128
from pre_commit_hooks.util import cmd_output
139

1410

15-
def lower_set(iterable): # type: (Iterable[str]) -> Set[str]
11+
def lower_set(iterable: Iterable[str]) -> Set[str]:
1612
return {x.lower() for x in iterable}
1713

1814

19-
def find_conflicting_filenames(filenames): # type: (Sequence[str]) -> int
15+
def find_conflicting_filenames(filenames: Sequence[str]) -> int:
2016
repo_files = set(cmd_output('git', 'ls-files').splitlines())
2117
relevant_files = set(filenames) | added_files()
2218
repo_files -= relevant_files
@@ -39,13 +35,13 @@ def find_conflicting_filenames(filenames): # type: (Sequence[str]) -> int
3935
if x.lower() in conflicts
4036
]
4137
for filename in sorted(conflicting_files):
42-
print('Case-insensitivity conflict found: {}'.format(filename))
38+
print(f'Case-insensitivity conflict found: {filename}')
4339
retv = 1
4440

4541
return retv
4642

4743

48-
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
44+
def main(argv: Optional[Sequence[str]] = None) -> int:
4945
parser = argparse.ArgumentParser()
5046
parser.add_argument(
5147
'filenames', nargs='*',

pre_commit_hooks/check_docstring_first.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
from __future__ import absolute_import
2-
from __future__ import print_function
3-
from __future__ import unicode_literals
4-
51
import argparse
62
import io
73
import tokenize
4+
from tokenize import tokenize as tokenize_tokenize
85
from typing import Optional
96
from typing import Sequence
107

11-
import six
12-
13-
if six.PY2: # pragma: no cover (PY2)
14-
from tokenize import generate_tokens as tokenize_tokenize
15-
OTHER_NON_CODE = ()
16-
else: # pragma: no cover (PY3)
17-
from tokenize import tokenize as tokenize_tokenize
18-
OTHER_NON_CODE = (tokenize.ENCODING,)
19-
20-
NON_CODE_TOKENS = frozenset(
21-
(tokenize.COMMENT, tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL) +
22-
OTHER_NON_CODE,
23-
)
8+
NON_CODE_TOKENS = frozenset((
9+
tokenize.COMMENT, tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL,
10+
tokenize.ENCODING,
11+
))
2412

2513

26-
def check_docstring_first(src, filename='<unknown>'):
27-
# type: (bytes, str) -> int
14+
def check_docstring_first(src: bytes, filename: str = '<unknown>') -> int:
2815
"""Returns nonzero if the source has what looks like a docstring that is
2916
not at the beginning of the source.
3017
@@ -40,18 +27,14 @@ def check_docstring_first(src, filename='<unknown>'):
4027
if tok_type == tokenize.STRING and scol == 0:
4128
if found_docstring_line is not None:
4229
print(
43-
'{}:{} Multiple module docstrings '
44-
'(first docstring on line {}).'.format(
45-
filename, sline, found_docstring_line,
46-
),
30+
f'{filename}:{sline} Multiple module docstrings '
31+
f'(first docstring on line {found_docstring_line}).',
4732
)
4833
return 1
4934
elif found_code_line is not None:
5035
print(
51-
'{}:{} Module docstring appears after code '
52-
'(code seen on line {}).'.format(
53-
filename, sline, found_code_line,
54-
),
36+
f'{filename}:{sline} Module docstring appears after code '
37+
f'(code seen on line {found_code_line}).',
5538
)
5639
return 1
5740
else:
@@ -62,7 +45,7 @@ def check_docstring_first(src, filename='<unknown>'):
6245
return 0
6346

6447

65-
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
48+
def main(argv: Optional[Sequence[str]] = None) -> int:
6649
parser = argparse.ArgumentParser()
6750
parser.add_argument('filenames', nargs='*')
6851
args = parser.parse_args(argv)

0 commit comments

Comments
 (0)