from pathlib import Path


def resolve_import(import_: str, location: str, file: Path) -> str:
    """Resolve potentially relative import inside a package."""
    if not import_.startswith('.'):
        return import_

    import_parts = import_.split('.')
    up_levels = len([p for p in import_parts if p == ''])
    import_down = import_parts[up_levels:]

    if file.stem == '__init__':
        up_levels -= 1
    location_parts = location.split('.')

    # up_levels can be 0
    location_parts = location_parts[:-up_levels or None]
    return '.'.join(location_parts + import_down)