libs/core/langchain_core/_import_utils.py PYTHON 42 lines View on github.com → Search inside
1from importlib import import_module234def import_attr(5    attr_name: str,6    module_name: str | None,7    package: str | None,8) -> object:9    """Import an attribute from a module located in a package.1011    This utility function is used in custom `__getattr__` methods within `__init__.py`12    files to dynamically import attributes.1314    Args:15        attr_name: The name of the attribute to import.16        module_name: The name of the module to import from.1718            If `None`, the attribute is imported from the package itself.19        package: The name of the package where the module is located.2021    Raises:22        ImportError: If the module cannot be found.23        AttributeError: If the attribute does not exist in the module or package.2425    Returns:26        The imported attribute.27    """28    if module_name == "__module__" or module_name is None:29        try:30            result = import_module(f".{attr_name}", package=package)31        except ModuleNotFoundError:32            msg = f"module '{package!r}' has no attribute {attr_name!r}"33            raise AttributeError(msg) from None34    else:35        try:36            module = import_module(f".{module_name}", package=package)37        except ModuleNotFoundError as err:38            msg = f"module '{package!r}.{module_name!r}' not found ({err})"39            raise ImportError(msg) from None40        result = getattr(module, attr_name)41    return result

Code quality findings 1

Ensure functions have docstrings for documentation
missing-docstring
def import_attr(

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.