{"record":{"id":"2d921784473c8574","repo":"apache/beam","slug":"notimplementederror-userstate","errorCode":null,"errorMessage":"NotImplementedError","messagePattern":"NotImplementedError","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/userstate.py","lineNumber":327,"sourceCode":"\n  # Reject DoFns that use timer specs without corresponding timer callbacks.\n  for timer_spec in all_timer_specs:\n    if not timer_spec._attached_callback:\n      raise ValueError((\n          'DoFn %r has a TimerSpec without an associated on_timer '\n          'callback: %s.') % (dofn, timer_spec))\n    method_name = timer_spec._attached_callback.__name__\n    if (timer_spec._attached_callback != getattr(dofn, method_name,\n                                                 None).__func__):  # type: ignore[union-attr]\n      raise ValueError((\n          'The on_timer callback for %s is not the specified .%s method '\n          'for DoFn %r (perhaps it was overwritten?).') %\n                       (timer_spec, method_name, dofn))\n\n\nclass BaseTimer(object):\n  def clear(self, dynamic_timer_tag: str = '') -> None:\n    raise NotImplementedError\n\n  def set(self, timestamp: Timestamp, dynamic_timer_tag: str = '') -> None:\n    raise NotImplementedError\n\n\n_TimerTuple = collections.namedtuple('timer_tuple', ('cleared', 'timestamp'))  # type: ignore[name-match]\n\n\nclass RuntimeTimer(BaseTimer):\n  \"\"\"Timer interface object passed to user code.\"\"\"\n  def __init__(self) -> None:\n    self._timer_recordings: dict[str, _TimerTuple] = {}\n    self._cleared = False\n    self._new_timestamp: Optional[Timestamp] = None\n\n  def clear(self, dynamic_timer_tag: str = '') -> None:\n    self._timer_recordings[dynamic_timer_tag] = _TimerTuple(\n        cleared=True, timestamp=None)","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/userstate.py#L309-L345","documentation":"BaseTimer.clear is an abstract-style placeholder in apache_beam.transforms.userstate that unconditionally raises NotImplementedError. It defines the timer interface; only concrete runtime implementations (e.g. RuntimeTimer in the direct runner) provide a body. Calling clear() on BaseTimer directly means you are using the interface class instead of a runtime-backed timer.","triggerScenarios":"Calling clear() on a BaseTimer instance obtained outside a running pipeline context (e.g. instantiating BaseTimer manually in tests or custom code) instead of using the runner-provided RuntimeTimer.","commonSituations":"Unit-testing a DoFn's timer methods by hand-constructing timer objects; custom runner integrations that forgot to subclass BaseTimer and override clear; calling clear before the DoFn's timer context is set up.","solutions":["Use the runner-provided runtime timer (RuntimeTimer) rather than BaseTimer directly.","In tests, inject a fake subclass that overrides clear() instead of instantiating BaseTimer.","If writing a runner/integration, implement clear() in your BaseTimer subclass."],"exampleFix":"# before\ntimer = BaseTimer(); timer.clear()\n# after\nclass FakeTimer(BaseTimer):\n  def clear(self, dynamic_timer_tag='') -> None: self.cleared = True\ntimer = FakeTimer(); timer.clear()","handlingStrategy":"type-guard","validationCode":"assert isinstance(timer, BaseTimer) and type(timer) is not BaseTimer, 'use a runtime timer implementation'","typeGuard":"def is_usable_timer(t) -> bool:\n  return isinstance(t, BaseTimer) and type(t).clear is not BaseTimer.clear","tryCatchPattern":"try:\n  timer.clear()\nexcept NotImplementedError as e:\n  logging.error('timer backend does not implement clear: %s', e)","preventionTips":["Never instantiate BaseTimer directly; get timers from the DoFn process context.","In tests, subclass BaseTimer and override clear().","When writing runner integrations, implement every BaseTimer method."],"tags":["python","apache-beam","timers","not-implemented","abstract-method"],"backgroundTag":"method-not-implemented","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"}