Repository: pre-commit/pre-commit-hooks
Pull request #748 (2022-04-06 20:56:07)

adjust error outputs to be more standardized


Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index c7edbe9..6c1f591 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -4,40 +4,44 @@ repos:
     hooks:
     -   id: trailing-whitespace
     -   id: end-of-file-fixer
+    -   id: check-docstring-first
+    -   id: check-json
+    -   id: check-added-large-files
     -   id: check-yaml
     -   id: debug-statements
-    -   id: double-quote-string-fixer
     -   id: name-tests-test
+    -   id: double-quote-string-fixer
     -   id: requirements-txt-fixer
--   repo: https://github.com/asottile/setup-cfg-fmt
-    rev: v1.20.1
+-   repo: https://github.com/PyCQA/flake8
+    rev: 4.0.1
     hooks:
-    -   id: setup-cfg-fmt
+    -   id: flake8
+        additional_dependencies: [flake8-typing-imports==1.12.0]
+-   repo: https://github.com/pre-commit/mirrors-autopep8
+    rev: v1.6.0
+    hooks:
+    -   id: autopep8
 -   repo: https://github.com/asottile/reorder_python_imports
     rev: v3.0.1
     hooks:
     -   id: reorder-python-imports
         args: [--py37-plus, --add-import, 'from __future__ import annotations']
--   repo: https://github.com/asottile/add-trailing-comma
-    rev: v2.2.2
-    hooks:
-    -   id: add-trailing-comma
-        args: [--py36-plus]
 -   repo: https://github.com/asottile/pyupgrade
     rev: v2.31.1
     hooks:
     -   id: pyupgrade
         args: [--py37-plus]
--   repo: https://github.com/pre-commit/mirrors-autopep8
-    rev: v1.6.0
+-   repo: https://github.com/asottile/add-trailing-comma
+    rev: v2.2.1
     hooks:
-    -   id: autopep8
--   repo: https://github.com/PyCQA/flake8
-    rev: 4.0.1
+    -   id: add-trailing-comma
+        args: [--py36-plus]
+-   repo: https://github.com/asottile/setup-cfg-fmt
+    rev: v1.20.0
     hooks:
-    -   id: flake8
+    -   id: setup-cfg-fmt
 -   repo: https://github.com/pre-commit/mirrors-mypy
-    rev: v0.942
+    rev: v0.941
     hooks:
     -   id: mypy
         additional_dependencies: [types-all]
diff --git a/pre_commit_hooks/check_docstring_first.py b/pre_commit_hooks/check_docstring_first.py
index 1744919..d55f08a 100644
--- a/pre_commit_hooks/check_docstring_first.py
+++ b/pre_commit_hooks/check_docstring_first.py
@@ -28,13 +28,13 @@ def check_docstring_first(src: bytes, filename: str = '<unknown>') -> int:
         if tok_type == tokenize.STRING and scol == 0:
             if found_docstring_line is not None:
                 print(
-                    f'{filename}:{sline} Multiple module docstrings '
+                    f'{filename}:{sline}: Multiple module docstrings '
                     f'(first docstring on line {found_docstring_line}).',
                 )
                 return 1
             elif found_code_line is not None:
                 print(
-                    f'{filename}:{sline} Module docstring appears after code '
+                    f'{filename}:{sline}: Module docstring appears after code '
                     f'(code seen on line {found_code_line}).',
                 )
                 return 1
diff --git a/pre_commit_hooks/check_merge_conflict.py b/pre_commit_hooks/check_merge_conflict.py
index 22031a2..15ec284 100644
--- a/pre_commit_hooks/check_merge_conflict.py
+++ b/pre_commit_hooks/check_merge_conflict.py
@@ -10,6 +10,7 @@ from pre_commit_hooks.util import cmd_output
 CONFLICT_PATTERNS = [
     b'<<<<<<< ',
     b'======= ',
+    b'=======\r\n',
     b'=======\n',
     b'>>>>>>> ',
 ]
@@ -39,12 +40,12 @@ def main(argv: Sequence[str] | None = None) -> int:
     retcode = 0
     for filename in args.filenames:
         with open(filename, 'rb') as inputfile:
-            for i, line in enumerate(inputfile):
+            for i, line in enumerate(inputfile, start=1):
                 for pattern in CONFLICT_PATTERNS:
                     if line.startswith(pattern):
                         print(
-                            f'Merge conflict string "{pattern.decode()}" '
-                            f'found in {filename}:{i + 1}',
+                            f'{filename}:{i}: Merge conflict string '
+                            f'{pattern.strip().decode()!r} found',
                         )
                         retcode = 1
 
diff --git a/pre_commit_hooks/debug_statement_hook.py b/pre_commit_hooks/debug_statement_hook.py
index 00b6798..9ada657 100644
--- a/pre_commit_hooks/debug_statement_hook.py
+++ b/pre_commit_hooks/debug_statement_hook.py
@@ -65,7 +65,7 @@ def check_file(filename: str) -> int:
     visitor.visit(ast_obj)
 
     for bp in visitor.breakpoints:
-        print(f'{filename}:{bp.line}:{bp.col} - {bp.name} {bp.reason}')
+        print(f'{filename}:{bp.line}:{bp.col}: {bp.name} {bp.reason}')
 
     return int(bool(visitor.breakpoints))
 


Test patch:
diff --git a/tests/check_docstring_first_test.py b/tests/check_docstring_first_test.py
index 079896f..8bafae8 100644
--- a/tests/check_docstring_first_test.py
+++ b/tests/check_docstring_first_test.py
@@ -17,7 +17,7 @@ TESTS = (
         b'from __future__ import unicode_literals\n'
         b'"foo"\n',
         1,
-        '{filename}:2 Module docstring appears after code '
+        '{filename}:2: Module docstring appears after code '
         '(code seen on line 1).\n',
     ),
     # Test double docstring
@@ -26,7 +26,7 @@ TESTS = (
         b'from __future__ import absolute_import\n'
         b'"fake docstring"\n',
         1,
-        '{filename}:3 Multiple module docstrings '
+        '{filename}:3: Multiple module docstrings '
         '(first docstring on line 1).\n',
     ),
     # Test multiple lines of code above
@@ -35,7 +35,7 @@ TESTS = (
         b'import sys\n'
         b'"docstring"\n',
         1,
-        '{filename}:3 Module docstring appears after code '
+        '{filename}:3: Module docstring appears after code '
         '(code seen on line 1).\n',
     ),
     # String literals in expressions are ok.
diff --git a/tests/check_merge_conflict_test.py b/tests/check_merge_conflict_test.py
index d3322db..76c4283 100644
--- a/tests/check_merge_conflict_test.py
+++ b/tests/check_merge_conflict_test.py
@@ -101,8 +101,14 @@ def repository_pending_merge(tmpdir):
 
 
 @pytest.mark.usefixtures('f1_is_a_conflict_file')
-def test_merge_conflicts_git():
+def test_merge_conflicts_git(capsys):
     assert main(['f1']) == 1
+    out, _ = capsys.readouterr()
+    assert out == (
+        "f1:1: Merge conflict string '<<<<<<<' found\n"
+        "f1:3: Merge conflict string '=======' found\n"
+        "f1:5: Merge conflict string '>>>>>>>' found\n"
+    )
 
 
 @pytest.mark.parametrize(
@@ -139,7 +145,7 @@ def test_care_when_assumed_merge(tmpdir):
     assert main([str(f.realpath()), '--assume-in-merge']) == 1
 
 
-def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir):
+def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir, capsys):
     worktree = tmpdir.join('worktree')
     cmd_output('git', 'worktree', 'add', str(worktree))
     with worktree.as_cwd():
@@ -148,4 +154,4 @@ def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir):
         )
         msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG')
         assert msg.exists()
-        test_merge_conflicts_git()
+        test_merge_conflicts_git(capsys)
diff --git a/tests/debug_statement_hook_test.py b/tests/debug_statement_hook_test.py
index 349fe89..5a8e0bb 100644
--- a/tests/debug_statement_hook_test.py
+++ b/tests/debug_statement_hook_test.py
@@ -55,7 +55,9 @@ def test_non_utf8_file(tmpdir):
     assert main((str(f_py),)) == 0
 
 
-def test_py37_breakpoint(tmpdir):
+def test_py37_breakpoint(tmpdir, capsys):
     f_py = tmpdir.join('f.py')
     f_py.write('def f():\n    breakpoint()\n')
     assert main((str(f_py),)) == 1
+    out, _ = capsys.readouterr()
+    assert out == f'{f_py}:2:4: breakpoint called\n'