{"record":{"id":"0f25cfac05dae4bb","repo":"apache/beam","slug":"cannot-check-importability-of-instances","errorCode":null,"errorMessage":"cannot check importability of {} instances","messagePattern":"cannot check importability of (.+?) instances","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py","lineNumber":354,"sourceCode":"    \"\"\"\n  if isinstance(obj, types.FunctionType) or issubclass(type(obj), type):\n    module_and_name = _lookup_module_and_qualname(obj, name=name, config=config)\n    if module_and_name is None:\n      return False\n    module, name = module_and_name\n    return not _is_registered_pickle_by_value(module)\n\n  elif isinstance(obj, types.ModuleType):\n    # We assume that sys.modules is primarily used as a cache mechanism for\n    # the Python import machinery. Checking if a module has been added in\n    # is sys.modules therefore a cheap and simple heuristic to tell us\n    # whether we can assume that a given module could be imported by name\n    # in another Python process.\n    if _is_registered_pickle_by_value(obj):\n      return False\n    return obj.__name__ in sys.modules\n  else:\n    raise TypeError(\n        \"cannot check importability of {} instances\".format(type(obj).__name__))\n\n\ndef _lookup_module_and_qualname(obj, name=None, config=DEFAULT_CONFIG):\n  if name is None:\n    name = getattr(obj, \"__qualname__\", None)\n  if name is None:  # pragma: no cover\n    # This used to be needed for Python 2.7 support but is probably not\n    # needed anymore. However we keep the __name__ introspection in case\n    # users of cloudpickle rely on this old behavior for unknown reasons.\n    name = getattr(obj, \"__name__\", None)\n\n  module_name = _whichmodule(obj, name)\n\n  if module_name is None:\n    # In this case, obj.__module__ is None AND obj was not found in any\n    # imported module. obj is thus treated as dynamic.\n    return None","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py#L336-L372","documentation":"_should_pickle_by_reference() decides whether an object (module, class, or function) can be pickled by importable reference. It handles only types.ModuleType, function-like, and type objects; anything else (e.g. an instance or partial) reaches the else branch and raises TypeError, since importability cannot be judged for such objects.","triggerScenarios":"Internal cloudpickle dispatch calling _should_pickle_by_reference with an object that is not a module, type, or function — e.g. pickling functools.partial, a lambda-bound callable wrapper, or a callable class instance that falls through save_global/save_function dispatch.","commonSituations":"Pickling pipelines containing non-module-level callables (DoFns wrapping callables, Closures over objects); vendored cloudpickle version mismatches where dispatch tables differ.","solutions":["Ensure pickled callables are functions, classes, or modules defined at module level.","If hit from Beam pickling, wrap the value so a proper function/class is pickled, or use a DoFn.","Check that the object's type is supported before passing it into a pickled pipeline element.","Update apache_beam / cloudpickle versions if this arises from a dispatch bug."],"exampleFix":"// before\ntransform = Map(partial(process, config))  # partial may reach the check\n// after\ndef process_with_config(x, config=config):\n    return process(x, config)\ntransform = Map(process_with_config)","handlingStrategy":"type-guard","validationCode":"import types, inspect\nassert isinstance(obj, (types.ModuleType, type)) or inspect.isfunction(obj), 'object must be module/class/function to pickle by reference'","typeGuard":"def reference_pickleable(obj) -> bool:\n    import types, inspect\n    return isinstance(obj, (types.ModuleType, type)) or inspect.isfunction(obj)","tryCatchPattern":"try:\n    pickler.dumps(obj)\nexcept TypeError as e:\n    if 'cannot check importability' in str(e):\n        pickler.dumps(make_module_level_equivalent(obj))\n    else:\n        raise","preventionTips":["Keep pickled callables at module level.","Avoid partials/callable instances in transform arguments; wrap in module-level functions.","Pin compatible apache_beam/cloudpickle versions."],"tags":["python","apache-beam","cloudpickle","pickling"],"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"}