Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Custom Version Scheme Outside setup.py (resolves #781) #803

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,25 @@ The callable must return the configuration.
)


Customizing Version Scheme with pyproject.toml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To support custom version schemes in pyproject.toml, you may specify your own function as an entrypoint for getting the version.

.. code:: toml

# pyproject.toml
[tool.setuptools_scm]
version_scheme = "myproject.my_file:myversion_func"

.. code:: python

# myproject/my_file
def myversion_func(version: ScmVersion):
from setuptools_scm.version import guess_next_version
return version.format_next_version(guess_next_version, '{guessed}b{distance}')


Note on testing non-installed versions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
16 changes: 15 additions & 1 deletion src/setuptools_scm/_entrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ def _version_from_entrypoints(

try:
from importlib.metadata import entry_points # type: ignore
from importlib.metadata import EntryPoint
except ImportError:
try:
from importlib_metadata import entry_points
from importlib_metadata import EntryPoint
except ImportError:
from collections import defaultdict

Expand All @@ -59,6 +61,10 @@ def entry_points() -> dict[str, list[_t.EntrypointProtocol]]:
)
return defaultdict(list)

class EntryPoint: # type: ignore
def __init__(self, *args: Any, **kwargs: Any):
pass # entry_points() already provides the warning


def iter_entry_points(
group: str, name: str | None = None
Expand All @@ -83,6 +89,13 @@ def _get_ep(group: str, name: str) -> Any | None:
return None


def _get_from_object_reference_str(path: str) -> Any | None:
try:
return EntryPoint(path, path, None).load()
except (AttributeError, ModuleNotFoundError):
return None


def _iter_version_schemes(
entrypoint: str,
scheme_value: _t.VERSION_SCHEMES,
Expand All @@ -93,7 +106,8 @@ def _iter_version_schemes(
if isinstance(scheme_value, str):
scheme_value = cast(
"_t.VERSION_SCHEMES",
_get_ep(entrypoint, scheme_value),
_get_ep(entrypoint, scheme_value)
or _get_from_object_reference_str(scheme_value),
)

if isinstance(scheme_value, (list, tuple)):
Expand Down
10 changes: 10 additions & 0 deletions testing/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ def test_format_version_schemes() -> None:
)


def test_custom_version_schemes() -> None:
version = meta("1.0", config=c)
custom_computed = format_version(
version,
local_scheme="no-local-version",
version_scheme="setuptools_scm.version:no_guess_dev_version",
)
assert custom_computed == no_guess_dev_version(version)


def date_to_str(
date_: date | None = None,
days_offset: int = 0,
Expand Down