Skip to content

Add pre-commit hooks for autofixing trailing whitespace #15237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Adopt black and isort
97c5ee99bc98dc475512e549b252b23a6e7e0997
97c5ee99bc98dc475512e549b252b23a6e7e0997
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
exclude: '^(mypyc/external/)|(mypy/typeshed/)' # Exclude all vendored code from lints
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0 # must match test-requirements.txt
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: 23.3.0 # must match test-requirements.txt
hooks:
Expand Down
2 changes: 1 addition & 1 deletion mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "CPy.h"

// Returns -1 on error, 0 on inequality, 1 on equality.
//
//
// Falls back to PyObject_RichCompareBool.
int CPyBytes_Compare(PyObject *left, PyObject *right) {
if (PyBytes_CheckExact(left) && PyBytes_CheckExact(right)) {
Expand Down
1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-dict.test
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,3 @@ L2:
L3:
r7 = box(None, 1)
return r7

1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-nested.test
Original file line number Diff line number Diff line change
Expand Up @@ -859,4 +859,3 @@ L2:
r2 = baz(r1)
r3 = CPyTagged_Add(n, r2)
return r3

1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-set.test
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,3 @@ L4:
r11 = CPy_NoErrOccured()
L5:
return 1

1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-strip-asserts.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ L0:
r0 = object 3
x = r0
return x

2 changes: 1 addition & 1 deletion mypyc/test-data/run-bools.test
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,4 @@ def test_mixed_comparisons_i64() -> None:
y = False
print((y or 0) and True)
[out]
0
0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bar_var: str
bar_var: str
2 changes: 1 addition & 1 deletion test-data/packages/modulefinder-site-packages/foo/bar.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bar_var = "bar"
bar_var = "bar"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a_var = "a"
a_var = "a"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c_var = "c"
c_var = "c"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a_var = "a"
a_var = "a"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c_var = "c"
c_var = "c"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pkg_typed_var = "pkg_typed"
pkg_typed_var = "pkg_typed"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a_var = "a"
a_var = "a"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b_var = "b"
b_var = "b"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c_var = "c"
c_var = "c"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pkg_untyped_var = "pkg_untyped"
pkg_untyped_var = "pkg_untyped"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a_var = "a"
a_var = "a"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b_var = "b"
b_var = "b"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c_var = "c"
c_var = "c"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
standalone_var = "standalone"
standalone_var = "standalone"
2 changes: 1 addition & 1 deletion test-data/pybind11_mypy_demo/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ requires = [
"pybind11==2.9.2",
]

build-backend = "setuptools.build_meta"
build-backend = "setuptools.build_meta"
2 changes: 1 addition & 1 deletion test-data/pybind11_mypy_demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@ void bind_basics(py::module& basics) {
PYBIND11_MODULE(pybind11_mypy_demo, m) {
auto basics = m.def_submodule("basics");
bind_basics(basics);
}
}
2 changes: 1 addition & 1 deletion test-data/unit/check-assert-type-fail.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class array:
i = 1
def f(si: arr.array[int]):
typing.assert_type(si, int) # E: Expression is of type "array[int]", not "int"
[builtins fixtures/tuple.pyi]
[builtins fixtures/tuple.pyi]
1 change: 0 additions & 1 deletion test-data/unit/check-python39.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ a = b = c = [1, 2, 3]
for x in *a, *b, *c:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

2 changes: 1 addition & 1 deletion test-data/unit/check-singledispatch.test
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def g(arg: int) -> None:
from functools import singledispatch

@singledispatch
def f(arg) -> None:
def f(arg) -> None:
pass

@f.register(str)
Expand Down
1 change: 0 additions & 1 deletion test-data/unit/envvars.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ BAR = 0 # type: int
[file subdir/mypy.ini]
\[mypy]
files=$MYPY_CONFIG_FILE_DIR/good.py

2 changes: 1 addition & 1 deletion test-data/unit/pep561.test
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,4 @@ from typedpkg_ns.a.bbb import bf
-- dummy should trigger a second iteration
[file dummy.py.2]
[out]
[out2]
[out2]
2 changes: 1 addition & 1 deletion test-data/unit/plugins/fully_qualified_test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def get_method_signature_hook(self, fullname):
if 'FullyQualifiedTest' in fullname:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just run Black on this?

Copy link
Member Author

@AlexWaygood AlexWaygood May 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black makes quite a few change if I apply this diff to our pyproject.toml file:

diff --git a/pyproject.toml b/pyproject.toml
index 3d100dff5..a06acccf2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -23,8 +23,6 @@ target-version = ["py37", "py38", "py39", "py310", "py311"]
 skip-magic-trailing-comma = true
 force-exclude = '''
 ^/mypy/typeshed|
-^/mypyc/test-data|
-^/test-data
 '''

 [tool.isort]

I'm happy to consider doing that, but maybe that should be left for another PR? Feels like running these new hooks on our test-data/ files and running black on our test-data/ files aren't really mutually exclusive.

assert fullname.startswith('__main__.') and not ' of ' in fullname, fullname
return my_hook

return None

def my_hook(ctx: MethodSigContext) -> CallableType:
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ flake8-noqa==1.3.1; python_version >= "3.8" # must match version in .pre-co
isort[colors]==5.12.0; python_version >= "3.8" # must match version in .pre-commit-config.yaml
lxml>=4.9.1; (python_version<'3.11' or sys_platform!='win32') and python_version<'3.12'
pre-commit
pre-commit-hooks==4.4.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can add this for consistency, but I don't think people will use pre-commit-hooks as a standalone package. I didn't even know it could be run standalone until now.

Copy link
Member Author

@AlexWaygood AlexWaygood May 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does seem unlikely that people are going to be manually running trailing-whitespace mypy/semanal.py locally (for example). And it's true that pre-commit requirements don't need to be pre-installed for pre-commit to work locally. So, I'm happy to remove this -- but curious to hear what others think, as well.

psutil>=4.0
# pytest 6.2.3 does not support Python 3.10
pytest>=6.2.4
Expand Down