Skip to content

Commit e087e3f

Browse files
Merge pull request #13009 from pytest-dev/pre-commit-ci-update-config
Upgrade ruff to 0.8.1 and fix existing issues
2 parents d65d991 + 17c5bbb commit e087e3f

30 files changed

+112
-136
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: "v0.7.4"
3+
rev: "v0.8.1"
44
hooks:
55
- id: ruff
66
args: ["--fix"]

bench/empty.py

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

33

44
for i in range(1000):
5-
exec("def test_func_%d(): pass" % i)
5+
exec(f"def test_func_{i}(): pass")

src/_pytest/_code/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
__all__ = [
1717
"Code",
1818
"ExceptionInfo",
19-
"filter_traceback",
2019
"Frame",
21-
"getfslineno",
22-
"getrawcode",
20+
"Source",
2321
"Traceback",
2422
"TracebackEntry",
25-
"Source",
23+
"filter_traceback",
24+
"getfslineno",
25+
"getrawcode",
2626
]

src/_pytest/_code/code.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def relline(self) -> int:
217217
return self.lineno - self.frame.code.firstlineno
218218

219219
def __repr__(self) -> str:
220-
return "<TracebackEntry %s:%d>" % (self.frame.code.path, self.lineno + 1)
220+
return f"<TracebackEntry {self.frame.code.path}:{self.lineno+1}>"
221221

222222
@property
223223
def statement(self) -> Source:
@@ -303,12 +303,7 @@ def __str__(self) -> str:
303303
# This output does not quite match Python's repr for traceback entries,
304304
# but changing it to do so would break certain plugins. See
305305
# https://github.com/pytest-dev/pytest/pull/7535/ for details.
306-
return " File %r:%d in %s\n %s\n" % (
307-
str(self.path),
308-
self.lineno + 1,
309-
name,
310-
line,
311-
)
306+
return f" File '{self.path}':{self.lineno+1} in {name}\n {line}\n"
312307

313308
@property
314309
def name(self) -> str:
@@ -953,7 +948,7 @@ def repr_traceback_entry(
953948
if short:
954949
message = f"in {entry.name}"
955950
else:
956-
message = excinfo and excinfo.typename or ""
951+
message = (excinfo and excinfo.typename) or ""
957952
entry_path = entry.path
958953
path = self._makepath(entry_path)
959954
reprfileloc = ReprFileLocation(path, entry.lineno + 1, message)
@@ -1182,10 +1177,8 @@ def toterminal(self, tw: TerminalWriter) -> None:
11821177
entry.toterminal(tw)
11831178
if i < len(self.reprentries) - 1:
11841179
next_entry = self.reprentries[i + 1]
1185-
if (
1186-
entry.style == "long"
1187-
or entry.style == "short"
1188-
and next_entry.style == "long"
1180+
if entry.style == "long" or (
1181+
entry.style == "short" and next_entry.style == "long"
11891182
):
11901183
tw.sep(self.entrysep)
11911184

@@ -1369,7 +1362,7 @@ def getfslineno(obj: object) -> tuple[str | Path, int]:
13691362
except TypeError:
13701363
return "", -1
13711364

1372-
fspath = fn and absolutepath(fn) or ""
1365+
fspath = (fn and absolutepath(fn)) or ""
13731366
lineno = -1
13741367
if fspath:
13751368
try:

src/_pytest/_io/pprint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def _pprint_deque(
540540
) -> None:
541541
stream.write(object.__class__.__name__ + "(")
542542
if object.maxlen is not None:
543-
stream.write("maxlen=%d, " % object.maxlen)
543+
stream.write(f"maxlen={object.maxlen}, ")
544544
stream.write("[")
545545

546546
self._format_items(object, stream, indent, allowance + 1, context, level)

src/_pytest/_py/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _geterrnoclass(self, eno: int) -> type[Error]:
6969
try:
7070
return self._errno2class[eno]
7171
except KeyError:
72-
clsname = errno.errorcode.get(eno, "UnknownErrno%d" % (eno,))
72+
clsname = errno.errorcode.get(eno, f"UnknownErrno{eno}")
7373
errorcls = type(
7474
clsname,
7575
(Error,),

src/_pytest/assertion/rewrite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Sentinel:
5353

5454
# pytest caches rewritten pycs in pycache dirs
5555
PYTEST_TAG = f"{sys.implementation.cache_tag}-pytest-{version}"
56-
PYC_EXT = ".py" + (__debug__ and "c" or "o")
56+
PYC_EXT = ".py" + ((__debug__ and "c") or "o")
5757
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
5858

5959
# Special marker that denotes we have just left a scope definition
@@ -481,7 +481,7 @@ def _should_repr_global_name(obj: object) -> bool:
481481

482482

483483
def _format_boolop(explanations: Iterable[str], is_or: bool) -> str:
484-
explanation = "(" + (is_or and " or " or " and ").join(explanations) + ")"
484+
explanation = "(" + ((is_or and " or ") or " and ").join(explanations) + ")"
485485
return explanation.replace("%", "%%")
486486

487487

src/_pytest/assertion/util.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ def _compare_eq_sequence(
406406
]
407407
else:
408408
explanation += [
409-
"%s contains %d more items, first extra item: %s"
410-
% (dir_with_more, len_diff, highlighter(extra))
409+
f"{dir_with_more} contains {len_diff} more items, first extra item: {highlighter(extra)}"
411410
]
412411
return explanation
413412

@@ -510,8 +509,7 @@ def _compare_eq_dict(
510509
len_extra_left = len(extra_left)
511510
if len_extra_left:
512511
explanation.append(
513-
"Left contains %d more item%s:"
514-
% (len_extra_left, "" if len_extra_left == 1 else "s")
512+
f"Left contains {len_extra_left} more item{'' if len_extra_left == 1 else 's'}:"
515513
)
516514
explanation.extend(
517515
highlighter(pprint.pformat({k: left[k] for k in extra_left})).splitlines()
@@ -520,8 +518,7 @@ def _compare_eq_dict(
520518
len_extra_right = len(extra_right)
521519
if len_extra_right:
522520
explanation.append(
523-
"Right contains %d more item%s:"
524-
% (len_extra_right, "" if len_extra_right == 1 else "s")
521+
f"Right contains {len_extra_right} more item{'' if len_extra_right == 1 else 's'}:"
525522
)
526523
explanation.extend(
527524
highlighter(pprint.pformat({k: right[k] for k in extra_right})).splitlines()

src/_pytest/cacheprovider.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ def pytest_collection_modifyitems(
388388
if not previously_failed:
389389
# Running a subset of all tests with recorded failures
390390
# only outside of it.
391-
self._report_status = "%d known failures not in selected tests" % (
392-
len(self.lastfailed),
391+
self._report_status = (
392+
f"{len(self.lastfailed)} known failures not in selected tests"
393393
)
394394
else:
395395
if self.config.getoption("lf"):
@@ -622,5 +622,5 @@ def cacheshow(config: Config, session: Session) -> int:
622622
# print("%s/" % p.relative_to(basedir))
623623
if p.is_file():
624624
key = str(p.relative_to(basedir))
625-
tw.line(f"{key} is a file of length {p.stat().st_size:d}")
625+
tw.line(f"{key} is a file of length {p.stat().st_size}")
626626
return 0

src/_pytest/capture.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def repr(self, class_name: str) -> str:
360360
return "<{} {} _old={} _state={!r} tmpfile={!r}>".format(
361361
class_name,
362362
self.name,
363-
hasattr(self, "_old") and repr(self._old) or "<UNSET>",
363+
(hasattr(self, "_old") and repr(self._old)) or "<UNSET>",
364364
self._state,
365365
self.tmpfile,
366366
)
@@ -369,7 +369,7 @@ def __repr__(self) -> str:
369369
return "<{} {} _old={} _state={!r} tmpfile={!r}>".format(
370370
self.__class__.__name__,
371371
self.name,
372-
hasattr(self, "_old") and repr(self._old) or "<UNSET>",
372+
(hasattr(self, "_old") and repr(self._old)) or "<UNSET>",
373373
self._state,
374374
self.tmpfile,
375375
)

0 commit comments

Comments
 (0)