shunping commented on code in PR #34310:
URL: https://github.com/apache/beam/pull/34310#discussion_r1998972432


##########
sdks/python/apache_beam/ml/anomaly/specifiable.py:
##########
@@ -104,33 +105,47 @@ class Spec():
   config: Optional[Dict[str, Any]] = dataclasses.field(default_factory=dict)
 
 
-@runtime_checkable
-class Specifiable(Protocol):
-  """Protocol that a specifiable class needs to implement."""
-  #: The value of the `type` field in the object's spec for this class.
-  spec_type: ClassVar[str]
-  #: The raw keyword arguments passed to `__init__` method during object
-  #: initialization.
-  init_kwargs: dict[str, Any]
+def _from_spec_helper(v, _run_init):
+  if isinstance(v, Spec):
+    return Specifiable.from_spec(v, _run_init)
+
+  if isinstance(v, List):
+    return [_from_spec_helper(e, _run_init) for e in v]
+
+  return v
+
+
+def _to_spec_helper(v):
+  if isinstance(v, Specifiable):
+    return v.to_spec()
+
+  if isinstance(v, List):
+    return [_to_spec_helper(e) for e in v]
+
+  if inspect.isfunction(v):
+    if not hasattr(v, "spec_type"):
+      _register(v, inject_spec_type=False)
+    return Spec(type=_get_default_spec_type(v), config=None)
 
-  # a boolean to tell whether the original `__init__` method is called
-  _initialized: bool
-  # a boolean used by new_getattr to tell whether it is in the `__init__` 
method
-  # call
-  _in_init: bool
+  if inspect.isclass(v):
+    if not hasattr(v, "spec_type"):
+      _register(v, inject_spec_type=False)
+    return Spec(type=_get_default_spec_type(v), config=None)
 
-  @staticmethod
-  def _from_spec_helper(v, _run_init):
-    if isinstance(v, Spec):
-      return Specifiable.from_spec(v, _run_init)
+  return v

Review Comment:
   Right, we only have limited support on the types we support spec treatment.
   If it is a dict, we will simply return it as it is. 
   
   We can add more, but it could be a work scope for a separate PR.



##########
sdks/python/apache_beam/ml/anomaly/specifiable.py:
##########
@@ -104,33 +105,47 @@ class Spec():
   config: Optional[Dict[str, Any]] = dataclasses.field(default_factory=dict)
 
 
-@runtime_checkable
-class Specifiable(Protocol):
-  """Protocol that a specifiable class needs to implement."""
-  #: The value of the `type` field in the object's spec for this class.
-  spec_type: ClassVar[str]
-  #: The raw keyword arguments passed to `__init__` method during object
-  #: initialization.
-  init_kwargs: dict[str, Any]
+def _from_spec_helper(v, _run_init):

Review Comment:
   Agree



-- 
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