Repository: huggingface/diffusers
File: src/diffusers/utils/dynamic_modules_utils.py (Python)

Code before:
    # Imports of the form `import xxx`
    imports = re.findall(r"^\s*import\s+(\S+)\s*$", content, flags=re.MULTILINE)
    # Imports of the form `from xxx import yyy`
    imports += re.findall(r"^\s*from\s+(\S+)\s+import", content, flags=re.MULTILINE)
    # Only keep the top-level module
    imports = [imp.split(".")[0] for imp in imports if not imp.startswith(".")]

    # Unique-ify and test we got them all
    imports = list(set(imports))
    missing_packages = []
    for imp in imports:
        try:
            importlib.import_module(imp)
        except ImportError:
            missing_packages.append(imp)

    if len(missing_packages) > 0:
        raise ImportError(
            "This modeling file requires the following packages that were not found in your environment: "
            f"{', '.join(missing_packages)}. Run `pip install {' '.join(missing_packages)}`"
        )

    return get_relative_imports(filename)


def get_class_in_module(pretrained_model_name_or_path, class_name, module_path):
    """
    Import a module on the cache directory for modules and extract a class from it.
    """
    module_path = module_path.replace(os.path.sep, ".")
    try:
        module = importlib.import_module(module_path)
    except ModuleNotFoundError as e:
        # This can happen when the repo id contains ".", which Python's import machinery interprets as a directory
        # separator. We do a bit of monkey patching to detect and fix this case.
        if not (
            "." in pretrained_model_name_or_path
            and module_path.startswith("diffusers_modules")
            and pretrained_model_name_or_path.replace("/", "--") in module_path
        ):
            raise e  # We can't figure this one out, just reraise the original error

        corrected_path = os.path.join(HF_MODULES_CACHE, module_path.replace(".", "/")) + ".py"
        corrected_path = corrected_path.replace(
            pretrained_model_name_or_path.replace("/", "--").replace(".", "/"),
            pretrained_model_name_or_path.replace("/", "--"),
        )
        module = importlib.machinery.SourceFileLoader(module_path, corrected_path).load_module()

    if class_name is None:
        return find_pipeline_class(module)

Review comment (suggestion):
Could we do:

```py
def get_class_in_module(class_name, module_path, pretrained_model_name_or_path=None):
```

This will ensure the change is not backward breaking.

Code after:
    # Imports of the form `import xxx`
    imports = re.findall(r"^\s*import\s+(\S+)\s*$", content, flags=re.MULTILINE)
    # Imports of the form `from xxx import yyy`
    imports += re.findall(r"^\s*from\s+(\S+)\s+import", content, flags=re.MULTILINE)
    # Only keep the top-level module
    imports = [imp.split(".")[0] for imp in imports if not imp.startswith(".")]

    # Unique-ify and test we got them all
    imports = list(set(imports))
    missing_packages = []
    for imp in imports:
        try:
            importlib.import_module(imp)
        except ImportError:
            missing_packages.append(imp)

    if len(missing_packages) > 0:
        raise ImportError(
            "This modeling file requires the following packages that were not found in your environment: "
            f"{', '.join(missing_packages)}. Run `pip install {' '.join(missing_packages)}`"
        )

    return get_relative_imports(filename)


def get_class_in_module(class_name, module_path, pretrained_model_name_or_path=None):
    """
    Import a module on the cache directory for modules and extract a class from it.
    """
    module_path = module_path.replace(os.path.sep, ".")
    try:
        module = importlib.import_module(module_path)
    except ModuleNotFoundError as e:
        # This can happen when the repo id contains ".", which Python's import machinery interprets as a directory
        # separator. We do a bit of monkey patching to detect and fix this case.
        if not (
            pretrained_model_name_or_path is not None
            and "." in pretrained_model_name_or_path
            and module_path.startswith("diffusers_modules")
            and pretrained_model_name_or_path.replace("/", "--") in module_path
        ):
            raise e  # We can't figure this one out, just reraise the original error

        corrected_path = os.path.join(HF_MODULES_CACHE, module_path.replace(".", "/")) + ".py"
        corrected_path = corrected_path.replace(
            pretrained_model_name_or_path.replace("/", "--").replace(".", "/"),
            pretrained_model_name_or_path.replace("/", "--"),
        )
        module = importlib.machinery.SourceFileLoader(module_path, corrected_path).load_module()

    if class_name is None: