{"record":{"id":"9a5525c46e21f956","repo":"apache/beam","slug":"windows-not-accessible-in-this-context","errorCode":null,"errorMessage":"windows not accessible in this context","messagePattern":"windows not accessible in this context","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/runners/common.py","lineNumber":1953,"sourceCode":"\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":1935,"sourceCodeEnd":1956,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/runners/common.py#L1935-L1956","documentation":"BeamBaseDoFnContext/Receiver raises this AttributeError when code accesses the `windows` property but the context has no windowed value attached. Beam contexts are only fully populated at certain phases of element processing (e.g. inside DoFn.process), so accessing context metadata outside that window is invalid.","triggerScenarios":"Accessing `context.windows` on a DoFnContext whose `windowed_value` is None — typically accessing context.windows outside of `process()` invocation, e.g. in setup/teardown, or holding a reference to the context and reading it after processing finished.","commonSituations":"Calling context.windows from a timer callback, from setup(), from a method captured/deferred, or in unit tests that construct a context without a windowed value.","solutions":["Only access `self._context.windows` inside `DoFn.process()` where a windowed value is guaranteed.","If you need windows elsewhere, carry the WindowedValue explicitly instead of reaching through the context.","In tests, construct the context with a real windowed value (e.g. WindowedValue(elem, timestamp, [GlobalWindow()]))."],"exampleFix":"// before\nvalue = fn.setup_context.windows\n// after\nvalue = process_context.windows  # inside process(), where windowed_value is set","handlingStrategy":"type-guard","validationCode":"if getattr(ctx, 'windowed_value', None) is None:\n    raise RuntimeError('context.windows only available during process()')","typeGuard":"def windows_accessible(ctx) -> bool:\n    return getattr(ctx, 'windowed_value', None) is not None","tryCatchPattern":"try:\n    wins = ctx.windows\nexcept AttributeError:\n    wins = []  # or defer: capture inside process() instead","preventionTips":["Only touch DoFn context properties inside process().","Never store the context for later use; store the needed values instead.","In tests, build contexts with real WindowedValues."],"tags":["python","apache-beam","dofn-context","attribute-error"],"backgroundTag":"invalid-state-transition","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"}