Skip to content

Commit a9e850f

Browse files
authored
Merge pull request #5096 from asottile/docs_highlight
blacken-docs more code samples in docs
2 parents 1dafe96 + da2e092 commit a9e850f

File tree

13 files changed

+305
-131
lines changed

13 files changed

+305
-131
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
exclude: doc/en/example/py2py3/test_py2.py
22
repos:
33
- repo: https://github.com/ambv/black
4-
rev: 18.9b0
4+
rev: 19.3b0
55
hooks:
66
- id: black
77
args: [--safe, --quiet]
88
language_version: python3
99
- repo: https://github.com/asottile/blacken-docs
10-
rev: v0.3.0
10+
rev: v0.5.0
1111
hooks:
1212
- id: blacken-docs
13-
additional_dependencies: [black==18.9b0]
13+
additional_dependencies: [black==19.3b0]
1414
language_version: python3
1515
- repo: https://github.com/pre-commit/pre-commit-hooks
1616
rev: v2.1.0
@@ -22,22 +22,22 @@ repos:
2222
exclude: _pytest/debugging.py
2323
language_version: python3
2424
- repo: https://gitlab.com/pycqa/flake8
25-
rev: 3.7.0
25+
rev: 3.7.7
2626
hooks:
2727
- id: flake8
2828
language_version: python3
2929
- repo: https://github.com/asottile/reorder_python_imports
30-
rev: v1.3.5
30+
rev: v1.4.0
3131
hooks:
3232
- id: reorder-python-imports
3333
args: ['--application-directories=.:src']
3434
- repo: https://github.com/asottile/pyupgrade
35-
rev: v1.11.1
35+
rev: v1.15.0
3636
hooks:
3737
- id: pyupgrade
3838
args: [--keep-percent-format]
3939
- repo: https://github.com/pre-commit/pygrep-hooks
40-
rev: v1.2.0
40+
rev: v1.3.0
4141
hooks:
4242
- id: rst-backticks
4343
- repo: local

bench/bench_argcomplete.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
if __name__ == "__main__":
1818
print(timeit.timeit(run, setup=setup % imports[0], number=count))
19-
print((timeit.timeit(run, setup=setup % imports[1], number=count)))
19+
print(timeit.timeit(run, setup=setup % imports[1], number=count))

doc/en/assert.rst

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ Asserting with the ``assert`` statement
1212

1313
``pytest`` allows you to use the standard python ``assert`` for verifying
1414
expectations and values in Python tests. For example, you can write the
15-
following::
15+
following:
16+
17+
.. code-block:: python
1618
1719
# content of test_assert1.py
1820
def f():
1921
return 3
2022
23+
2124
def test_function():
2225
assert f() == 4
2326
@@ -52,7 +55,9 @@ operators. (See :ref:`tbreportdemo`). This allows you to use the
5255
idiomatic python constructs without boilerplate code while not losing
5356
introspection information.
5457

55-
However, if you specify a message with the assertion like this::
58+
However, if you specify a message with the assertion like this:
59+
60+
.. code-block:: python
5661
5762
assert a % 2 == 0, "value was odd, should be even"
5863
@@ -67,38 +72,49 @@ Assertions about expected exceptions
6772
------------------------------------------
6873

6974
In order to write assertions about raised exceptions, you can use
70-
``pytest.raises`` as a context manager like this::
75+
``pytest.raises`` as a context manager like this:
76+
77+
.. code-block:: python
7178
7279
import pytest
7380
81+
7482
def test_zero_division():
7583
with pytest.raises(ZeroDivisionError):
7684
1 / 0
7785
78-
and if you need to have access to the actual exception info you may use::
86+
and if you need to have access to the actual exception info you may use:
87+
88+
.. code-block:: python
7989
8090
def test_recursion_depth():
8191
with pytest.raises(RuntimeError) as excinfo:
92+
8293
def f():
8394
f()
95+
8496
f()
85-
assert 'maximum recursion' in str(excinfo.value)
97+
assert "maximum recursion" in str(excinfo.value)
8698
8799
``excinfo`` is a ``ExceptionInfo`` instance, which is a wrapper around
88100
the actual exception raised. The main attributes of interest are
89101
``.type``, ``.value`` and ``.traceback``.
90102

91103
You can pass a ``match`` keyword parameter to the context-manager to test
92104
that a regular expression matches on the string representation of an exception
93-
(similar to the ``TestCase.assertRaisesRegexp`` method from ``unittest``)::
105+
(similar to the ``TestCase.assertRaisesRegexp`` method from ``unittest``):
106+
107+
.. code-block:: python
94108
95109
import pytest
96110
111+
97112
def myfunc():
98113
raise ValueError("Exception 123 raised")
99114
115+
100116
def test_match():
101-
with pytest.raises(ValueError, match=r'.* 123 .*'):
117+
with pytest.raises(ValueError, match=r".* 123 .*"):
102118
myfunc()
103119
104120
The regexp parameter of the ``match`` method is matched with the ``re.search``
@@ -107,7 +123,9 @@ well.
107123

108124
There's an alternate form of the ``pytest.raises`` function where you pass
109125
a function that will be executed with the given ``*args`` and ``**kwargs`` and
110-
assert that the given exception is raised::
126+
assert that the given exception is raised:
127+
128+
.. code-block:: python
111129
112130
pytest.raises(ExpectedException, func, *args, **kwargs)
113131
@@ -116,7 +134,9 @@ exception* or *wrong exception*.
116134

117135
Note that it is also possible to specify a "raises" argument to
118136
``pytest.mark.xfail``, which checks that the test is failing in a more
119-
specific way than just having any exception raised::
137+
specific way than just having any exception raised:
138+
139+
.. code-block:: python
120140
121141
@pytest.mark.xfail(raises=IndexError)
122142
def test_f():
@@ -148,10 +168,13 @@ Making use of context-sensitive comparisons
148168
.. versionadded:: 2.0
149169

150170
``pytest`` has rich support for providing context-sensitive information
151-
when it encounters comparisons. For example::
171+
when it encounters comparisons. For example:
172+
173+
.. code-block:: python
152174
153175
# content of test_assert2.py
154176
177+
155178
def test_set_comparison():
156179
set1 = set("1308")
157180
set2 = set("8035")
@@ -205,16 +228,21 @@ the ``pytest_assertrepr_compare`` hook.
205228
:noindex:
206229

207230
As an example consider adding the following hook in a :ref:`conftest.py <conftest.py>`
208-
file which provides an alternative explanation for ``Foo`` objects::
231+
file which provides an alternative explanation for ``Foo`` objects:
232+
233+
.. code-block:: python
209234
210235
# content of conftest.py
211236
from test_foocompare import Foo
237+
238+
212239
def pytest_assertrepr_compare(op, left, right):
213240
if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":
214-
return ['Comparing Foo instances:',
215-
' vals: %s != %s' % (left.val, right.val)]
241+
return ["Comparing Foo instances:", " vals: %s != %s" % (left.val, right.val)]
216242
217-
now, given this test module::
243+
now, given this test module:
244+
245+
.. code-block:: python
218246
219247
# content of test_foocompare.py
220248
class Foo(object):
@@ -224,6 +252,7 @@ now, given this test module::
224252
def __eq__(self, other):
225253
return self.val == other.val
226254
255+
227256
def test_compare():
228257
f1 = Foo(1)
229258
f2 = Foo(2)

0 commit comments

Comments
 (0)