Skip to content

Commit beede24

Browse files
Merge branch 'release/3.9.5'
2 parents a550957 + 0a6dbb5 commit beede24

File tree

10 files changed

+421
-134
lines changed

10 files changed

+421
-134
lines changed

.undertake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name=Add isort settings
2+
color=#3C79AB
3+
font_color=white
4+
format=instantly

ACKNOWLEDGEMENTS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ Code Contributors
3838
- Elliott Sales de Andrade (@qulogic)
3939
- Kasper Jacobsen (@dinoshauer)
4040
- Sebastian Pipping (@hartwork)
41+
- Helen Sherwood-Taylor (@helenst)
42+
- Mocker (@Zuckonit)
43+
- Tim Graham (@timgraham)
44+
- Adam (@NorthIsUp)
45+
46+
Documenters
47+
===================
48+
- Reinout van Rees (@reinout)
49+
- Helen Sherwood-Taylor (@helenst)
50+
- Elliott Sales de Andrade (@QuLogic)
51+
- Brian Peiris (@brianpeiris)
52+
- Tim Graham (@timgraham)
4153

4254
--------------------------------------------
4355

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,17 @@ own line
255255
Note: to change the how constant indents appear - simply change the indent property with the following accepted formats:
256256
* Number of spaces you would like. For example: 4 would cause standard 4 space indentation.
257257
* Tab
258-
* A verbatim string with quotes around it. For example: " " is equivalent to 4
258+
* A verbatim string with quotes around it.
259+
260+
For example:
261+
262+
" "
263+
264+
is equivalent to 4
265+
266+
For the import styles that use parentheses, you can control whether or not to
267+
include a trailing comma after the last import with the include_trailing_comma
268+
option (defaults to false).
259269

260270
Intelligently Balanced Multi-line Imports
261271
======================
@@ -391,6 +401,25 @@ https://gist.github.com/acdha/8717683
391401

392402
Which can help to ensure a certain level of code quality throughout a project.
393403

404+
405+
Git hook
406+
========
407+
408+
isort provides a hook function that can be integrated into your Git pre-commit script to check
409+
Python code before committing.
410+
411+
To cause the commit to fail if there are isort errors (strict mode), include the following in
412+
`.git/hooks/pre-commit`:
413+
414+
from isort.hooks import git_hook
415+
416+
if __name__ == '__main__':
417+
sys.exit(git_hook(strict=True))
418+
419+
If you just want to display warnings, but allow the commit to happen anyway, call git_hook without
420+
the `strict` parameter.
421+
422+
394423
Why isort?
395424
======================
396425

isort/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
from . import settings
2626
from .isort import SECTION_NAMES, SECTIONS, SortImports
2727

28-
__version__ = "3.9.4"
28+
__version__ = "3.9.5"

isort/hooks.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""isort.py.
2+
3+
Defines a git hook to allow pre-commit warnings and errors about import order.
4+
5+
usage:
6+
exit_code = git_hook(strict=True)
7+
8+
Copyright (C) 2015 Helen Sherwood-Taylor
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
11+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
12+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
13+
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all copies or
16+
substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
"""
25+
import subprocess
26+
27+
from isort import SortImports
28+
29+
30+
def get_output(command):
31+
"""
32+
Run a command and return raw output
33+
34+
:param str command: the command to run
35+
:returns: the stdout output of the command
36+
"""
37+
return subprocess.check_output(command.split())
38+
39+
40+
def get_lines(command):
41+
"""
42+
Run a command and return lines of output
43+
44+
:param str command: the command to run
45+
:returns: list of whitespace-stripped lines output by command
46+
"""
47+
stdout = get_output(command)
48+
return [line.strip().decode('utf-8') for line in stdout.splitlines()]
49+
50+
51+
def git_hook(strict=False):
52+
"""
53+
Git pre-commit hook to check staged files for isort errors
54+
55+
:param bool strict - if True, return number of errors on exit,
56+
causing the hook to fail. If False, return zero so it will
57+
just act as a warning.
58+
59+
:return number of errors if in strict mode, 0 otherwise.
60+
"""
61+
62+
# Get list of files modified and staged
63+
diff_cmd = "git diff-index --cached --name-only --diff-filter=ACMRTUXB HEAD"
64+
files_modified = get_lines(diff_cmd)
65+
66+
errors = 0
67+
for filename in files_modified:
68+
if filename.endswith('.py'):
69+
# Get the staged contents of the file
70+
staged_cmd = "git show :%s" % filename
71+
staged_contents = get_output(staged_cmd)
72+
73+
sort = SortImports(
74+
file_path=filename,
75+
file_contents=staged_contents.decode(),
76+
check=True
77+
)
78+
79+
if sort.incorrectly_sorted:
80+
errors += 1
81+
82+
return errors if strict else 0

0 commit comments

Comments
 (0)