HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/miniconda3/Lib/site-packages/menuinst/_vendor/apipkg/_alias_module.py
from __future__ import annotations

from types import ModuleType

from ._importing import importobj


def AliasModule(modname: str, modpath: str, attrname: str | None = None) -> ModuleType:

    cached_obj: object | None = None

    def getmod() -> object:
        nonlocal cached_obj
        if cached_obj is None:
            cached_obj = importobj(modpath, attrname)
        return cached_obj

    x = modpath + ("." + attrname if attrname else "")
    repr_result = f"<AliasModule {modname!r} for {x!r}>"

    class AliasModule(ModuleType):
        def __repr__(self) -> str:
            return repr_result

        def __getattribute__(self, name: str) -> object:
            try:
                return getattr(getmod(), name)
            except ImportError:
                if modpath == "pytest" and attrname is None:
                    # hack for pylibs py.test
                    return None
                else:
                    raise

        def __setattr__(self, name: str, value: object) -> None:
            setattr(getmod(), name, value)

        def __delattr__(self, name: str) -> None:
            delattr(getmod(), name)

    return AliasModule(str(modname))