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


##########
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:
   Note that this function will be called for both builtin Python types (e.g. 
int, float, str) as well as user defined types. We can check if the type of the 
instance is a builtin type and only raise warnings if it is not.



-- 
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: github-unsubscr...@beam.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to