Skip to content

Commit c03c7a0

Browse files
committed
Refactor packer/unpacker change handling for improved readability
1 parent c92b9c7 commit c03c7a0

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
### Enhancements made
1010

1111
- Support psutil for finding network addresses [#1033](https://github.com/jupyter/jupyter_client/pull/1033) ([@juliangilbey](https://github.com/juliangilbey))
12-
- Add new functions `orjson_packer`, `orjson_unpacker`, `msgpack_packer` and `msgpack_unpacker`
1312

1413
### Bugs fixed
1514

jupyter_client/session.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,28 +393,21 @@ class Session(Configurable):
393393

394394
@observe("packer", "unpacker")
395395
def _packer_unpacker_changed(self, change: t.Any) -> None:
396-
new = change["new"]
397-
new_ = new.lower()
398-
if new_ == "orjson" and orjson:
399-
self.pack = orjson_packer
400-
self.unpack = orjson_unpacker
401-
self.packer = self.unpacker = new
402-
elif new_ in ["json", "orjson"]:
403-
self.pack = json_packer
404-
self.unpack = json_unpacker
405-
self.packer = self.unpacker = new
406-
elif new_ == "pickle":
407-
self.pack = pickle_packer
408-
self.unpack = pickle_unpacker
409-
self.packer = self.unpacker = new
410-
elif new_ == "msgpack":
411-
self.pack = msgpack_packer
412-
self.unpack = msgpack_unpacker
413-
self.packer = self.unpacker = new
396+
new = change["new"].lower()
397+
if new == "orjson" and orjson:
398+
self.pack, self.unpack = orjson_packer, orjson_unpacker
399+
elif new == "json" or new == "orjson":
400+
self.pack, self.unpack = json_packer, json_unpacker
401+
elif new == "pickle":
402+
self.pack, self.unpack = pickle_packer, pickle_unpacker
403+
elif new == "msgpack" and msgpack:
404+
self.pack, self.unpack = msgpack_packer, msgpack_unpacker
414405
else:
415-
obj = import_item(str(new))
406+
obj = import_item(str(change["new"]))
416407
name = "pack" if change["name"] == "packer" else "unpack"
417408
self.set_trait(name, obj)
409+
return
410+
self.packer = self.unpacker = change["new"]
418411

419412
session = CUnicode("", config=True, help="""The UUID identifying this session.""")
420413

tests/test_session.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,16 @@ def test_squash_unicode():
524524
assert ss.squash_unicode("hi") == b"hi"
525525

526526

527+
_sample_time = datetime(2021, 4, 1, 12, tzinfo=tzlocal())
528+
_sample_time_string = ss.json_default(_sample_time)
529+
530+
527531
@pytest.mark.parametrize(
528532
["description", "data"],
529533
[
530534
("dict", [{"a": 1}, [{"a": 1}]]),
531535
("infinite", [math.inf, ["inf", None]]),
532-
("datetime", [datetime(2021, 4, 1, 12, tzinfo=tzlocal()), ["2021-04-01T12:00:00+11:00"]]),
536+
("datetime", [_sample_time, [_sample_time_string]]),
533537
],
534538
)
535539
@pytest.mark.parametrize(["packer", "pack", "unpack"], serializers)

0 commit comments

Comments
 (0)