{"record":{"id":"e56e0f096685ae08","repo":"apache/beam","slug":"dofn-r-has-duplicate-s-method-parameters-s-userstate","errorCode":null,"errorMessage":"DoFn %r has duplicate %s method parameters: %s.","messagePattern":"DoFn %r has duplicate (.+?) method parameters: (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/userstate.py","lineNumber":274,"sourceCode":"  from apache_beam.runners.common import MethodWrapper\n  from apache_beam.transforms.core import _DoFnParam\n  from apache_beam.transforms.core import _StateDoFnParam\n  from apache_beam.transforms.core import _TimerDoFnParam\n\n  all_state_specs = set()\n  all_timer_specs = set()\n\n  # Validate params to process(), start_bundle(), finish_bundle() and to\n  # any on_timer callbacks.\n  for method_name in dir(dofn):\n    if not isinstance(getattr(dofn, method_name, None), types.MethodType):\n      continue\n    method = MethodWrapper(dofn, method_name)\n    param_ids = [\n        d.param_id for d in method.defaults if isinstance(d, _DoFnParam)\n    ]\n    if len(param_ids) != len(set(param_ids)):\n      raise ValueError(\n          'DoFn %r has duplicate %s method parameters: %s.' %\n          (dofn, method_name, param_ids))\n    for d in method.defaults:\n      if isinstance(d, _StateDoFnParam):\n        all_state_specs.add(d.state_spec)\n      elif isinstance(d, _TimerDoFnParam):\n        all_timer_specs.add(d.timer_spec)\n\n  return all_state_specs, all_timer_specs\n\n\ndef is_stateful_dofn(dofn: 'DoFn') -> bool:\n  \"\"\"Determines whether a given DoFn is a stateful DoFn.\"\"\"\n\n  # A Stateful DoFn is a DoFn that uses user state or timers.\n  all_state_specs, all_timer_specs = get_dofn_specs(dofn)\n  return bool(all_state_specs or all_timer_specs)\n","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/userstate.py#L256-L292","documentation":"get_dofn_specs inspects a stateful DoFn's process/start_bundle/finish_bundle (and timer) signatures for _DoFnParam defaults (e.g. DoFn.StateParam, DoFn.TimerParam, DoFn.TimestampParam). If the same parameter (same param_id) appears more than once in one method's defaults, the spec set would be inconsistent, so a ValueError naming the DoFn, method, and duplicate param ids is raised during pipeline validation.","triggerScenarios":"Writing def process(self, item, state=DoFn.StateParam(SPEC), other=DoFn.StateParam(SPEC)) — the same state spec used twice in one method; two params sharing the same underlying spec/param id; duplicate TimerParam/BagStateParam defaults.","commonSituations":"Copy-pasting a parameter line and forgetting to change the spec; deliberately reading the same state twice in a signature instead of inside the method body; auto-generated DoFn signatures emitting repeated params.","solutions":["Reference each state/timer spec at most once per method signature and reuse the value inside the method body","If you need the same state in multiple places, bind the parameter once and pass the handle around","Fix duplicated parameter defaults so each _DoFnParam in the signature has a distinct spec"],"exampleFix":"// before\ndef process(self, item, a=DoFn.StateParam(BAG_SPEC), b=DoFn.StateParam(BAG_SPEC)): ...\n// after\ndef process(self, item, bag=DoFn.StateParam(BAG_SPEC)):\n    ...use bag...","handlingStrategy":"validation","validationCode":"from apache_beam.transforms.userstate import get_dofn_specs\n# fail fast in tests before submitting the pipeline\nget_dofn_specs(MyDoFn)","typeGuard":null,"tryCatchPattern":"try:\n    validate_stateful_dofn(MyDoFn)\nexcept ValueError as e:\n    raise PipelineDefinitionError(f'fix DoFn signature: {e}') from e","preventionTips":["Use each StateParam/TimerParam at most once per method signature","Run get_dofn_specs/validate_stateful_dofn in unit tests for stateful DoFns","Read shared state once into a local handle instead of duplicating parameters"],"tags":["python","apache-beam","userstate","dofn-signature","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}