Skip to content

Commit 71878f8

Browse files
author
boris
committed
add pyupgrade-docs to pre-commit-config
1 parent d6522b5 commit 71878f8

File tree

10 files changed

+32
-23
lines changed

10 files changed

+32
-23
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ repos:
99
hooks:
1010
- id: blacken-docs
1111
additional_dependencies: [black==20.8b1]
12+
- repo: https://github.com/verhovsky/pyupgrade-docs
13+
rev: v0.2.5
14+
hooks:
15+
- id: pyupgrade-docs
16+
args: [--py36-plus]
1217
- repo: https://github.com/pre-commit/pre-commit-hooks
1318
rev: v3.4.0
1419
hooks:

doc/en/assert.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,7 @@ file which provides an alternative explanation for ``Foo`` objects:
240240
241241
def pytest_assertrepr_compare(op, left, right):
242242
if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":
243-
return [
244-
"Comparing Foo instances:",
245-
" vals: {} != {}".format(left.val, right.val),
246-
]
243+
return ["Comparing Foo instances:", f" vals: {left.val} != {right.val}"]
247244
248245
now, given this test module:
249246

doc/en/doctest.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ pytest also introduces new options:
126126

127127
* ``NUMBER``: when enabled, floating-point numbers only need to match as far as
128128
the precision you have written in the expected doctest output. For example,
129-
the following output would only need to match to 2 decimal places::
129+
the following output would only need to match to 2 decimal places:
130+
131+
.. code-block:: pycon
130132
131133
>>> math.pi
132134
3.14

doc/en/example/markers.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ specifies via named environments:
375375
envnames = [mark.args[0] for mark in item.iter_markers(name="env")]
376376
if envnames:
377377
if item.config.getoption("-E") not in envnames:
378-
pytest.skip("test requires env in {!r}".format(envnames))
378+
pytest.skip(f"test requires env in {envnames!r}")
379379
380380
A test file using this local plugin:
381381

@@ -530,7 +530,7 @@ test function. From a conftest file we can read it like this:
530530
531531
def pytest_runtest_setup(item):
532532
for mark in item.iter_markers(name="glob"):
533-
print("glob args={} kwargs={}".format(mark.args, mark.kwargs))
533+
print(f"glob args={mark.args} kwargs={mark.kwargs}")
534534
sys.stdout.flush()
535535
536536
Let's run this without capturing output and see what we get:
@@ -569,7 +569,7 @@ for your particular platform, you could use the following plugin:
569569
supported_platforms = ALL.intersection(mark.name for mark in item.iter_markers())
570570
plat = sys.platform
571571
if supported_platforms and plat not in supported_platforms:
572-
pytest.skip("cannot run on platform {}".format(plat))
572+
pytest.skip(f"cannot run on platform {plat}")
573573
574574
then tests will be skipped if they were specified for a different platform.
575575
Let's do a little test file to show how this looks like:

doc/en/example/simple.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Example:
288288
def checkconfig(x):
289289
__tracebackhide__ = True
290290
if not hasattr(x, "config"):
291-
pytest.fail("not configured: {}".format(x))
291+
pytest.fail(f"not configured: {x}")
292292
293293
294294
def test_something():
@@ -332,7 +332,7 @@ this to make sure unexpected exception types aren't hidden:
332332
def checkconfig(x):
333333
__tracebackhide__ = operator.methodcaller("errisinstance", ConfigException)
334334
if not hasattr(x, "config"):
335-
raise ConfigException("not configured: {}".format(x))
335+
raise ConfigException(f"not configured: {x}")
336336
337337
338338
def test_something():
@@ -557,7 +557,7 @@ an ``incremental`` marker which is to be used on classes:
557557
test_name = _test_failed_incremental[cls_name].get(parametrize_index, None)
558558
# if name found, test has failed for the combination of class name & test name
559559
if test_name is not None:
560-
pytest.xfail("previous test failed ({})".format(test_name))
560+
pytest.xfail(f"previous test failed ({test_name})")
561561
562562
563563
These two hook implementations work together to abort incremental-marked

doc/en/fixture.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ read an optional server URL from the test module which uses our fixture:
16811681
server = getattr(request.module, "smtpserver", "smtp.gmail.com")
16821682
smtp_connection = smtplib.SMTP(server, 587, timeout=5)
16831683
yield smtp_connection
1684-
print("finalizing {} ({})".format(smtp_connection, server))
1684+
print(f"finalizing {smtp_connection} ({server})")
16851685
smtp_connection.close()
16861686
16871687
We use the ``request.module`` attribute to optionally obtain an
@@ -1843,7 +1843,7 @@ through the special :py:class:`request <FixtureRequest>` object:
18431843
def smtp_connection(request):
18441844
smtp_connection = smtplib.SMTP(request.param, 587, timeout=5)
18451845
yield smtp_connection
1846-
print("finalizing {}".format(smtp_connection))
1846+
print(f"finalizing {smtp_connection}")
18471847
smtp_connection.close()
18481848
18491849
The main change is the declaration of ``params`` with
@@ -2146,7 +2146,7 @@ to show the setup/teardown flow:
21462146
21472147
21482148
def test_2(otherarg, modarg):
2149-
print(" RUN test2 with otherarg {} and modarg {}".format(otherarg, modarg))
2149+
print(f" RUN test2 with otherarg {otherarg} and modarg {modarg}")
21502150
21512151
21522152
Let's run the tests in verbose mode and with looking at the print-output:

doc/en/logging.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ the records for the ``setup`` and ``call`` stages during teardown like so:
164164
x.message for x in caplog.get_records(when) if x.levelno == logging.WARNING
165165
]
166166
if messages:
167-
pytest.fail(
168-
"warning messages encountered during testing: {}".format(messages)
169-
)
167+
pytest.fail(f"warning messages encountered during testing: {messages}")
170168
171169
172170

doc/en/usage.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ Note that on any failure the exception information is stored on
371371
``sys.last_value``, ``sys.last_type`` and ``sys.last_traceback``. In
372372
interactive use, this allows one to drop into postmortem debugging with
373373
any debug tool. One can also manually access the exception information,
374-
for example::
374+
for example:
375+
376+
.. code-block:: pycon
375377
376378
>>> import sys
377379
>>> sys.last_traceback.tb_lineno

doc/en/warnings.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,21 @@ which works in a similar manner to :ref:`raises <assertraises>`:
279279
warnings.warn("my warning", UserWarning)
280280
281281
The test will fail if the warning in question is not raised. The keyword
282-
argument ``match`` to assert that the exception matches a text or regex::
282+
argument ``match`` to assert that the exception matches a text or regex:
283283

284-
>>> with warns(UserWarning, match='must be 0 or None'):
284+
.. code-block:: pycon
285+
286+
>>> with warns(UserWarning, match="must be 0 or None"):
285287
... warnings.warn("value must be 0 or None", UserWarning)
288+
...
286289
287-
>>> with warns(UserWarning, match=r'must be \d+$'):
290+
>>> with warns(UserWarning, match=r"must be \d+$"):
288291
... warnings.warn("value must be 42", UserWarning)
292+
...
289293
290-
>>> with warns(UserWarning, match=r'must be \d+$'):
294+
>>> with warns(UserWarning, match=r"must be \d+$"):
291295
... warnings.warn("this is not here", UserWarning)
296+
...
292297
Traceback (most recent call last):
293298
...
294299
Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted...

doc/en/writing_plugins.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ string value of ``Hello World!`` if we do not supply a value or ``Hello
369369
def _hello(name=None):
370370
if not name:
371371
name = request.config.getoption("name")
372-
return "Hello {name}!".format(name=name)
372+
return f"Hello {name}!"
373373
374374
return _hello
375375

0 commit comments

Comments
 (0)