Skip to content

Commit 5fa1164

Browse files
Anusha ShekharAnusha Shekhar
authored andcommitted
linting
1 parent e7f6b98 commit 5fa1164

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

src/pluggy/_hooks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,10 @@ def __init__(
668668
self.trylast: Final = hook_impl_opts["trylast"]
669669

670670
def __repr__(self) -> str:
671-
return f"<HookImpl plugin_name={saferepr(self.plugin_name)}, plugin={saferepr(self.plugin)}>"
671+
return (
672+
f"<HookImpl plugin_name={saferepr(self.plugin_name)}, "
673+
f"plugin={saferepr(self.plugin)}>"
674+
)
672675

673676

674677
@final

src/pluggy/_tracing.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"""
44
from __future__ import annotations
55

6+
import reprlib
67
from typing import Any
78
from typing import Callable
89
from typing import Sequence
910
from typing import Tuple
10-
import reprlib
1111

1212

1313
_Writer = Callable[[str], object]
@@ -60,6 +60,7 @@ def setprocessor(self, tags: str | tuple[str, ...], processor: _Processor) -> No
6060
assert isinstance(tags, tuple)
6161
self._tags2proc[tags] = processor
6262

63+
6364
def _try_repr_or_str(obj: object) -> str:
6465
try:
6566
return repr(obj)
@@ -68,6 +69,7 @@ def _try_repr_or_str(obj: object) -> str:
6869
except BaseException:
6970
return f'{type(obj).__name__}("{obj}")'
7071

72+
7173
def _format_repr_exception(exc: BaseException, obj: object) -> str:
7274
try:
7375
exc_info = _try_repr_or_str(exc)
@@ -79,20 +81,24 @@ def _format_repr_exception(exc: BaseException, obj: object) -> str:
7981
exc_info, type(obj).__name__, id(obj)
8082
)
8183

84+
8285
def _ellipsize(s: str, maxsize: int) -> str:
8386
if len(s) > maxsize:
8487
i = max(0, (maxsize - 3) // 2)
8588
j = max(0, maxsize - 3 - i)
86-
return s[:i] + "..." + s[len(s) - j :]
89+
90+
x = len(s) - j
91+
return s[:i] + "..." + s[x:]
8792
return s
8893

94+
8995
class SafeRepr(reprlib.Repr):
9096
"""
9197
repr.Repr that limits the resulting size of repr() and includes
9298
information on exceptions raised during the call.
9399
"""
94100

95-
def __init__(self, maxsize: Optional[int], use_ascii: bool = False) -> None:
101+
def __init__(self, maxsize: int | None, use_ascii: bool = False) -> None:
96102
"""
97103
:param maxsize:
98104
If not None, will truncate the resulting repr to that specific size, using ellipsis
@@ -136,8 +142,10 @@ def repr_instance(self, x: object, level: int) -> str:
136142

137143
# Maximum size of overall repr of objects to display during assertion errors.
138144
DEFAULT_REPR_MAX_SIZE = 240
145+
146+
139147
def saferepr(
140-
obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE, use_ascii: bool = False
148+
obj: object, maxsize: int | None = DEFAULT_REPR_MAX_SIZE, use_ascii: bool = False
141149
) -> str:
142150
"""Return a size-limited safe repr-string for the given object.
143151
@@ -151,6 +159,7 @@ def saferepr(
151159

152160
return SafeRepr(maxsize, use_ascii).repr(obj)
153161

162+
154163
class TagTracerSub:
155164
def __init__(self, root: TagTracer, tags: tuple[str, ...]) -> None:
156165
self.root = root

testing/benchmark.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
"""
44
import pytest
55

6+
from ._tracing import saferepr
67
from pluggy import HookimplMarker
78
from pluggy import HookspecMarker
89
from pluggy import PluginManager
910
from pluggy._callers import _multicall
1011
from pluggy._hooks import HookImpl
1112

12-
from ._tracing import saferepr
13-
1413

1514
hookspec = HookspecMarker("example")
1615
hookimpl = HookimplMarker("example")

testing/test_hookcaller.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -450,20 +450,15 @@ def conflict(self) -> None:
450450
"<class 'test_hookcaller.test_hook_conflict.<locals>.Api1'>"
451451
)
452452

453-
def test_hookcaller_repr_with_saferepr_failure(hc: HookCaller, addmeth: AddMeth) -> None:
454-
@addmeth()
455-
def he_method1() -> None:
456-
pass
457453

454+
def test_hookcaller_repr_with_saferepr_failure(
455+
hc: HookCaller, addmeth: AddMeth
456+
) -> None:
458457
@addmeth()
459458
def he_method2() -> None:
460459
# Intentional error to make the repr fail
461460
raise ValueError("Intentional error in he_method2")
462461

463-
@addmeth()
464-
def he_method3() -> None:
465-
pass
466-
467462
# Verify that HookCaller.repr with saferepr still works despite the error
468463
expected_repr = f"<HookCaller {saferepr(hc.name)}>"
469-
assert repr(hc) == expected_repr
464+
assert repr(hc) == expected_repr

testing/test_tracer.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import pytest
44

5-
from pluggy._tracing import saferepr, DEFAULT_REPR_MAX_SIZE
5+
from pluggy._tracing import DEFAULT_REPR_MAX_SIZE
6+
from pluggy._tracing import saferepr
67
from pluggy._tracing import TagTracer
78

89

@@ -79,6 +80,7 @@ def test_setprocessor(rootlogger: TagTracer) -> None:
7980
tags, args = l2[0]
8081
assert args == ("seen",)
8182

83+
8284
def test_saferepr_simple_repr():
8385
assert saferepr(1) == "1"
8486
assert saferepr(None) == "None"
@@ -110,10 +112,10 @@ def __repr__(self):
110112

111113
def test_saferepr_exceptions() -> None:
112114
class BrokenRepr:
113-
def __init__(self, ex):
115+
def __init__(self, ex) -> None:
114116
self.ex = ex
115117

116-
def __repr__(self):
118+
def __repr__(self) -> str:
117119
raise self.ex
118120

119121
class BrokenReprException(Exception):
@@ -143,10 +145,10 @@ def test_saferepr_baseexception():
143145
"""Test saferepr() with BaseExceptions, which includes pytest outcomes."""
144146

145147
class RaisingOnStrRepr(BaseException):
146-
def __init__(self, exc_types):
148+
def __init__(self, exc_types) -> None:
147149
self.exc_types = exc_types
148150

149-
def raise_exc(self, *args):
151+
def raise_exc(self, *args) -> None:
150152
try:
151153
self.exc_type = self.exc_types.pop(0)
152154
except IndexError:
@@ -155,17 +157,19 @@ def raise_exc(self, *args):
155157
raise self.exc_type(*args)
156158
raise self.exc_type
157159

158-
def __str__(self):
160+
def __str__(self) -> str:
159161
self.raise_exc("__str__")
162+
return ""
160163

161-
def __repr__(self):
164+
def __repr__(self) -> str:
162165
self.raise_exc("__repr__")
166+
return ""
163167

164168
class BrokenObj:
165-
def __init__(self, exc):
169+
def __init__(self, exc) -> None:
166170
self.exc = exc
167171

168-
def __repr__(self):
172+
def __repr__(self) -> str:
169173
raise self.exc
170174

171175
__str__ = __repr__
@@ -247,4 +251,4 @@ def __repr__(self):
247251

248252
assert saferepr(SomeClass()).startswith(
249253
"<[RuntimeError() raised in repr()] SomeClass object at 0x"
250-
)
254+
)

0 commit comments

Comments
 (0)