apache/beam · error · AttributeError

Could not find code object with path: {code_object_identifie

Error message

Could not find code object with path: {code_object_identifier}

What it means

After walking all dot-separated path segments, get_code_from_identifier requires the final object to be an instance of types.CodeType. If traversal ends on a function, class, or plain attribute instead, it raises AttributeError 'Could not find code object with path'. The path resolved to something, but not to a code object.

Source

Thrown at sdks/python/apache_beam/internal/code_object_pickler.py:463

    elif lambda_with_args_result := _LAMBDA_WITH_ARGS_PATTERN.fullmatch(part):
      obj = _get_code_object_from_lambda_with_args_pattern(
          obj, lambda_with_args_result, code_object_identifier)
    elif lambda_with_hash_result := _LAMBDA_WITH_HASH_PATTERN.fullmatch(part):
      obj = _get_code_object_from_lambda_with_hash_pattern(
          obj, lambda_with_hash_result, code_object_identifier)
    elif default_result := _DEFAULT_PATTERN.fullmatch(part):
      index = int(default_result.group(2))
      if index >= len(obj.__defaults__):
        raise ValueError(
            f'Index {index} is out of bounds for obj.__defaults__'
            f' {len(obj.__defaults__)} in path {code_object_identifier}')
      obj = getattr(obj, '__defaults__')[index]
    else:
      obj = getattr(obj, part)
  if isinstance(obj, types.CodeType):
    return obj
  else:
    raise AttributeError(
        f'Could not find code object with path: {code_object_identifier}')


def _signature(obj: types.CodeType):
  """Returns the signature of a code object.

  The signature is the names of the arguments of the code object. This is used
  to unique identify lambdas.

  Args:
    obj: A code object, function, method, or cell.

  Returns:
    A tuple of the names of the arguments of the code object.
  """
  return obj.co_varnames[:_get_arg_count(obj)]

View on GitHub (pinned to 12126d8942)

Solutions

  1. Ensure the path ends at an actual code object (a function definition), not a class, variable, or bound method.
  2. Append the missing segment if the path was truncated.
  3. Change the target to a plain function so code-object resolution succeeds.
  4. Use a different serialization mechanism (cloudpickle directly) for non-code-object callables.

Example fix

// before
get_code_from_identifier('mylib.MyClass')  # class, not code object
// after
get_code_from_identifier('mylib.my_function')  # resolves to CodeType
Defensive patterns

Strategy: type-guard

Validate before calling

import sys, types
def resolves_to_code(path):
    obj = sys.modules.get(path.split('.', 1)[0])
    if obj is None: return False
    try:
        for p in path.split('.')[1:]: obj = getattr(obj, p)
    except AttributeError:
        return False
    return isinstance(obj, types.CodeType)

Type guard

def is_code_type(obj) -> bool:
    import types
    return isinstance(obj, types.CodeType)

Try / catch

try:
    code = get_code_from_identifier(path)
except AttributeError:
    raise RuntimeError(f'{path} resolves but is not a code object; point the identifier at a function')

Prevention

When it happens

Trigger: Identifier pointing at a module-level attribute that is not a code object, e.g. 'module.MyClass', 'module.CONSTANT'; getattr succeeds at each step but the endpoint is not an instance of types.CodeType.

Common situations: Hand-crafted paths in tests; identifiers built for classes or data instead of functions; missing the final segment that selects the code object; API drift where the target was refactored from function to callable object.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/74d40cea55fb04d6. Report an issue: GitHub.