Skip to content

Commit

Permalink
deprecate extracting dates in message headers
Browse files Browse the repository at this point in the history
adds Session.extract_header_dates config
and JUPYTER_SESSION_EXTRACT_HEADER_DATES env

default unchanged, but deprecated
  • Loading branch information
minrk committed Mar 10, 2024
1 parent a66bd11 commit 0229711
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions jupyter_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
Set,
TraitError,
Unicode,
default,
observe,
)
from traitlets.config.configurable import Configurable, LoggingConfigurable
Expand Down Expand Up @@ -420,6 +421,38 @@ def _session_changed(self, change: t.Any) -> None:
"message.",
)

extract_header_dates = Bool(
True,
config=True,
help="""
Parse timestamps in message headers to datetime objects.
If True, `date` and other timestamp fields
will be `datetime.datetime` objects.
If False, they will be ISO8601 strings.
Parsing has a performance cost and is deprecated,
but kept as default for backward compatibility.
""",
)

@default("extract_header_dates")
def _extract_header_dates_default(self):
msg = """
Session.extract_header_dates = True is deprecated in jupyter-client 8.6
set cfg.Session.extract_header_dates = False
or JUPYTER_SESSION_EXTRACT_HEADER_DATES=0
to avoid this message.
"""
env_value = os.environ.get("JUPYTER_SESSION_EXTRACT_HEADER_DATES", "") != "0"
if env_value:
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return True
else:
return False

# if 0, no adapting to do.
adapt_version = Integer(0)

Expand Down Expand Up @@ -1076,10 +1109,14 @@ def deserialize(
msg = "malformed message, must have at least %i elements" % minlen
raise TypeError(msg)
header = self.unpack(msg_list[1])
message["header"] = extract_dates(header)
parent_header = self.unpack(msg_list[2])
if self.extract_header_dates:
header = extract_dates(header)
parent_header = extract_dates(parent_header)
message["header"] = header
message["msg_id"] = header["msg_id"]
message["msg_type"] = header["msg_type"]
message["parent_header"] = extract_dates(self.unpack(msg_list[2]))
message["parent_header"] = parent_header
message["metadata"] = self.unpack(msg_list[3])
if content:
message["content"] = self.unpack(msg_list[4])
Expand Down

0 comments on commit 0229711

Please sign in to comment.