{"record":{"id":"72e7704caa6c40a9","repo":"apache/beam","slug":"obj-to-invoke-has-to-be-either-a-dofn-or-a","errorCode":null,"errorMessage":"'obj_to_invoke' has to be either a 'DoFn' or a 'RestrictionProvider'. Received %r instead.","messagePattern":"'obj_to_invoke' has to be either a 'DoFn' or a 'RestrictionProvider'\\. Received %r instead\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/runners/common.py","lineNumber":153,"sourceCode":"\n\nclass MethodWrapper(object):\n  \"\"\"For internal use only; no backwards-compatibility guarantees.\n\n  Represents a method that can be invoked by `DoFnInvoker`.\"\"\"\n  def __init__(self, obj_to_invoke, method_name):\n    \"\"\"\n    Initiates a ``MethodWrapper``.\n\n    Args:\n      obj_to_invoke: the object that contains the method. Has to either be a\n                    `DoFn` object or a `RestrictionProvider` object.\n      method_name: name of the method as a string.\n    \"\"\"\n\n    if not isinstance(obj_to_invoke,\n                      (DoFn, RestrictionProvider, WatermarkEstimatorProvider)):\n      raise ValueError(\n          '\\'obj_to_invoke\\' has to be either a \\'DoFn\\' or '\n          'a \\'RestrictionProvider\\'. Received %r instead.' % obj_to_invoke)\n\n    self.args, self.defaults = core.get_function_arguments(obj_to_invoke,\n                                                           method_name)\n    # TODO(BEAM-5878) support kwonlyargs on Python 3.\n    self.method_value = getattr(obj_to_invoke, method_name)\n    self.method_name = method_name\n\n    self.has_userstate_arguments = False\n    self.state_args_to_replace = {}  # type: Dict[str, core.StateSpec]\n    self.timer_args_to_replace = {}  # type: Dict[str, core.TimerSpec]\n    self.timestamp_arg_name = None  # type: Optional[str]\n    self.window_arg_name = None  # type: Optional[str]\n    self.key_arg_name = None  # type: Optional[str]\n    self.restriction_provider = None\n    self.restriction_provider_arg_name = None\n    self.watermark_estimator_provider = None","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/runners/common.py#L135-L171","documentation":"DoFnSignature.__init__ validates that obj_to_invoke is a DoFn, RestrictionProvider, or WatermarkEstimatorProvider before extracting method arguments. Anything else cannot provide a recognizable process method, so a ValueError is raised immediately.","triggerScenarios":"Constructing DoFnSignature(obj) directly, or invoking a DoFn via runner common.py where a plain function, class (not instance), callable object, or None was passed instead of a DoFn instance.","commonSituations":"Passing a top-level function where a DoFn is expected; forgetting to instantiate the DoFn class (DoFn vs MyDoFn()); custom runners or test harnesses building invokers with the wrong object.","solutions":["Pass an instance of apache_beam.transforms.core.DoFn (e.g. MyDoFn(), not MyDoFn)","If using splittable DoFn machinery, ensure the object exposes a RestrictionProvider","Check that a runner/plugin isn't forwarding a wrapped or partial object into DoFnSignature"],"exampleFix":"// before\nsig = DoFnSignature(MyDoFn)  # class, not instance\n// after\nsig = DoFnSignature(MyDoFn())","handlingStrategy":"type-guard","validationCode":"from apache_beam.transforms.core import DoFn\nassert isinstance(obj, DoFn), f'expected DoFn instance, got {type(obj)}'","typeGuard":"def is_dofn_instance(obj): return isinstance(obj, DoFn)","tryCatchPattern":null,"preventionTips":["Instantiate DoFn classes before passing them","Don't pass plain functions where a DoFn is required"],"tags":["python","apache-beam","dofn"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}