Skip to content

Sync package dependencies

Expriment with setting pyproject versions to latest lock file versions.

Functions⚓︎

replace_versions ⚓︎

replace_versions(path_lock)

Read packages from poetry.lock and update the versions in pyproject.toml.

PARAMETER DESCRIPTION
path_lock

Path to the poetry.lock file

TYPE: Path

RAISES DESCRIPTION
NotImplementedError

if a lock file other that the poetry lock file is used

Source code in calcipy/experiments/sync_package_dependencies.py
def replace_versions(path_lock: Path) -> None:
    """Read packages from poetry.lock and update the versions in pyproject.toml.

    Args:
        path_lock: Path to the poetry.lock file

    Raises:
        NotImplementedError: if a lock file other that the poetry lock file is used

    """
    if path_lock.name != 'poetry.lock':
        msg = f'Expected a path to a "poetry.lock" file. Instead, received: "{path_lock.name}"'
        raise NotImplementedError(msg)

    lock = tomllib.loads(path_lock.read_text(encoding='utf-8', errors='ignore'))
    lock_versions = {dependency['name']: dependency['version'] for dependency in lock['package']}

    path_pyproject = path_lock.parent / 'pyproject.toml'
    pyproject_text = path_pyproject.read_text(encoding='utf-8')
    pyproject_versions = _collect_pyproject_versions(pyproject_text)

    path_pyproject.write_text(_replace_pyproject_versions(lock_versions, pyproject_versions, pyproject_text))