hatch-env-plus 0.1.0__tar.gz → 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hatch-env-plus
3
- Version: 0.1.0
3
+ Version: 1.0.1
4
4
  Summary: A version source for Hatchling that reads the version from a given environment variable, but also allows for a fallback if the variable is not populated.
5
5
  Project-URL: homepage, https://github.com/jenskeiner/hatch_env_plus/
6
6
  Project-URL: repository, https://github.com/jenskeiner/hatch_env_plus.git
@@ -27,8 +27,9 @@ Description-Content-Type: text/markdown
27
27
 
28
28
  # Hatchling Environment Variable Version Source
29
29
 
30
- A Hatchling version source plugin that reads the version from an environment variable with a configurable fallback value,
31
- since the built-in environment variable version source does not allow to define a fallback.
30
+ A [Hatchling](https://github.com/pypa/hatch) version source plugin that reads the version from an environment variable
31
+ with a configurable fallback value, since the built-in [`env`](https://hatch.pypa.io/latest/plugins/version-source/env/)
32
+ version source does not allow to define a fallback.
32
33
 
33
34
  ## Installation
34
35
 
@@ -57,13 +58,18 @@ Then, configure the version source:
57
58
  [tool.hatch.version]
58
59
  source = "env-plus"
59
60
  variable = "PACKAGE_VERSION" # optional, default shown
60
- fallback = "0.0.0dev0" # optional, default shown
61
+ fallback = "0.0.0dev0" # optional, default shown
61
62
  ```
62
63
 
63
- Use the `variable` field to set the environment variable to use and set `fallback` to the desired fallback version.
64
+ Use the `variable` field to set the environment variable to use, and set `fallback` to the desired fallback version.
64
65
 
65
- **Note: An empty version string is treated as undefined. Therefore, this will throw an error if the configured environment
66
- variable as well as the fallback are both empty.**
66
+ **Note: An empty string, whether from the configured environment variable or from the fallback, is an
67
+ undefined version.**
68
+
69
+ If you explicitly set the configured environment variable to the empty string, your build will fail as no valid version
70
+ is set. If you explicitly set `fallback` to an empty string, your build will fail when the configured environment
71
+ variable is not set. This is the default behavior of [Hatchling]([Hatchling](https://github.com/pypa/hatch))'s built-in
72
+ enviroment variable version source [`env`](https://hatch.pypa.io/latest/plugins/version-source/env/).
67
73
 
68
74
  ## Usage
69
75
 
@@ -0,0 +1,61 @@
1
+ # Hatchling Environment Variable Version Source
2
+
3
+ A [Hatchling](https://github.com/pypa/hatch) version source plugin that reads the version from an environment variable
4
+ with a configurable fallback value, since the built-in [`env`](https://hatch.pypa.io/latest/plugins/version-source/env/)
5
+ version source does not allow to define a fallback.
6
+
7
+ ## Installation
8
+
9
+ Add the package as a build dependency to your `pyproject.toml`:
10
+
11
+ ```toml
12
+ [build-system]
13
+ requires = ["hatchling", "hatch-env-plus"]
14
+ build-backend = "hatchling.build"
15
+ ```
16
+
17
+ ## Configuration
18
+
19
+ Configure a dynamic version source in the `project` section of `pyproject.toml`:
20
+
21
+ ```toml
22
+ [project]
23
+ dynamic = ["version"]
24
+ ```
25
+
26
+ Make sure you don't specify the `version` directly in the `project` section, or the dynamic version source may not be picked up.
27
+
28
+ Then, configure the version source:
29
+
30
+ ```toml
31
+ [tool.hatch.version]
32
+ source = "env-plus"
33
+ variable = "PACKAGE_VERSION" # optional, default shown
34
+ fallback = "0.0.0dev0" # optional, default shown
35
+ ```
36
+
37
+ Use the `variable` field to set the environment variable to use, and set `fallback` to the desired fallback version.
38
+
39
+ **Note: An empty string, whether from the configured environment variable or from the fallback, is an
40
+ undefined version.**
41
+
42
+ If you explicitly set the configured environment variable to the empty string, your build will fail as no valid version
43
+ is set. If you explicitly set `fallback` to an empty string, your build will fail when the configured environment
44
+ variable is not set. This is the default behavior of [Hatchling]([Hatchling](https://github.com/pypa/hatch))'s built-in
45
+ enviroment variable version source [`env`](https://hatch.pypa.io/latest/plugins/version-source/env/).
46
+
47
+ ## Usage
48
+
49
+ Build your project with the desired version by setting the environment variable:
50
+
51
+ ```bash
52
+ PACKAGE_VERSION=2.0.0 uv build
53
+ # Builds package version "2.0.0".
54
+ ```
55
+
56
+ Leave the environment variable empty to use the default:
57
+
58
+ ```bash
59
+ uv build
60
+ # Builds package version 0.0.0dev0 (or configured fallback).
61
+ ```
@@ -1,3 +1,7 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.21.1", "hatch-env-plus>=1.0.0"]
3
+ build-backend = "hatchling.build"
4
+
1
5
  [project]
2
6
  name = "hatch-env-plus"
3
7
  dynamic = ["version"]
@@ -24,8 +28,7 @@ classifiers = [
24
28
  "Framework :: Hatch",
25
29
  ]
26
30
  dependencies = [
27
- # Package dependencies go here, e.g.
28
- # "pandas>=1,<3",
31
+ # Package dependencies go here.
29
32
  ]
30
33
 
31
34
  [project.urls]
@@ -41,10 +44,6 @@ dev = [
41
44
  "pytest-cov>=7,<8",
42
45
  ]
43
46
 
44
- [build-system]
45
- requires = ["hatchling>=1.21.1"]
46
- build-backend = "hatchling.build"
47
-
48
47
  [tool.hatch.build.targets.sdist]
49
48
  include = [
50
49
  "src/hatch_env_plus/**",
@@ -60,8 +59,7 @@ sources = ["src"]
60
59
  only-include = ["src/hatch_env_plus"]
61
60
 
62
61
  [tool.hatch.version]
63
- source = "env"
64
- variable = "PACKAGE_VERSION"
62
+ source = "env-plus"
65
63
 
66
64
  [tool.pytest.ini_options]
67
65
  addopts = "--cov=hatch_env_plus --cov-report=term-missing --cov-report=html"
@@ -0,0 +1,16 @@
1
+ import os
2
+
3
+ from hatchling.version.source.plugin.interface import VersionSourceInterface
4
+
5
+
6
+ class EnvironmentVariableVersionSource(VersionSourceInterface):
7
+ PLUGIN_NAME = "env-plus"
8
+
9
+ def get_version_data(self) -> dict:
10
+ variable = self.config.get("variable", "PACKAGE_VERSION")
11
+ fallback = self.config.get("fallback", "0.0.0dev0") or None
12
+ version = os.environ.get(variable, None) or fallback
13
+ return {"version": version}
14
+
15
+ def set_version(self, version: str, version_data: dict) -> None:
16
+ version_data["version"] = version
@@ -0,0 +1,81 @@
1
+ import pytest
2
+
3
+ from hatchling.version.source.plugin.interface import VersionSourceInterface
4
+ from hatch_env_plus import EnvironmentVariableVersionSource
5
+
6
+
7
+ def test_plugin_is_version_source():
8
+ assert issubclass(EnvironmentVariableVersionSource, VersionSourceInterface)
9
+
10
+
11
+ def test_plugin_name():
12
+ assert EnvironmentVariableVersionSource.PLUGIN_NAME == "env-plus"
13
+
14
+
15
+ def test_version_from_env_var():
16
+ """Version is read from environment variable when set."""
17
+ plugin = EnvironmentVariableVersionSource("", {})
18
+ with pytest.MonkeyPatch().context() as m:
19
+ m.setenv("PACKAGE_VERSION", "2.0.0")
20
+ data = plugin.get_version_data()
21
+ assert data == {"version": "2.0.0"}
22
+
23
+
24
+ def test_fallback_when_env_var_not_set():
25
+ """Fallback value is used when environment variable is not set."""
26
+ plugin = EnvironmentVariableVersionSource("", {})
27
+ with pytest.MonkeyPatch().context() as m:
28
+ m.delenv("PACKAGE_VERSION", raising=False)
29
+ data = plugin.get_version_data()
30
+ assert data == {"version": "0.0.0dev0"}
31
+
32
+
33
+ def test_fallback_when_env_var_empty_string():
34
+ """Fallback value is used when environment variable is set to the empty string."""
35
+ plugin = EnvironmentVariableVersionSource("", {})
36
+ with pytest.MonkeyPatch().context() as m:
37
+ m.setenv("PACKAGE_VERSION", "")
38
+ data = plugin.get_version_data()
39
+ assert data == {"version": "0.0.0dev0"}
40
+
41
+
42
+ def test_custom_env_var_name():
43
+ """Custom environment variable name is respected."""
44
+ plugin = EnvironmentVariableVersionSource("", {"variable": "MY_VERSION"})
45
+ with pytest.MonkeyPatch().context() as m:
46
+ m.setenv("MY_VERSION", "3.5.0")
47
+ data = plugin.get_version_data()
48
+ assert data == {"version": "3.5.0"}
49
+
50
+
51
+ def test_custom_fallback_value():
52
+ """Custom fallback value is used when configured."""
53
+ plugin = EnvironmentVariableVersionSource("", {"fallback": "1.0.0"})
54
+ with pytest.MonkeyPatch().context() as m:
55
+ m.delenv("PACKAGE_VERSION", raising=False)
56
+ data = plugin.get_version_data()
57
+ assert data == {"version": "1.0.0"}
58
+
59
+
60
+ def test_env_var_takes_precedence_over_fallback():
61
+ """Environment variable takes precedence over fallback when both are set."""
62
+ plugin = EnvironmentVariableVersionSource(
63
+ "", {"variable": "CUSTOM_VER", "fallback": "0.1.0"}
64
+ )
65
+ with pytest.MonkeyPatch().context() as m:
66
+ # Not set - uses fallback
67
+ m.delenv("CUSTOM_VER", raising=False)
68
+ assert plugin.get_version_data() == {"version": "0.1.0"}
69
+
70
+ # Set - uses env var
71
+ m.setenv("CUSTOM_VER", "5.0.0")
72
+ assert plugin.get_version_data() == {"version": "5.0.0"}
73
+
74
+
75
+ def test_empty_fallback_string_treated_as_none():
76
+ """Empty fallback string is converted to None."""
77
+ plugin = EnvironmentVariableVersionSource("", {"fallback": ""})
78
+ with pytest.MonkeyPatch().context() as m:
79
+ m.delenv("PACKAGE_VERSION", raising=False)
80
+ data = plugin.get_version_data()
81
+ assert data == {"version": None}
@@ -1,55 +0,0 @@
1
- # Hatchling Environment Variable Version Source
2
-
3
- A Hatchling version source plugin that reads the version from an environment variable with a configurable fallback value,
4
- since the built-in environment variable version source does not allow to define a fallback.
5
-
6
- ## Installation
7
-
8
- Add the package as a build dependency to your `pyproject.toml`:
9
-
10
- ```toml
11
- [build-system]
12
- requires = ["hatchling", "hatch-env-plus"]
13
- build-backend = "hatchling.build"
14
- ```
15
-
16
- ## Configuration
17
-
18
- Configure a dynamic version source in the `project` section of `pyproject.toml`:
19
-
20
- ```toml
21
- [project]
22
- dynamic = ["version"]
23
- ```
24
-
25
- Make sure you don't specify the `version` directly in the `project` section, or the dynamic version source may not be picked up.
26
-
27
- Then, configure the version source:
28
-
29
- ```toml
30
- [tool.hatch.version]
31
- source = "env-plus"
32
- variable = "PACKAGE_VERSION" # optional, default shown
33
- fallback = "0.0.0dev0" # optional, default shown
34
- ```
35
-
36
- Use the `variable` field to set the environment variable to use and set `fallback` to the desired fallback version.
37
-
38
- **Note: An empty version string is treated as undefined. Therefore, this will throw an error if the configured environment
39
- variable as well as the fallback are both empty.**
40
-
41
- ## Usage
42
-
43
- Build your project with the desired version by setting the environment variable:
44
-
45
- ```bash
46
- PACKAGE_VERSION=2.0.0 uv build
47
- # Builds package version "2.0.0".
48
- ```
49
-
50
- Leave the environment variable empty to use the default:
51
-
52
- ```bash
53
- uv build
54
- # Builds package version 0.0.0dev0 (or configured fallback).
55
- ```
@@ -1,16 +0,0 @@
1
- import os
2
-
3
- from hatchling.version.source.plugin.interface import VersionSourceInterface
4
-
5
-
6
- class EnvironmentVariableVersionSource(VersionSourceInterface):
7
- PLUGIN_NAME = 'env-plus'
8
-
9
- def get_version_data(self) -> dict:
10
- variable = self.config.get('variable', 'PACKAGE_VERSION')
11
- fallback = self.config.get('fallback', '0.0.0dev0')
12
- version = os.environ.get(variable, fallback)
13
- return {'version': version}
14
-
15
- def set_version(self, version: str, version_data: dict) -> None:
16
- version_data['version'] = version
@@ -1,65 +0,0 @@
1
- import os
2
-
3
- import pytest
4
-
5
- from hatchling.version.source.plugin.interface import VersionSourceInterface
6
- from hatch_env_plus import EnvironmentVariableVersionSource
7
-
8
-
9
- def test_plugin_is_version_source():
10
- assert issubclass(EnvironmentVariableVersionSource, VersionSourceInterface)
11
-
12
-
13
- def test_plugin_name():
14
- assert EnvironmentVariableVersionSource.PLUGIN_NAME == 'env-plus'
15
-
16
-
17
- def test_version_from_env_var():
18
- """Version is read from environment variable when set."""
19
- plugin = EnvironmentVariableVersionSource('', {})
20
- with pytest.MonkeyPatch().context() as m:
21
- m.setenv('PACKAGE_VERSION', '2.0.0')
22
- data = plugin.get_version_data()
23
- assert data == {'version': '2.0.0'}
24
-
25
-
26
- def test_fallback_when_env_var_not_set():
27
- """Fallback value is used when environment variable is not set."""
28
- plugin = EnvironmentVariableVersionSource('', {})
29
- with pytest.MonkeyPatch().context() as m:
30
- m.delenv('PACKAGE_VERSION', raising=False)
31
- data = plugin.get_version_data()
32
- assert data == {'version': '0.0.0dev0'}
33
-
34
-
35
- def test_custom_env_var_name():
36
- """Custom environment variable name is respected."""
37
- plugin = EnvironmentVariableVersionSource('', {'variable': 'MY_VERSION'})
38
- with pytest.MonkeyPatch().context() as m:
39
- m.setenv('MY_VERSION', '3.5.0')
40
- data = plugin.get_version_data()
41
- assert data == {'version': '3.5.0'}
42
-
43
-
44
- def test_custom_fallback_value():
45
- """Custom fallback value is used when configured."""
46
- plugin = EnvironmentVariableVersionSource('', {'fallback': '1.0.0'})
47
- with pytest.MonkeyPatch().context() as m:
48
- m.delenv('PACKAGE_VERSION', raising=False)
49
- data = plugin.get_version_data()
50
- assert data == {'version': '1.0.0'}
51
-
52
-
53
- def test_env_var_takes_precedence_over_fallback():
54
- """Environment variable takes precedence over fallback when both are set."""
55
- plugin = EnvironmentVariableVersionSource(
56
- '', {'variable': 'CUSTOM_VER', 'fallback': '0.1.0'}
57
- )
58
- with pytest.MonkeyPatch().context() as m:
59
- # Not set - uses fallback
60
- m.delenv('CUSTOM_VER', raising=False)
61
- assert plugin.get_version_data() == {'version': '0.1.0'}
62
-
63
- # Set - uses env var
64
- m.setenv('CUSTOM_VER', '5.0.0')
65
- assert plugin.get_version_data() == {'version': '5.0.0'}
File without changes