{"record":{"id":"bc3daccfef99b4f1","repo":"apache/beam","slug":"element-not-accessible-in-this-context","errorCode":null,"errorMessage":"element not accessible in this context","messagePattern":"element not accessible in this context","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/runners/common.py","lineNumber":1939,"sourceCode":"\n\n# TODO(robertwb): Replace core.DoFnContext with this.\nclass DoFnContext(object):\n  \"\"\"For internal use only; no backwards-compatibility guarantees.\"\"\"\n  def __init__(self, label, element=None, state=None):\n    self.label = label\n    self.state = state\n    if element is not None:\n      self.set_element(element)\n\n  def set_element(self, windowed_value):\n    # type: (Optional[WindowedValue]) -> None\n    self.windowed_value = windowed_value\n\n  @property\n  def element(self):\n    if self.windowed_value is None:\n      raise AttributeError('element not accessible in this context')\n    else:\n      return self.windowed_value.value\n\n  @property\n  def timestamp(self):\n    if self.windowed_value is None:\n      raise AttributeError('timestamp not accessible in this context')\n    else:\n      return self.windowed_value.timestamp\n\n  @property\n  def windows(self):\n    if self.windowed_value is None:\n      raise AttributeError('windows not accessible in this context')\n    else:\n      return self.windowed_value.windows\n","sourceCodeStart":1921,"sourceCodeEnd":1956,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/runners/common.py#L1921-L1956","documentation":"DoFnContext.element raises AttributeError('element not accessible in this context') when the context's windowed_value is None. Certain context types (e.g. start_bundle/finish_bundle contexts, or user-side/timing contexts) have no current element, so reading .element is invalid.","triggerScenarios":"Accessing `self.element` or ctx.element inside start_bundle/finish_bundle, or in a context created with windowed_value=None (e.g. in tests or setup methods).","commonSituations":"Trying to read the current element during bundle setup/teardown; caching context accessors outside of process(); custom runner/DoFnInvoker usage without passing a windowed value.","solutions":["Only access element inside process() where an element is in scope; pass the element as a process parameter instead.","For setup/teardown, use bundle-local state initialized in start_bundle without referencing the element.","In tests, ensure the context is constructed with a WindowedValue before accessing .element."],"exampleFix":"// before\nclass MyDoFn(beam.DoFn):\n    def start_bundle(self):\n        self.last = self.element  # AttributeError\n// after\nclass MyDoFn(beam.DoFn):\n    def process(self, element):\n        self.last = element  # element in scope here","handlingStrategy":"try-catch","validationCode":"if getattr(ctx, 'windowed_value', None) is None:\n    raise SkipElement('no element in this context')","typeGuard":"def has_element(ctx):\n    return getattr(ctx, 'windowed_value', None) is not None","tryCatchPattern":"try:\n    elem = ctx.element\nexcept AttributeError as e:\n    if 'element not accessible' in str(e):\n        elem = None\n    else:\n        raise","preventionTips":["Pass the element as a process() argument instead of reading ctx.element.","Never access element context in start_bundle/finish_bundle.","Keep element-dependent state updates inside process()."],"tags":["python","apache-beam","dofn-context","attribute-error"],"backgroundTag":"attribute-not-accessible","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"}