{"record":{"id":"92d1fb2972603955","repo":"apache/beam","slug":"name-is-not-a-string","errorCode":null,"errorMessage":"name is not a string","messagePattern":"name is not a string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/userstate.py","lineNumber":52,"sourceCode":"from apache_beam.coders import coders\nfrom apache_beam.portability import common_urns\nfrom apache_beam.portability.api import beam_runner_api_pb2\nfrom apache_beam.transforms.timeutil import TimeDomain\nfrom apache_beam.utils import windowed_value\nfrom apache_beam.utils.timestamp import Timestamp\n\nif TYPE_CHECKING:\n  from apache_beam.runners.pipeline_context import PipelineContext\n  from apache_beam.transforms.core import DoFn\n\nCallableT = TypeVar('CallableT', bound=Callable)\n\n\nclass StateSpec(object):\n  \"\"\"Specification for a user DoFn state cell.\"\"\"\n  def __init__(self, name: str, coder: Coder) -> None:\n    if not isinstance(name, str):\n      raise TypeError(\"name is not a string\")\n    if not isinstance(coder, Coder):\n      raise TypeError(\"coder is not of type Coder\")\n    self.name = name\n    self.coder = coder\n\n  def __repr__(self) -> str:\n    return '%s(%s)' % (self.__class__.__name__, self.name)\n\n  def to_runner_api(\n      self, context: 'PipelineContext') -> beam_runner_api_pb2.StateSpec:\n    raise NotImplementedError\n\n\nclass ReadModifyWriteStateSpec(StateSpec):\n  \"\"\"Specification for a user DoFn value state cell.\n     Read more about ReadModifyWriteState (ValueState) here:\n     https://beam.apache.org/documentation/programming-guide/#valuestate\n  \"\"\"","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/userstate.py#L34-L70","documentation":"StateSpec.__init__ validates that the state cell name is a Python str. A TypeError is raised immediately at spec construction time because the name is used as a dictionary/registry key throughout the state machinery and must be a string.","triggerScenarios":"Calling StateSpec(42, coder), StateSpec(b'name', coder), StateSpec(None, coder), or otherwise passing a non-string as the first positional argument to StateSpec (or its subclasses like ReadModifyWriteStateSpec, CombiningValueStateSpec, BagStateSpec, SetStateSpec, TimerSpec-adjacent specs).","commonSituations":"Passing a variable that is accidentally an int/enum/bytes; f-string-free concatenation mistakes; confusing argument order and passing the coder first.","solutions":["Pass a string as the name argument, e.g. StateSpec('running_sum', coder)","Check the argument order — name comes before coder","Coerce the variable with str(...) only if that preserves the intended identifier"],"exampleFix":"// before\nspec = StateSpec(42, coders.StrUtf8Coder())\n// after\nspec = StateSpec('counter', coders.StrUtf8Coder())","handlingStrategy":"type-guard","validationCode":"if not isinstance(name, str):\n    raise TypeError(f'StateSpec name must be str, got {type(name).__name__}')","typeGuard":"def is_valid_state_name(name) -> bool:\n    return isinstance(name, str)","tryCatchPattern":"try:\n    spec = StateSpec(name, coder)\nexcept TypeError as e:\n    raise TypeError(f'bad StateSpec args (name={name!r}): {e}') from e","preventionTips":["Always use a short descriptive string as the state name","Double-check positional order: name first, coder second","Run unit tests that construct all your StateSpecs at import time to fail fast"],"tags":["python","apache-beam","userstate","typeerror"],"backgroundTag":"type-mismatch","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"}