Repository: ipython/ipykernel (language: python)

Issue:
TypeError: '>=' not supported between instances of 'str' and 'datetime.datetime'
There is a new downstream failure
```
TypeError: '>=' not supported between instances of 'str' and 'datetime.datetime'
````

For example:

https://github.com/ipython/ipython/actions/runs/8138250074/job/22238601371?pr=14360

_Originally posted by @Carreau in https://github.com/ipython/ipykernel/pull/1210#discussion_r1510889678_
            


Discussion / hints:
Copied from the linked, previously-merged, PR:

These are the same symptoms that I observed at https://github.com/ipython/ipykernel/pull/1210#discussion_r1487916907. `ipyparallel` unfortunately monkey-patches jupyter_clients conversion of strings to datetimes so it is a no-op, and some versions of `pytest-asyncio` don't fully isolate the tests so sometimes some of these start and end dates come through as strings.

In this PR updating `pytest-asyncio` to 0.23.5 worked, but this is evidently not sufficient as the IPython failure already uses `pytest-asyncio-0.23.5`.

I think the sensible approach here is to assume that in this test sometimes, outside of our direct control, the dates are strings and convert them to datetimes. Otherwise this test is not testing what it is supposed to, it is testing the types of some fields returned in messages. I'll submit a PR here ASAP.

Thanks, sorry I missed the earlier comment in the PR. 

If I might suggest, you may want to check if it's a string or a date and still emit a warning if it's a string so that upstream can slowly patch. It would be good to have reply messages always well typed. 

Fix patch:
diff --git a/pyproject.toml b/pyproject.toml
index 1bd260c2..3f91deb2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -54,7 +54,7 @@ docs = [
   "trio"
 ]
 test = [
-    "pytest>=7.0",
+    "pytest>=7.0,<8",
     "pytest-cov",
     "flaky",
     "ipyparallel",


Test patch:
diff --git a/tests/test_kernel.py b/tests/test_kernel.py
index a0bd8334..88f02ae9 100644
--- a/tests/test_kernel.py
+++ b/tests/test_kernel.py
@@ -620,20 +620,25 @@ def test_sequential_control_messages():
         # Get replies
         replies = [get_reply(kc, msg_id, channel="control") for msg_id in msg_ids]
 
+        def ensure_datetime(arg):
+            # Support arg which is a datetime or str.
+            if isinstance(arg, str):
+                if sys.version_info[:2] < (3, 11) and arg.endswith("Z"):
+                    # Python < 3.11 doesn't support "Z" suffix in datetime.fromisoformat,
+                    # so use alternative timezone format.
+                    # https://github.com/python/cpython/issues/80010
+                    arg = arg[:-1] + "+00:00"
+                return datetime.fromisoformat(arg)
+            return arg
+
         # Check messages are processed in order, one at a time, and of a sensible duration.
         previous_end = None
         for reply, sleep in zip(replies, sleeps):
-            start_str = reply["metadata"]["started"]
-            if sys.version_info[:2] < (3, 11) and start_str.endswith("Z"):
-                # Python < 3.11 doesn't support "Z" suffix in datetime.fromisoformat,
-                # so use alternative timezone format.
-                # https://github.com/python/cpython/issues/80010
-                start_str = start_str[:-1] + "+00:00"
-            start = datetime.fromisoformat(start_str)
-            end = reply["header"]["date"]  # Already a datetime
+            start = ensure_datetime(reply["metadata"]["started"])
+            end = ensure_datetime(reply["header"]["date"])
 
             if previous_end is not None:
-                assert start > previous_end
+                assert start >= previous_end
             previous_end = end
 
             assert end >= start + timedelta(seconds=sleep)