Skip to content

Add blacken-docs and pycln pre-commit hooks #3292

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 6 commits into from
Sep 22, 2021
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
30 changes: 21 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ repos:
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-docstring-first
- id: check-merge-conflict
- id: check-symlinks
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
Expand All @@ -42,19 +44,29 @@ repos:

# Black, the code formatter, natively supports pre-commit
- repo: https://github.com/psf/black
rev: 21.9b0
rev: 21.9b0 # Keep in sync with blacken-docs
hooks:
- id: black
# By default, this ignores pyi files, though black supports them
types: [text]
files: \.pyi?$

- repo: https://github.com/asottile/blacken-docs
rev: v1.11.0
hooks:
- id: blacken-docs
additional_dependencies:
- black==21.9b0 # keep in sync with black hook

# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.10
hooks:
- id: remove-tabs

# Autoremoves unused imports
- repo: https://github.com/hadialqattan/pycln
rev: v1.0.3
hooks:
- id: pycln

# Flake8 also supports pre-commit natively (same author)
- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
Expand Down Expand Up @@ -86,7 +98,7 @@ repos:

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

- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.7.2.1
hooks:
- id: shellcheck
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.7.2.1
hooks:
- id: shellcheck

# The original pybind11 checks for a few C++ style items
- repo: local
Expand Down
2 changes: 2 additions & 0 deletions docs/advanced/cast/custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ The following Python snippet demonstrates the intended usage from the Python sid
def __int__(self):
return 123


from example import print

print(A())

To register the necessary conversion routines, it is necessary to add an
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced/cast/eigen.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ example:
.. code-block:: python

a = MyClass()
m = a.get_matrix() # flags.writeable = True, flags.owndata = False
m = a.get_matrix() # flags.writeable = True, flags.owndata = False
v = a.view_matrix() # flags.writeable = False, flags.owndata = False
c = a.copy_matrix() # flags.writeable = True, flags.owndata = True
# m[5,6] and v[5,6] refer to the same element, c[5,6] does not.
Expand Down Expand Up @@ -203,7 +203,7 @@ adding the ``order='F'`` option when creating an array:

.. code-block:: python

myarray = np.array(source, order='F')
myarray = np.array(source, order="F")

Such an object will be passable to a bound function accepting an
``Eigen::Ref<MatrixXd>`` (or similar column-major Eigen type).
Expand Down
32 changes: 16 additions & 16 deletions docs/advanced/cast/strings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ everywhere <http://utf8everywhere.org/>`_.
}
);

.. code-block:: python
.. code-block:: pycon

>>> utf8_test('🎂')
>>> utf8_test("🎂")
utf-8 is icing on the cake.
🎂

>>> utf8_charptr('🍕')
>>> utf8_charptr("🍕")
My favorite food is
🍕

Expand Down Expand Up @@ -80,7 +80,7 @@ raise a ``UnicodeDecodeError``.
}
);

.. code-block:: python
.. code-block:: pycon

>>> isinstance(example.std_string_return(), str)
True
Expand Down Expand Up @@ -114,7 +114,7 @@ conversion has the same overhead as implicit conversion.
}
);

.. code-block:: python
.. code-block:: pycon

>>> str_output()
'Send your résumé to Alice in HR'
Expand Down Expand Up @@ -143,7 +143,7 @@ returned to Python as ``bytes``, then one can return the data as a
}
);

.. code-block:: python
.. code-block:: pycon

>>> example.return_bytes()
b'\xba\xd0\xba\xd0'
Expand All @@ -160,7 +160,7 @@ encoding, but cannot convert ``std::string`` back to ``bytes`` implicitly.
}
);

.. code-block:: python
.. code-block:: pycon

>>> isinstance(example.asymmetry(b"have some bytes"), str)
True
Expand Down Expand Up @@ -229,16 +229,16 @@ character.
m.def("pass_char", [](char c) { return c; });
m.def("pass_wchar", [](wchar_t w) { return w; });

.. code-block:: python
.. code-block:: pycon

>>> example.pass_char('A')
>>> example.pass_char("A")
'A'

While C++ will cast integers to character types (``char c = 0x65;``), pybind11
does not convert Python integers to characters implicitly. The Python function
``chr()`` can be used to convert integers to characters.

.. code-block:: python
.. code-block:: pycon

>>> example.pass_char(0x65)
TypeError
Expand All @@ -259,17 +259,17 @@ a combining acute accent). The combining character will be lost if the
two-character sequence is passed as an argument, even though it renders as a
single grapheme.

.. code-block:: python
.. code-block:: pycon

>>> example.pass_wchar('é')
>>> example.pass_wchar("é")
'é'

>>> combining_e_acute = 'e' + '\u0301'
>>> combining_e_acute = "e" + "\u0301"

>>> combining_e_acute
'é'

>>> combining_e_acute == 'é'
>>> combining_e_acute == "é"
False

>>> example.pass_wchar(combining_e_acute)
Expand All @@ -278,9 +278,9 @@ single grapheme.
Normalizing combining characters before passing the character literal to C++
may resolve *some* of these issues:

.. code-block:: python
.. code-block:: pycon

>>> example.pass_wchar(unicodedata.normalize('NFC', combining_e_acute))
>>> example.pass_wchar(unicodedata.normalize("NFC", combining_e_acute))
'é'

In some languages (Thai for example), there are `graphemes that cannot be
Expand Down
8 changes: 5 additions & 3 deletions docs/advanced/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ a virtual method call.
u'woof! woof! woof! '
>>> class Cat(Animal):
... def go(self, n_times):
... return "meow! " * n_times
... return "meow! " * n_times
...
>>> c = Cat()
>>> call_go(c)
Expand All @@ -159,8 +159,9 @@ Here is an example:

class Dachshund(Dog):
def __init__(self, name):
Dog.__init__(self) # Without this, a TypeError is raised.
Dog.__init__(self) # Without this, a TypeError is raised.
self.name = name

def bark(self):
return "yap!"

Expand Down Expand Up @@ -1153,6 +1154,7 @@ error:

>>> class PyFinalChild(IsFinal):
... pass
...
TypeError: type 'IsFinal' is not an acceptable base type

.. note:: This attribute is currently ignored on PyPy
Expand Down Expand Up @@ -1247,7 +1249,7 @@ Accessing the type object

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

.. code-block:: python
.. code-block:: cpp

py::type T_py = py::type::of<T>();

Expand Down
1 change: 1 addition & 0 deletions docs/advanced/embedding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ embedding the interpreter. This makes it easy to import local Python files:

"""calc.py located in the working directory"""


def add(i, j):
return i + j

Expand Down
7 changes: 4 additions & 3 deletions docs/advanced/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ And used in Python as usual:

.. code-block:: pycon

>>> print_dict({'foo': 123, 'bar': 'hello'})
>>> print_dict({"foo": 123, "bar": "hello"})
key=foo, value=123
key=bar, value=hello

Expand Down Expand Up @@ -377,10 +377,11 @@ argument in a function definition:
def f(a, *, b): # a can be positional or via keyword; b must be via keyword
pass


f(a=1, b=2) # good
f(b=2, a=1) # good
f(1, b=2) # good
f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given
f(1, b=2) # good
f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given

Pybind11 provides a ``py::kw_only`` object that allows you to implement
the same behaviour by specifying the object between positional and keyword-only
Expand Down
6 changes: 3 additions & 3 deletions docs/advanced/pycpp/numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ by the compiler. The result is returned as a NumPy array of type

.. code-block:: pycon

>>> x = np.array([[1, 3],[5, 7]])
>>> y = np.array([[2, 4],[6, 8]])
>>> x = np.array([[1, 3], [5, 7]])
>>> y = np.array([[2, 4], [6, 8]])
>>> z = 3
>>> result = vectorized_func(x, y, z)

Expand Down Expand Up @@ -403,7 +403,7 @@ In Python 2, the syntactic sugar ``...`` is not available, but the singleton

.. code-block:: python

a = # a NumPy array
a = ... # a NumPy array
b = a[0, ..., 0]

The function ``py::ellipsis()`` function can be used to perform the same
Expand Down
1 change: 1 addition & 0 deletions docs/advanced/pycpp/object.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Keyword arguments are also supported. In Python, there is the usual call syntax:
def f(number, say, to):
... # function code


f(1234, say="hello", to=some_instance) # keyword call in Python

In C++, the same call can be made using:
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced/pycpp/utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extra type, `py::scoped_estream_redirect <scoped_estream_redirect>`, is identica
except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with
`py::call_guard`, which allows multiple items, but uses the default constructor:

.. code-block:: py
.. code-block:: cpp

// Alternative: Call single function using call guard
m.def("noisy_func", &call_noisy_function,
Expand Down
1 change: 1 addition & 0 deletions docs/advanced/smart_ptrs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ segmentation fault).
.. code-block:: python

from example import Parent

print(Parent().get_child())

The problem is that ``Parent::get_child()`` returns a pointer to an instance of
Expand Down
1 change: 0 additions & 1 deletion docs/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import datetime as dt
import os
import random
import time

nfns = 4 # Functions per class
nargs = 4 # Arguments per function
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ v2.2.0 (August 31, 2017)

from cpp_module import CppBase1, CppBase2


class PyDerived(CppBase1, CppBase2):
def __init__(self):
CppBase1.__init__(self) # C++ bases must be initialized explicitly
Expand Down
Loading