Repository: celery/celery
File: celery/loaders/base.py (Python)

Code before:
    @property
    def conf(self):
        """Loader configuration."""
        if self._conf is unconfigured:
            self._conf = self.read_configuration()
        return self._conf


def autodiscover_tasks(packages, related_name='tasks'):
    global _RACE_PROTECTION

    if _RACE_PROTECTION:
        return ()
    _RACE_PROTECTION = True
    try:
        return [find_related_module(pkg, related_name) for pkg in packages]
    finally:
        _RACE_PROTECTION = False


def find_related_module(package, related_name):
    """Find module in package."""
    # Django 1.7 allows for specifying a class name in INSTALLED_APPS.
    # (Issue #2248).
    try:
        # Return package itself when no related_name.
        module = importlib.import_module(package)
        if not related_name and module:
            return module
    except ModuleNotFoundError:
        # On import error, try to walk package up one level.
        package, _, _ = package.rpartition('.')
        if not package:
            raise

    module_name = f'{package}.{related_name}'

    try:
        # Try to find related_name under package.
        return importlib.import_module(module_name)
    except ModuleNotFoundError as e:
        import_exc_name = getattr(e, 'name', module_name)
        # If sibling does not exist return None.
        if import_exc_name == module_name:
            return

        # Otherwise, raise because error probably originated from a nested import.
        raise e


Review comment (suggestion):
I dropped some notes to myself in these comments to try and figure out what each of these conditions was trying to do. Happy to remove if you don't want to clutter the code.

Code after:
    @property
    def conf(self):
        """Loader configuration."""
        if self._conf is unconfigured:
            self._conf = self.read_configuration()
        return self._conf


def autodiscover_tasks(packages, related_name='tasks'):
    global _RACE_PROTECTION

    if _RACE_PROTECTION:
        return ()
    _RACE_PROTECTION = True
    try:
        return [find_related_module(pkg, related_name) for pkg in packages]
    finally:
        _RACE_PROTECTION = False


def find_related_module(package, related_name):
    """Find module in package."""
    # Django 1.7 allows for specifying a class name in INSTALLED_APPS.
    # (Issue #2248).
    try:
        # Return package itself when no related_name.
        module = importlib.import_module(package)
        if not related_name and module:
            return module
    except ModuleNotFoundError:
        # On import error, try to walk package up one level.
        package, _, _ = package.rpartition('.')
        if not package:
            raise

    module_name = f'{package}.{related_name}'

    try:
        # Try to find related_name under package.
        return importlib.import_module(module_name)
    except ModuleNotFoundError as e:
        import_exc_name = getattr(e, 'name', None)
        # If candidate does not exist, then return None.
        if import_exc_name and module_name.startswith(import_exc_name):
            return

        # Otherwise, raise because error probably originated from a nested import.
        raise e