Repository: SuperDuperDB/superduperdb
File: superduperdb/misc/annotations.py (Python)

Code before:
                f"**public_api({stability}):** This API is in {stability} "
                "and may change before becoming stable."
            )
            _append_doc(obj, message=message)
        return obj

    return wrap


class SuperDuperDBDeprecationWarning(DeprecationWarning):
    """Specialized Deprecation Warning for fine grained filtering control"""

    pass


if not sys.warnoptions:
    warnings.filterwarnings("module", category=SuperDuperDBDeprecationWarning)


def _append_doc(obj, *, message: str, directive: Optional[str] = None):
    if not obj.__doc__:
        obj.__doc__ = ""

    obj.__doc__ = obj.__doc__.rstrip()

    indent = _get_indent(obj.__doc__)
    obj.__doc__ += "\n\n"

    if directive is not None:
        obj.__doc__ += f"{' ' * indent}.. {directive}::\n\n"

        message = message.replace("\n", "\n" + " " * (indent + 4))
        obj.__doc__ += f"{' ' * (indent + 4)}{message}"
    else:
        message = message.replace("\n", "\n" + " " * (indent + 4))
        obj.__doc__ += f"{' ' * indent}{message}"
    obj.__doc__ += f"\n{' ' * indent}"


def _get_indent(docstring: str) -> int:
    if not docstring:
        return 0

    non_empty_lines = list(filter(bool, docstring.splitlines()))
    if len(non_empty_lines) == 1:
        return 0

    return len(non_empty_lines[1]) - len(non_empty_lines[1].lstrip())


Review comment (bug):
A bug here

when we call `requires_modules(['transformer<=4.36.2'])`
if we check `<` before checking `<=`, that will be a bug.

Code after:
            f"{requirement} is required for a normal"
            f"functioning of this module, but found {pkg}=={got_ver}"
        )


def check_versioned(module_name, pkg, got_ver):
    if len(module_name) == 3:
        op = None
        if module_name[1] is None:
            # Upper bounded version check
            want_ver = module_name[-1]
            op = '<='
            _compare_versions(op, got_ver, want_ver, f'{pkg}{op}{want_ver}', pkg)
        elif module_name[-1] is None:
            # Lower bounded version check
            want_ver = module_name[1]
            op = '>='
            _compare_versions(op, got_ver, want_ver, f'{pkg}{op}{want_ver}', pkg)
        else:
            # Lower and upper bounded version check
            want_ver_lower = module_name[1]
            want_ver_upper = module_name[-1]
            _compare_versions(
                '>=', got_ver, want_ver_lower, f'{pkg}>={want_ver_lower}', pkg
            )
            _compare_versions(
                '<=', got_ver, want_ver_upper, f'{pkg}<={want_ver_upper}', pkg
            )

    elif len(module_name) == 2:
        # Exact version check
        got_ver = importlib.metadata.version(module_name[0])
        want_ver = module_name[1]
        _compare_versions('==', got_ver, want_ver, f'{pkg}<={want_ver}', pkg)

    else:
        raise ValueError(
            f'Cannot check the package requirement for the module {module_name}'
        )


def requires_packages(*modules):
    '''
    A utility function to check required packages for a module
    i.e superduperdb.ext
    '''
    missing_modules = []
    missing_module_versions = []
    for module_name in modules:
        pkg = module_name[0]
        if len(module_name) == 1: