Skip to content

Commit 85f44b4

Browse files
committed
make repr more pythonic
1 parent d2c01ac commit 85f44b4

File tree

4 files changed

+53
-42
lines changed

4 files changed

+53
-42
lines changed

src/hyperframe/flags.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def __init__(self, defined_flags):
2323
self._valid_flags = set(flag.name for flag in defined_flags)
2424
self._flags = set()
2525

26+
def __repr__(self):
27+
return repr(sorted(list(self._flags)))
28+
2629
def __contains__(self, x):
2730
return self._flags.__contains__(x)
2831

src/hyperframe/frame.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,19 @@ def __init__(self, stream_id, flags=()):
7676
if (self.stream_id and
7777
self.stream_association == _STREAM_ASSOC_NO_STREAM):
7878
raise InvalidDataError(
79-
'Stream ID must be zero for {} with stream_id: {}'.format(
79+
'Stream ID must be zero for {} with stream_id={}'.format(
8080
type(self).__name__,
8181
self.stream_id,
8282
)
8383
)
8484

8585
def __repr__(self):
8686
return (
87-
"{}(stream_id: {}; flags: {}): {}"
87+
"{}(stream_id={}, flags={}): {}"
8888
).format(
8989
type(self).__name__,
9090
self.stream_id,
91-
", ".join(sorted(self.flags)) or "None",
91+
repr(self.flags),
9292
self._body_repr(),
9393
)
9494

@@ -350,7 +350,7 @@ class PriorityFrame(Priority, Frame):
350350
stream_association = _STREAM_ASSOC_HAS_STREAM
351351

352352
def _body_repr(self):
353-
return "exclusive: {}, depends_on: {}, stream_weight: {}".format(
353+
return "exclusive={}, depends_on={}, stream_weight={}".format(
354354
self.exclusive,
355355
self.depends_on,
356356
self.stream_weight
@@ -394,7 +394,7 @@ def __init__(self, stream_id, error_code=0, **kwargs):
394394
self.error_code = error_code
395395

396396
def _body_repr(self):
397-
return "error_code: {}".format(
397+
return "error_code={}".format(
398398
self.error_code,
399399
)
400400

@@ -465,7 +465,7 @@ def __init__(self, stream_id=0, settings=None, **kwargs):
465465
self.settings = settings or {}
466466

467467
def _body_repr(self):
468-
return "settings: {}".format(
468+
return "settings={}".format(
469469
self.settings,
470470
)
471471

@@ -520,7 +520,7 @@ def __init__(self, stream_id, promised_stream_id=0, data=b'', **kwargs):
520520
self.data = data
521521

522522
def _body_repr(self):
523-
return "promised_stream_id: {}, data: {}".format(
523+
return "promised_stream_id={}, data={}".format(
524524
self.promised_stream_id,
525525
_raw_data_repr(self.data),
526526
)
@@ -577,7 +577,7 @@ def __init__(self, stream_id=0, opaque_data=b'', **kwargs):
577577
self.opaque_data = opaque_data
578578

579579
def _body_repr(self):
580-
return "opaque_data: {}".format(
580+
return "opaque_data={}".format(
581581
self.opaque_data,
582582
)
583583

@@ -635,7 +635,7 @@ def __init__(self,
635635
self.additional_data = additional_data
636636

637637
def _body_repr(self):
638-
return "last_stream_id: {}, error_code: {}, additional_data: {}".format(
638+
return "last_stream_id={}, error_code={}, additional_data={}".format(
639639
self.last_stream_id,
640640
self.error_code,
641641
self.additional_data,
@@ -692,7 +692,7 @@ def __init__(self, stream_id, window_increment=0, **kwargs):
692692
self.window_increment = window_increment
693693

694694
def _body_repr(self):
695-
return "window_increment: {}".format(
695+
return "window_increment={}".format(
696696
self.window_increment,
697697
)
698698

@@ -751,7 +751,7 @@ def __init__(self, stream_id, data=b'', **kwargs):
751751
self.data = data
752752

753753
def _body_repr(self):
754-
return "exclusive: {}, depends_on: {}, stream_weight: {}, data: {}".format(
754+
return "exclusive={}, depends_on={}, stream_weight={}, data={}".format(
755755
self.exclusive,
756756
self.depends_on,
757757
self.stream_weight,
@@ -812,7 +812,7 @@ def __init__(self, stream_id, data=b'', **kwargs):
812812
self.data = data
813813

814814
def _body_repr(self):
815-
return "data: {}".format(
815+
return "data={}".format(
816816
_raw_data_repr(self.data),
817817
)
818818

@@ -854,7 +854,7 @@ def __init__(self, stream_id, origin=b'', field=b'', **kwargs):
854854
self.field = field
855855

856856
def _body_repr(self):
857-
return "origin: {}, field: {}".format(
857+
return "origin={}, field={}".format(
858858
self.origin,
859859
self.field,
860860
)
@@ -903,7 +903,7 @@ def __init__(self, type, stream_id, flag_byte=0x0, body=b'', **kwargs):
903903
self.body = body
904904

905905
def _body_repr(self):
906-
return "type: {}, flag_byte: {}, body: {}".format(
906+
return "type={}, flag_byte={}, body={}".format(
907907
self.type,
908908
self.flag_byte,
909909
_raw_data_repr(self.body),
@@ -947,7 +947,7 @@ def _raw_data_repr(data):
947947
r = binascii.hexlify(data).decode('ascii')
948948
if len(r) > 20:
949949
r = r[:20] + "..."
950-
return r
950+
return "<hex:" + r + ">"
951951

952952

953953
_FRAME_CLASSES = [

test/test_flags.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ def test_validation(self):
3333
flags.add("VALID_FLAG")
3434
with pytest.raises(ValueError):
3535
flags.add("INVALID_FLAG")
36+
37+
def test_repr(self):
38+
flags = Flags([Flag("VALID_FLAG", 0x00), Flag("OTHER_FLAG", 0x01)])
39+
assert repr(flags) == "[]"
40+
flags.add("VALID_FLAG")
41+
assert repr(flags) == "['VALID_FLAG']"
42+
flags.add("OTHER_FLAG")
43+
assert repr(flags) == "['OTHER_FLAG', 'VALID_FLAG']"

test/test_frames.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,25 @@ def test_repr(self, monkeypatch):
8989
f = Frame(0)
9090
monkeypatch.setattr(Frame, "serialize_body", lambda _: b"body")
9191
assert repr(f) == (
92-
"Frame(stream_id: 0; flags: None): 626f6479"
92+
"Frame(stream_id=0, flags=[]): <hex:626f6479>"
9393
)
9494

9595
f.stream_id = 42
9696
f.flags = ["END_STREAM", "PADDED"]
9797
assert repr(f) == (
98-
"Frame(stream_id: 42; flags: END_STREAM, PADDED): 626f6479"
98+
"Frame(stream_id=42, flags=['END_STREAM', 'PADDED']): <hex:626f6479>"
9999
)
100100

101101
monkeypatch.setattr(Frame, "serialize_body", lambda _: b"A"*25)
102102
assert repr(f) == (
103-
"Frame(stream_id: 42; flags: END_STREAM, PADDED): {}...".format("41"*10)
103+
"Frame(stream_id=42, flags=['END_STREAM', 'PADDED']): <hex:{}...>".format("41"*10)
104104
)
105105

106106
def test_frame_explain(self, capsys):
107107
d = b'\x00\x00\x08\x00\x01\x00\x00\x00\x01testdata'
108108
Frame.explain(memoryview(d))
109109
captured = capsys.readouterr()
110-
assert captured.out.strip() == "DataFrame(stream_id: 1; flags: END_STREAM): 7465737464617461"
110+
assert captured.out.strip() == "DataFrame(stream_id=1, flags=['END_STREAM']): <hex:7465737464617461>"
111111

112112
def test_cannot_parse_invalid_frame_header(self):
113113
with pytest.raises(InvalidFrameError):
@@ -122,7 +122,7 @@ class TestDataFrame:
122122

123123
def test_repr(self):
124124
f = DataFrame(1, b"testdata")
125-
assert repr(f).endswith("7465737464617461")
125+
assert repr(f).endswith("<hex:7465737464617461>")
126126

127127
def test_data_frame_has_correct_flags(self):
128128
f = DataFrame(1)
@@ -251,11 +251,11 @@ class TestPriorityFrame:
251251

252252
def test_repr(self):
253253
f = PriorityFrame(1)
254-
assert repr(f).endswith("exclusive: False, depends_on: 0, stream_weight: 0")
254+
assert repr(f).endswith("exclusive=False, depends_on=0, stream_weight=0")
255255
f.exclusive = True
256256
f.depends_on = 0x04
257257
f.stream_weight = 64
258-
assert repr(f).endswith("exclusive: True, depends_on: 4, stream_weight: 64")
258+
assert repr(f).endswith("exclusive=True, depends_on=4, stream_weight=64")
259259

260260
def test_priority_frame_has_no_flags(self):
261261
f = PriorityFrame(1)
@@ -306,9 +306,9 @@ def test_short_priority_frame_errors(self):
306306
class TestRstStreamFrame:
307307
def test_repr(self):
308308
f = RstStreamFrame(1)
309-
assert repr(f).endswith("error_code: 0")
309+
assert repr(f).endswith("error_code=0")
310310
f.error_code = 420
311-
assert repr(f).endswith("error_code: 420")
311+
assert repr(f).endswith("error_code=420")
312312

313313
def test_rst_stream_frame_has_no_flags(self):
314314
f = RstStreamFrame(1)
@@ -366,9 +366,9 @@ class TestSettingsFrame:
366366

367367
def test_repr(self):
368368
f = SettingsFrame()
369-
assert repr(f).endswith("settings: {}")
369+
assert repr(f).endswith("settings={}")
370370
f.settings[SettingsFrame.MAX_FRAME_SIZE] = 16384
371-
assert repr(f).endswith("settings: {5: 16384}")
371+
assert repr(f).endswith("settings={5: 16384}")
372372

373373
def test_settings_frame_has_only_one_flag(self):
374374
f = SettingsFrame()
@@ -431,10 +431,10 @@ def test_short_settings_frame_errors(self):
431431
class TestPushPromiseFrame:
432432
def test_repr(self):
433433
f = PushPromiseFrame(1)
434-
assert repr(f).endswith("promised_stream_id: 0, data: None")
434+
assert repr(f).endswith("promised_stream_id=0, data=None")
435435
f.promised_stream_id = 4
436436
f.data = b"testdata"
437-
assert repr(f).endswith("promised_stream_id: 4, data: 7465737464617461")
437+
assert repr(f).endswith("promised_stream_id=4, data=<hex:7465737464617461>")
438438

439439
def test_push_promise_frame_flags(self):
440440
f = PushPromiseFrame(1)
@@ -523,9 +523,9 @@ def test_short_push_promise_errors(self):
523523
class TestPingFrame:
524524
def test_repr(self):
525525
f = PingFrame()
526-
assert repr(f).endswith("opaque_data: b''")
526+
assert repr(f).endswith("opaque_data=b''")
527527
f.opaque_data = b'hello'
528-
assert repr(f).endswith("opaque_data: b'hello'")
528+
assert repr(f).endswith("opaque_data=b'hello'")
529529

530530
def test_ping_frame_has_only_one_flag(self):
531531
f = PingFrame()
@@ -581,11 +581,11 @@ def test_ping_frame_has_no_less_than_body_length_8(self):
581581
class TestGoAwayFrame:
582582
def test_repr(self):
583583
f = GoAwayFrame()
584-
assert repr(f).endswith("last_stream_id: 0, error_code: 0, additional_data: b''")
584+
assert repr(f).endswith("last_stream_id=0, error_code=0, additional_data=b''")
585585
f.last_stream_id = 64
586586
f.error_code = 32
587587
f.additional_data = b'hello'
588-
assert repr(f).endswith("last_stream_id: 64, error_code: 32, additional_data: b'hello'")
588+
assert repr(f).endswith("last_stream_id=64, error_code=32, additional_data=b'hello'")
589589

590590
def test_go_away_has_no_flags(self):
591591
f = GoAwayFrame()
@@ -652,10 +652,10 @@ def test_short_goaway_frame_errors(self):
652652
class TestWindowUpdateFrame:
653653
def test_repr(self):
654654
f = WindowUpdateFrame(0)
655-
assert repr(f).endswith("window_increment: 0")
655+
assert repr(f).endswith("window_increment=0")
656656
f.stream_id = 1
657657
f.window_increment = 512
658-
assert repr(f).endswith("window_increment: 512")
658+
assert repr(f).endswith("window_increment=512")
659659

660660
def test_window_update_has_no_flags(self):
661661
f = WindowUpdateFrame(0)
@@ -699,12 +699,12 @@ def test_short_windowupdate_frame_errors(self):
699699
class TestHeadersFrame:
700700
def test_repr(self):
701701
f = HeadersFrame(1)
702-
assert repr(f).endswith("exclusive: False, depends_on: 0, stream_weight: 0, data: None")
702+
assert repr(f).endswith("exclusive=False, depends_on=0, stream_weight=0, data=None")
703703
f.data = b'hello'
704704
f.exclusive = True
705705
f.depends_on = 42
706706
f.stream_weight = 64
707-
assert repr(f).endswith("exclusive: True, depends_on: 42, stream_weight: 64, data: 68656c6c6f")
707+
assert repr(f).endswith("exclusive=True, depends_on=42, stream_weight=64, data=<hex:68656c6c6f>")
708708

709709
def test_headers_frame_flags(self):
710710
f = HeadersFrame(1)
@@ -790,9 +790,9 @@ def test_headers_frame_with_no_length_parses(self):
790790
class TestContinuationFrame:
791791
def test_repr(self):
792792
f = ContinuationFrame(1)
793-
assert repr(f).endswith("data: None")
793+
assert repr(f).endswith("data=None")
794794
f.data = b'hello'
795-
assert repr(f).endswith("data: 68656c6c6f")
795+
assert repr(f).endswith("data=<hex:68656c6c6f>")
796796

797797
def test_continuation_frame_flags(self):
798798
f = ContinuationFrame(1)
@@ -852,11 +852,11 @@ class TestAltSvcFrame:
852852

853853
def test_repr(self):
854854
f = AltSvcFrame(0)
855-
assert repr(f).endswith("origin: b'', field: b''")
855+
assert repr(f).endswith("origin=b'', field=b''")
856856
f.field = b'h2="alt.example.com:8000", h2=":443"'
857-
assert repr(f).endswith("origin: b'', field: b'h2=\"alt.example.com:8000\", h2=\":443\"'")
857+
assert repr(f).endswith("origin=b'', field=b'h2=\"alt.example.com:8000\", h2=\":443\"'")
858858
f.origin = b'example.com'
859-
assert repr(f).endswith("origin: b'example.com', field: b'h2=\"alt.example.com:8000\", h2=\":443\"'")
859+
assert repr(f).endswith("origin=b'example.com', field=b'h2=\"alt.example.com:8000\", h2=\":443\"'")
860860

861861
def test_altsvc_frame_flags(self):
862862
f = AltSvcFrame(0)
@@ -927,4 +927,4 @@ def test_altsvc_with_unicode_field_fails(self):
927927
class TestExtensionFrame:
928928
def test_repr(self):
929929
f = ExtensionFrame(0xFF, 1, 42, b'hello')
930-
assert repr(f).endswith("type: 255, flag_byte: 42, body: 68656c6c6f")
930+
assert repr(f).endswith("type=255, flag_byte=42, body=<hex:68656c6c6f>")

0 commit comments

Comments
 (0)