Skip to content

Commit 23207fd

Browse files
authored
Improve repr string representation of GqlStatusObject (#1234)
1 parent a3cc646 commit 23207fd

File tree

4 files changed

+84
-46
lines changed

4 files changed

+84
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
197197
- `SummaryCounters`: `repr` and `str` to conform with Python's recommendations.
198198
- `Point` and its subclasses: `repr` to conform with Python's recommendations.
199199
- `Duration` and `ClockTime`: `repr` to be more consistent with other temporal driver types.
200+
- `GqlStatusObject`: `repr` to conform with Python's recommendations.
201+
- `SummaryInputPosition`: `repr` to be more informative.
200202

201203

202204
## Version 5.28

src/neo4j/_work/gql_status.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/neo4j/_work/summary.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,15 @@ def __str__(self) -> str:
495495
f"line: {self.line}, column: {self.column}, offset: {self.offset}"
496496
)
497497

498+
def __repr__(self) -> str:
499+
return (
500+
f"<{self.__class__.__name__} "
501+
f"line={self.line!r}, "
502+
f"column={self.column!r}, "
503+
f"offset={self.offset!r}"
504+
">"
505+
)
506+
498507

499508
# Deprecated alias for :class:`.SummaryInputPosition`.
500509
#
@@ -744,7 +753,7 @@ def __str__(self) -> str:
744753

745754
def __repr__(self) -> str:
746755
return (
747-
"GqlStatusObject("
756+
f"<{self.__class__.__name__} "
748757
f"gql_status={self.gql_status!r}, "
749758
f"status_description={self.status_description!r}, "
750759
f"position={self.position!r}, "
@@ -753,7 +762,7 @@ def __repr__(self) -> str:
753762
f"raw_severity={self.raw_severity!r}, "
754763
f"severity={self.severity!r}, "
755764
f"diagnostic_record={self.diagnostic_record!r}"
756-
")"
765+
">"
757766
)
758767

759768
@property

tests/unit/common/work/test_summary.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
SummaryCounters,
3838
SummaryInputPosition,
3939
)
40+
from neo4j._work.summary import (
41+
_CLASSIFICATION_LOOKUP,
42+
_SEVERITY_LOOKUP,
43+
)
4044

4145
from ...._deprecated_imports import (
4246
NotificationCategory,
@@ -388,6 +392,73 @@ def test_non_notification_statuses(raw_status, summary_args_kwargs) -> None:
388392
assert_is_non_notification_status(status)
389393

390394

395+
@pytest.mark.parametrize(
396+
"raw_status",
397+
(
398+
STATUS_SUCCESS,
399+
STATUS_OMITTED_RESULT,
400+
STATUS_NO_DATA,
401+
StatusOrderHelper.make_raw_status(0, "SUCCESS"),
402+
StatusOrderHelper.make_raw_status(0, "OMITTED"),
403+
StatusOrderHelper.make_raw_status(0, "NODATA"),
404+
StatusOrderHelper.make_raw_status(0, "WARNING"),
405+
StatusOrderHelper.make_raw_status(0, "INFORMATION"),
406+
),
407+
)
408+
def test_status_order_helper_repr(raw_status, summary_args_kwargs) -> None:
409+
args, kwargs = summary_args_kwargs
410+
kwargs["metadata"]["statuses"] = [raw_status]
411+
412+
expected_status = raw_status["gql_status"]
413+
expected_description = raw_status["status_description"]
414+
expected_diag_record = raw_status.get("diagnostic_record", {})
415+
expected_position = SummaryInputPosition._from_metadata(
416+
expected_diag_record.get("_position")
417+
)
418+
expected_raw_cls = expected_diag_record.get("_classification")
419+
expected_cls = _CLASSIFICATION_LOOKUP.get(
420+
expected_raw_cls, NotificationClassification.UNKNOWN
421+
)
422+
expected_raw_sev = expected_diag_record.get("_severity")
423+
expected_sev = _SEVERITY_LOOKUP.get(
424+
expected_raw_sev, NotificationSeverity.UNKNOWN
425+
)
426+
427+
expected = (
428+
"<GqlStatusObject "
429+
f"gql_status={expected_status!r}, "
430+
f"status_description={expected_description!r}, "
431+
f"position={expected_position!r}, "
432+
f"raw_classification={expected_raw_cls!r}, "
433+
f"classification={expected_cls!r}, "
434+
f"raw_severity={expected_raw_sev!r}, "
435+
f"severity={expected_sev!r}, "
436+
f"diagnostic_record={expected_diag_record!r}"
437+
">"
438+
)
439+
440+
summary = ResultSummary(*args, **kwargs)
441+
status_objects: t.Sequence[GqlStatusObject] = summary.gql_status_objects
442+
443+
assert len(status_objects) == 1
444+
status = status_objects[0]
445+
446+
assert repr(status) == expected
447+
448+
449+
def test_summary_input_position_repr():
450+
position = SummaryInputPosition._from_metadata(
451+
{
452+
"line": 42,
453+
"column": 1337,
454+
"offset": 666,
455+
}
456+
)
457+
expected = "<SummaryInputPosition line=42, column=1337, offset=666>"
458+
459+
assert repr(position) == expected
460+
461+
391462
@pytest.mark.parametrize(
392463
"types",
393464
(

0 commit comments

Comments
 (0)