这是indexloc提供的服务,不要输入任何密码
Skip to content

🚀 Feature Request: Allow fallback version when setuptools-scm lookup fails #102

@gaborbernat

Description

@gaborbernat

🧩 Problem

In some corporate environments, source control metadata (like .git) is stripped or unavailable during the packaging process. This causes setuptools-scm to fail when trying to resolve the version, which in turn can break builds using hatchling and other build backends.

✅ Proposed Solution

Introduce a mechanism that allows suppressing setuptools-scm lookup failures and fallback to a predefined static version instead. This fallback enables artifact packaging to proceed without SCM access, while still benefiting from dynamic versioning during development.

💡 Use Case

This pattern is particularly helpful in corporate or air-gapped environments where:

  • SCM metadata is not available during artifact packaging.
  • Artifactories or CI systems set the version downstream
  • Developers still want dynamic versioning from setuptools-scm when working locally.

🧪 Minimal Working Example

"""Dynamic hatchling code during artifact build."""

from __future__ import annotations
from contextlib import suppress
from pathlib import Path
from typing import Any

from hatchling.metadata.plugin.interface import MetadataHookInterface
from setuptools_scm import get_version


class CustomMetadataHook(MetadataHookInterface):
    """Dynamically set project metadata."""

    def update(self, metadata: dict[str, Any]) -> None:
        """:param metadata: the metadata to alter"""
        version = "0.0.1+notset"
        with suppress(ValueError):
            version = get_version(root=Path.cwd(), relative_to=__file__)
        metadata["version"] = version


__all__ = [
    "CustomMetadataHook",
]

This allows local builds to benefit from SCM-based versioning, while fallback ensures packaging doesn’t fail in restricted environments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions