gemini-code-assist[bot] commented on code in PR #38236:
URL: https://github.com/apache/beam/pull/38236#discussion_r3119370048


##########
sdks/python/apache_beam/yaml/yaml_mapping.py:
##########
@@ -16,8 +16,12 @@
 #
 
 """This module defines the basic MapToFields operation."""
+
+import datetime
 import itertools
 import re
+import threading
+import uuid
 from collections import abc
 from collections.abc import Callable
 from collections.abc import Collection

Review Comment:
   ![critical](https://www.gstatic.com/codereviewagent/critical.svg)
   
   The code in `js_to_py` uses `Mapping` and `Iterable`, but these are not 
imported into the global namespace. They should be imported from 
`collections.abc` to avoid a `NameError` at runtime.
   
   ```suggestion
   from collections import abc
   from collections.abc import Callable
   from collections.abc import Collection
   from collections.abc import Iterable
   from collections.abc import Mapping
   ```



##########
sdks/python/apache_beam/yaml/yaml_mapping.py:
##########
@@ -205,83 +211,78 @@ def py_value_to_js_dict(py_value):
     return py_value
 
 
+def js_to_py(obj):
+  """Converts mini-racer mapped objects to standard Python types.
+  
+  This is needed because ctx.eval returns objects that implement Mapping
+  and Iterable but are not picklable (like JSMappedObjectImpl and JSArrayImpl),
+  which would fail when Beam tries to serialize rows containing them.
+  We also preserve datetime objects which are correctly produced by ctx.eval
+  for JS Date objects.
+  """
+  if isinstance(obj, datetime.datetime):
+    return obj
+  elif isinstance(obj, Mapping):
+    return {k: js_to_py(v) for k, v in obj.items()}
+  elif not isinstance(obj, (str, bytes)) and isinstance(obj, Iterable):
+    return [js_to_py(v) for v in obj]
+  elif isinstance(obj, str):
+    try:
+      if obj.endswith('Z'):
+        return datetime.datetime.fromisoformat(obj[:-1] + '+00:00')
+      return datetime.datetime.fromisoformat(obj)
+    except ValueError:
+      return obj

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   This string-to-datetime conversion is very aggressive as it attempts to 
convert *any* string that matches the ISO format into a `datetime` object. This 
can lead to unexpected data type changes if a user intended to return a string 
that happens to look like a date. Since `mini-racer` (V8) typically returns 
`datetime.datetime` objects directly for JavaScript `Date` objects when using 
`ctx.call`, this manual parsing might be unnecessary or should be more targeted.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to