Skip to content

Commit 0fb981b

Browse files
authored
Add blacken-docs and pycln pre-commit hooks (#3292)
* Apply blacken-docs and fix language-hints * Add blacken-docs pre-commit hook * Add pycln pre-commit hook * Enable a few builtin hooks * Black no longer ignores pyi files
1 parent ee0c5ee commit 0fb981b

File tree

17 files changed

+72
-60
lines changed

17 files changed

+72
-60
lines changed

.pre-commit-config.yaml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ repos:
1919
hooks:
2020
- id: check-added-large-files
2121
- id: check-case-conflict
22+
- id: check-docstring-first
2223
- id: check-merge-conflict
2324
- id: check-symlinks
25+
- id: check-toml
2426
- id: check-yaml
2527
- id: debug-statements
2628
- id: end-of-file-fixer
@@ -42,19 +44,29 @@ repos:
4244

4345
# Black, the code formatter, natively supports pre-commit
4446
- repo: https://github.com/psf/black
45-
rev: 21.9b0
47+
rev: 21.9b0 # Keep in sync with blacken-docs
4648
hooks:
4749
- id: black
48-
# By default, this ignores pyi files, though black supports them
49-
types: [text]
50-
files: \.pyi?$
50+
51+
- repo: https://github.com/asottile/blacken-docs
52+
rev: v1.11.0
53+
hooks:
54+
- id: blacken-docs
55+
additional_dependencies:
56+
- black==21.9b0 # keep in sync with black hook
5157

5258
# Changes tabs to spaces
5359
- repo: https://github.com/Lucas-C/pre-commit-hooks
5460
rev: v1.1.10
5561
hooks:
5662
- id: remove-tabs
5763

64+
# Autoremoves unused imports
65+
- repo: https://github.com/hadialqattan/pycln
66+
rev: v1.0.3
67+
hooks:
68+
- id: pycln
69+
5870
# Flake8 also supports pre-commit natively (same author)
5971
- repo: https://github.com/PyCQA/flake8
6072
rev: 3.9.2
@@ -86,7 +98,7 @@ repos:
8698

8799
# Checks the manifest for missing files (native support)
88100
- repo: https://github.com/mgedmin/check-manifest
89-
rev: "0.46"
101+
rev: "0.47"
90102
hooks:
91103
- id: check-manifest
92104
# This is a slow hook, so only run this if --hook-stage manual is passed
@@ -100,10 +112,10 @@ repos:
100112
exclude: ".supp$"
101113
args: ["-L", "nd,ot,thist"]
102114

103-
- repo: https://github.com/shellcheck-py/shellcheck-py
104-
rev: v0.7.2.1
105-
hooks:
106-
- id: shellcheck
115+
- repo: https://github.com/shellcheck-py/shellcheck-py
116+
rev: v0.7.2.1
117+
hooks:
118+
- id: shellcheck
107119

108120
# The original pybind11 checks for a few C++ style items
109121
- repo: local

docs/advanced/cast/custom.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ The following Python snippet demonstrates the intended usage from the Python sid
2626
def __int__(self):
2727
return 123
2828
29+
2930
from example import print
31+
3032
print(A())
3133
3234
To register the necessary conversion routines, it is necessary to add an

docs/advanced/cast/eigen.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ example:
112112
.. code-block:: python
113113
114114
a = MyClass()
115-
m = a.get_matrix() # flags.writeable = True, flags.owndata = False
115+
m = a.get_matrix() # flags.writeable = True, flags.owndata = False
116116
v = a.view_matrix() # flags.writeable = False, flags.owndata = False
117117
c = a.copy_matrix() # flags.writeable = True, flags.owndata = True
118118
# m[5,6] and v[5,6] refer to the same element, c[5,6] does not.
@@ -203,7 +203,7 @@ adding the ``order='F'`` option when creating an array:
203203

204204
.. code-block:: python
205205
206-
myarray = np.array(source, order='F')
206+
myarray = np.array(source, order="F")
207207
208208
Such an object will be passable to a bound function accepting an
209209
``Eigen::Ref<MatrixXd>`` (or similar column-major Eigen type).

docs/advanced/cast/strings.rst

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ everywhere <http://utf8everywhere.org/>`_.
3636
}
3737
);
3838
39-
.. code-block:: python
39+
.. code-block:: pycon
4040
41-
>>> utf8_test('🎂')
41+
>>> utf8_test("🎂")
4242
utf-8 is icing on the cake.
4343
🎂
4444
45-
>>> utf8_charptr('🍕')
45+
>>> utf8_charptr("🍕")
4646
My favorite food is
4747
🍕
4848
@@ -80,7 +80,7 @@ raise a ``UnicodeDecodeError``.
8080
}
8181
);
8282

83-
.. code-block:: python
83+
.. code-block:: pycon
8484
8585
>>> isinstance(example.std_string_return(), str)
8686
True
@@ -114,7 +114,7 @@ conversion has the same overhead as implicit conversion.
114114
}
115115
);
116116

117-
.. code-block:: python
117+
.. code-block:: pycon
118118
119119
>>> str_output()
120120
'Send your résumé to Alice in HR'
@@ -143,7 +143,7 @@ returned to Python as ``bytes``, then one can return the data as a
143143
}
144144
);
145145

146-
.. code-block:: python
146+
.. code-block:: pycon
147147
148148
>>> example.return_bytes()
149149
b'\xba\xd0\xba\xd0'
@@ -160,7 +160,7 @@ encoding, but cannot convert ``std::string`` back to ``bytes`` implicitly.
160160
}
161161
);
162162

163-
.. code-block:: python
163+
.. code-block:: pycon
164164
165165
>>> isinstance(example.asymmetry(b"have some bytes"), str)
166166
True
@@ -229,16 +229,16 @@ character.
229229
m.def("pass_char", [](char c) { return c; });
230230
m.def("pass_wchar", [](wchar_t w) { return w; });
231231

232-
.. code-block:: python
232+
.. code-block:: pycon
233233
234-
>>> example.pass_char('A')
234+
>>> example.pass_char("A")
235235
'A'
236236
237237
While C++ will cast integers to character types (``char c = 0x65;``), pybind11
238238
does not convert Python integers to characters implicitly. The Python function
239239
``chr()`` can be used to convert integers to characters.
240240

241-
.. code-block:: python
241+
.. code-block:: pycon
242242
243243
>>> example.pass_char(0x65)
244244
TypeError
@@ -259,17 +259,17 @@ a combining acute accent). The combining character will be lost if the
259259
two-character sequence is passed as an argument, even though it renders as a
260260
single grapheme.
261261

262-
.. code-block:: python
262+
.. code-block:: pycon
263263
264-
>>> example.pass_wchar('é')
264+
>>> example.pass_wchar("é")
265265
'é'
266266
267-
>>> combining_e_acute = 'e' + '\u0301'
267+
>>> combining_e_acute = "e" + "\u0301"
268268
269269
>>> combining_e_acute
270270
'é'
271271
272-
>>> combining_e_acute == 'é'
272+
>>> combining_e_acute == "é"
273273
False
274274
275275
>>> example.pass_wchar(combining_e_acute)
@@ -278,9 +278,9 @@ single grapheme.
278278
Normalizing combining characters before passing the character literal to C++
279279
may resolve *some* of these issues:
280280

281-
.. code-block:: python
281+
.. code-block:: pycon
282282
283-
>>> example.pass_wchar(unicodedata.normalize('NFC', combining_e_acute))
283+
>>> example.pass_wchar(unicodedata.normalize("NFC", combining_e_acute))
284284
'é'
285285
286286
In some languages (Thai for example), there are `graphemes that cannot be

docs/advanced/classes.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ a virtual method call.
136136
u'woof! woof! woof! '
137137
>>> class Cat(Animal):
138138
... def go(self, n_times):
139-
... return "meow! " * n_times
139+
... return "meow! " * n_times
140140
...
141141
>>> c = Cat()
142142
>>> call_go(c)
@@ -159,8 +159,9 @@ Here is an example:
159159
160160
class Dachshund(Dog):
161161
def __init__(self, name):
162-
Dog.__init__(self) # Without this, a TypeError is raised.
162+
Dog.__init__(self) # Without this, a TypeError is raised.
163163
self.name = name
164+
164165
def bark(self):
165166
return "yap!"
166167
@@ -1153,6 +1154,7 @@ error:
11531154
11541155
>>> class PyFinalChild(IsFinal):
11551156
... pass
1157+
...
11561158
TypeError: type 'IsFinal' is not an acceptable base type
11571159
11581160
.. note:: This attribute is currently ignored on PyPy
@@ -1247,7 +1249,7 @@ Accessing the type object
12471249

12481250
You can get the type object from a C++ class that has already been registered using:
12491251

1250-
.. code-block:: python
1252+
.. code-block:: cpp
12511253
12521254
py::type T_py = py::type::of<T>();
12531255

docs/advanced/embedding.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ embedding the interpreter. This makes it easy to import local Python files:
122122
123123
"""calc.py located in the working directory"""
124124
125+
125126
def add(i, j):
126127
return i + j
127128

docs/advanced/functions.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ And used in Python as usual:
272272

273273
.. code-block:: pycon
274274
275-
>>> print_dict({'foo': 123, 'bar': 'hello'})
275+
>>> print_dict({"foo": 123, "bar": "hello"})
276276
key=foo, value=123
277277
key=bar, value=hello
278278
@@ -377,10 +377,11 @@ argument in a function definition:
377377
def f(a, *, b): # a can be positional or via keyword; b must be via keyword
378378
pass
379379
380+
380381
f(a=1, b=2) # good
381382
f(b=2, a=1) # good
382-
f(1, b=2) # good
383-
f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given
383+
f(1, b=2) # good
384+
f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given
384385
385386
Pybind11 provides a ``py::kw_only`` object that allows you to implement
386387
the same behaviour by specifying the object between positional and keyword-only

docs/advanced/pycpp/numpy.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ by the compiler. The result is returned as a NumPy array of type
258258

259259
.. code-block:: pycon
260260
261-
>>> x = np.array([[1, 3],[5, 7]])
262-
>>> y = np.array([[2, 4],[6, 8]])
261+
>>> x = np.array([[1, 3], [5, 7]])
262+
>>> y = np.array([[2, 4], [6, 8]])
263263
>>> z = 3
264264
>>> result = vectorized_func(x, y, z)
265265
@@ -403,7 +403,7 @@ In Python 2, the syntactic sugar ``...`` is not available, but the singleton
403403

404404
.. code-block:: python
405405
406-
a = # a NumPy array
406+
a = ... # a NumPy array
407407
b = a[0, ..., 0]
408408
409409
The function ``py::ellipsis()`` function can be used to perform the same

docs/advanced/pycpp/object.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Keyword arguments are also supported. In Python, there is the usual call syntax:
173173
def f(number, say, to):
174174
... # function code
175175
176+
176177
f(1234, say="hello", to=some_instance) # keyword call in Python
177178
178179
In C++, the same call can be made using:

docs/advanced/pycpp/utilities.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extra type, `py::scoped_estream_redirect <scoped_estream_redirect>`, is identica
6666
except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with
6767
`py::call_guard`, which allows multiple items, but uses the default constructor:
6868

69-
.. code-block:: py
69+
.. code-block:: cpp
7070
7171
// Alternative: Call single function using call guard
7272
m.def("noisy_func", &call_noisy_function,

0 commit comments

Comments
 (0)