Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 13, 2023
1 parent e7f6b98 commit 9e10722
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/pluggy/_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"""
from __future__ import annotations

import reprlib
from typing import Any
from typing import Callable
from typing import Sequence
from typing import Tuple
import reprlib


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


def _try_repr_or_str(obj: object) -> str:
try:
return repr(obj)
Expand All @@ -68,6 +69,7 @@ def _try_repr_or_str(obj: object) -> str:
except BaseException:
return f'{type(obj).__name__}("{obj}")'


Check warning on line 72 in src/pluggy/_tracing.py

View check run for this annotation

Codecov / codecov/patch

src/pluggy/_tracing.py#L71-L72

Added lines #L71 - L72 were not covered by tests
def _format_repr_exception(exc: BaseException, obj: object) -> str:
try:
exc_info = _try_repr_or_str(exc)
Expand All @@ -79,13 +81,15 @@ def _format_repr_exception(exc: BaseException, obj: object) -> str:
exc_info, type(obj).__name__, id(obj)
)


def _ellipsize(s: str, maxsize: int) -> str:
if len(s) > maxsize:
i = max(0, (maxsize - 3) // 2)
j = max(0, maxsize - 3 - i)
return s[:i] + "..." + s[len(s) - j :]
return s


class SafeRepr(reprlib.Repr):
"""
repr.Repr that limits the resulting size of repr() and includes
Expand Down Expand Up @@ -136,6 +140,8 @@ def repr_instance(self, x: object, level: int) -> str:

# Maximum size of overall repr of objects to display during assertion errors.
DEFAULT_REPR_MAX_SIZE = 240


def saferepr(
obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE, use_ascii: bool = False
) -> str:
Expand All @@ -151,6 +157,7 @@ def saferepr(

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


class TagTracerSub:
def __init__(self, root: TagTracer, tags: tuple[str, ...]) -> None:
self.root = root
Expand Down
3 changes: 1 addition & 2 deletions testing/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
"""
import pytest

from ._tracing import saferepr
from pluggy import HookimplMarker
from pluggy import HookspecMarker
from pluggy import PluginManager
from pluggy._callers import _multicall
from pluggy._hooks import HookImpl

from ._tracing import saferepr


hookspec = HookspecMarker("example")
hookimpl = HookimplMarker("example")
Expand Down
7 changes: 5 additions & 2 deletions testing/test_hookcaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ def conflict(self) -> None:
"<class 'test_hookcaller.test_hook_conflict.<locals>.Api1'>"
)

def test_hookcaller_repr_with_saferepr_failure(hc: HookCaller, addmeth: AddMeth) -> None:

def test_hookcaller_repr_with_saferepr_failure(
hc: HookCaller, addmeth: AddMeth
) -> None:
@addmeth()
def he_method1() -> None:
pass

Check warning on line 459 in testing/test_hookcaller.py

View check run for this annotation

Codecov / codecov/patch

testing/test_hookcaller.py#L459

Added line #L459 was not covered by tests
Expand All @@ -466,4 +469,4 @@ def he_method3() -> None:

# Verify that HookCaller.repr with saferepr still works despite the error
expected_repr = f"<HookCaller {saferepr(hc.name)}>"
assert repr(hc) == expected_repr
assert repr(hc) == expected_repr
6 changes: 4 additions & 2 deletions testing/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest

from pluggy._tracing import saferepr, DEFAULT_REPR_MAX_SIZE
from pluggy._tracing import DEFAULT_REPR_MAX_SIZE
from pluggy._tracing import saferepr
from pluggy._tracing import TagTracer


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


def test_saferepr_simple_repr():
assert saferepr(1) == "1"
assert saferepr(None) == "None"
Expand Down Expand Up @@ -247,4 +249,4 @@ def __repr__(self):

assert saferepr(SomeClass()).startswith(
"<[RuntimeError() raised in repr()] SomeClass object at 0x"
)
)

0 comments on commit 9e10722

Please sign in to comment.