Skip to content

Commit

Permalink
feat: Use fallback_version for source = "call"
Browse files Browse the repository at this point in the history
Fixes #262

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Oct 8, 2024
1 parent 88c3f44 commit 07d5540
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/pdm/backend/hooks/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from typing import Callable

from pdm.backend._vendor.packaging.version import Version
from pdm.backend.exceptions import ConfigError, PDMWarning, ValidationError
from pdm.backend.hooks.base import Context
from pdm.backend.hooks.version.scm import SCMVersion as SCMVersion
Expand Down Expand Up @@ -55,12 +56,12 @@ def pdm_build_initialize(self, context: Context) -> None:
)
return
options = {k: v for k, v in version_config.items() if k != "source"}
metadata["version"] = getattr(self, f"resolve_version_from_{source}")(
context, **options
metadata["version"] = str(
getattr(self, f"resolve_version_from_{source}")(context, **options)
)
metadata["dynamic"].remove("version")

def resolve_version_from_file(self, context: Context, path: str) -> str:
def resolve_version_from_file(self, context: Context, path: str) -> Version:
"""Resolve version from a file."""
version_source = context.root / path
with open(version_source, encoding="utf-8") as fp:
Expand All @@ -72,7 +73,7 @@ def resolve_version_from_file(self, context: Context, path: str) -> str:
f"Couldn't find version in file {version_source!r}, "
"it should appear as `__version__ = 'a.b.c'`.",
)
return match.group(1)
return Version(match.group(1))

def resolve_version_from_scm(
self,
Expand All @@ -83,7 +84,7 @@ def resolve_version_from_scm(
tag_filter: str | None = None,
version_format: str | None = None,
fallback_version: str | None = None,
) -> str:
) -> Version:
if os.environ.get("PDM_BUILD_SCM_VERSION"):
version = os.environ["PDM_BUILD_SCM_VERSION"]
else:
Expand Down Expand Up @@ -114,9 +115,18 @@ def resolve_version_from_scm(
version = version_formatter(scm_version, context) # type: ignore[call-arg]
else:
version = version_formatter(scm_version) # type: ignore[call-arg]

try:
parsed_version = Version(version)
except ValueError:
if fallback_version is not None:
return Version(fallback_version)
raise ConfigError(
f"Invalid version {version}, it must comply with PEP 440. \n"
"You can still specify the version via environment variable "
"`PDM_BUILD_SCM_VERSION`, or specify `fallback_version` config."
)
self._write_version(context, version, write_to, write_template)
return version
return parsed_version

def _write_version(
self,
Expand Down Expand Up @@ -146,8 +156,17 @@ def resolve_version_from_call(
getter: str,
write_to: str | None = None,
write_template: str = "{}\n",
) -> str:
fallback_version: str | None = None,
) -> Version:
version_getter, args = evaluate_module_attribute(getter, context.root)
version = version_getter(*args)
if version is None:
if fallback_version is not None:
return Version(fallback_version)
else:
raise ConfigError(
"Cannot get version from the call, you can specify fallback version via `fallback_version` config."
)
parsed_version = Version(version)
self._write_version(context, version, write_to, write_template)
return version
return parsed_version

0 comments on commit 07d5540

Please sign in to comment.