{"record":{"id":"74d40cea55fb04d6","repo":"apache/beam","slug":"could-not-find-code-object-with-path-code-object-identifier","errorCode":null,"errorMessage":"Could not find code object with path: {code_object_identifier}","messagePattern":"Could not find code object with path: (.+?)","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/internal/code_object_pickler.py","lineNumber":463,"sourceCode":"    elif lambda_with_args_result := _LAMBDA_WITH_ARGS_PATTERN.fullmatch(part):\n      obj = _get_code_object_from_lambda_with_args_pattern(\n          obj, lambda_with_args_result, code_object_identifier)\n    elif lambda_with_hash_result := _LAMBDA_WITH_HASH_PATTERN.fullmatch(part):\n      obj = _get_code_object_from_lambda_with_hash_pattern(\n          obj, lambda_with_hash_result, code_object_identifier)\n    elif default_result := _DEFAULT_PATTERN.fullmatch(part):\n      index = int(default_result.group(2))\n      if index >= len(obj.__defaults__):\n        raise ValueError(\n            f'Index {index} is out of bounds for obj.__defaults__'\n            f' {len(obj.__defaults__)} in path {code_object_identifier}')\n      obj = getattr(obj, '__defaults__')[index]\n    else:\n      obj = getattr(obj, part)\n  if isinstance(obj, types.CodeType):\n    return obj\n  else:\n    raise AttributeError(\n        f'Could not find code object with path: {code_object_identifier}')\n\n\ndef _signature(obj: types.CodeType):\n  \"\"\"Returns the signature of a code object.\n\n  The signature is the names of the arguments of the code object. This is used\n  to unique identify lambdas.\n\n  Args:\n    obj: A code object, function, method, or cell.\n\n  Returns:\n    A tuple of the names of the arguments of the code object.\n  \"\"\"\n  return obj.co_varnames[:_get_arg_count(obj)]\n\n","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/code_object_pickler.py#L445-L481","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the path ends at an actual code object (a function definition), not a class, variable, or bound method.","Append the missing segment if the path was truncated.","Change the target to a plain function so code-object resolution succeeds.","Use a different serialization mechanism (cloudpickle directly) for non-code-object callables."],"exampleFix":"// before\nget_code_from_identifier('mylib.MyClass')  # class, not code object\n// after\nget_code_from_identifier('mylib.my_function')  # resolves to CodeType","handlingStrategy":"type-guard","validationCode":"import sys, types\ndef resolves_to_code(path):\n    obj = sys.modules.get(path.split('.', 1)[0])\n    if obj is None: return False\n    try:\n        for p in path.split('.')[1:]: obj = getattr(obj, p)\n    except AttributeError:\n        return False\n    return isinstance(obj, types.CodeType)","typeGuard":"def is_code_type(obj) -> bool:\n    import types\n    return isinstance(obj, types.CodeType)","tryCatchPattern":"try:\n    code = get_code_from_identifier(path)\nexcept AttributeError:\n    raise RuntimeError(f'{path} resolves but is not a code object; point the identifier at a function')","preventionTips":["Only serialize plain module-level functions via identifiers.","Don't craft identifier paths to classes or constants.","Validate endpoints with isinstance(obj, types.CodeType) in tests."],"tags":["python","apache-beam","pickling","type-mismatch"],"backgroundTag":"type-mismatch","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"}