{"record":{"id":"b91757b02ed2e2d8","repo":"apache/beam","slug":"expected-a-callable-object-instead-of-r","errorCode":null,"errorMessage":"Expected a callable object instead of: %r","messagePattern":"Expected a callable object instead of: %r","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/core.py","lineNumber":1004,"sourceCode":"class CallableWrapperDoFn(DoFn):\n  \"\"\"For internal use only; no backwards-compatibility guarantees.\n\n  A DoFn (function) object wrapping a callable object.\n\n  The purpose of this class is to conveniently wrap simple functions and use\n  them in transforms.\n  \"\"\"\n  def __init__(self, fn, fullargspec=None):\n    \"\"\"Initializes a CallableWrapperDoFn object wrapping a callable.\n\n    Args:\n      fn: A callable object.\n\n    Raises:\n      TypeError: if fn parameter is not a callable type.\n    \"\"\"\n    if not callable(fn):\n      raise TypeError('Expected a callable object instead of: %r' % fn)\n\n    self._fn = fn\n    self._fullargspec = fullargspec\n    if isinstance(\n        fn, (types.BuiltinFunctionType, types.MethodType, types.FunctionType)):\n      self.process = fn\n    else:\n      # For cases such as set / list where fn is callable but not a function\n      self.process = lambda element: fn(element)\n\n    super().__init__()\n\n  def display_data(self):\n    # If the callable has a name, then it's likely a function, and\n    # we show its name.\n    # Otherwise, it might be an instance of a callable class. We\n    # show its class.\n    display_data_value = (","sourceCodeStart":986,"sourceCodeEnd":1022,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/core.py#L986-L1022","documentation":"CallableWrapperDoFn.__init__ (core.py:1004) wraps an arbitrary callable as a DoFn for FlatMap/Map. It validates that `fn` is callable before wrapping; passing a non-callable (a plain value, class instance without __call__, string, etc.) raises this TypeError.","triggerScenarios":"beam.FlatMap(x) / beam.Map(x) where x is not a function or callable object — e.g. FlatMap([1,2,3]), Map('lowercase'), passing a lambda result by mistake, or forgetting the lambda: FlatMap(lambda: ...) mis-parenthesized.","commonSituations":"Typo calling FlatMap with a value instead of a function; passing a bound method from the wrong object; calling a function and passing its result (fn() vs fn); passing None after a failed lookup.","solutions":["Pass a callable: a function, lambda, method, or object implementing __call__.","Remove accidental parentheses so you pass the function itself, not its result.","Check for None values from factory functions that were supposed to return the transform function."],"exampleFix":"# before\nbeam.FlatMap('split_words')\n\n# after\nbeam.FlatMap(lambda line: line.split())","handlingStrategy":"type-guard","validationCode":"def check_callable(fn):\n    if not callable(fn):\n        raise TypeError(f'FlatMap/Map require a callable, got {type(fn).__name__}: {fn!r}')","typeGuard":"def is_callable_arg(x) -> bool:\n    return callable(x)","tryCatchPattern":"try:\n    dofn = beam.FlatMap(fn)\nexcept TypeError as e:\n    if 'Expected a callable object' in str(e):\n        raise TypeError(f'Got {type(fn).__name__}; pass a function/lambda/callable') from e\n    raise","preventionTips":["Never call the function when passing it: FlatMap(fn), not FlatMap(fn())","Use mypy/pyright to type beam.Map/FlatMap arguments as Callable","Check factory helpers return functions, not computed values"],"tags":["python","apache-beam","callable","typeerror"],"backgroundTag":"invalid-argument-value","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"}