Skip to content

Commit efaddd6

Browse files
committed
docs: clarify usage of version files
I found the current discussion around version files somewhat confusing. When you're reading the usage guide, the second point is telling you to make a version file; strongly implying you should. But in reality, you most likely do not need to bother with keeping a specific version file. Explain more clearly about the implications of this. Under this is a section on using `python -m setuptools_scm --help` which seems like an orphan from the "as a cli" section; move it down there. The runtime section starts by telling you what is strongly discouraged without explaination why. Make it clearer by giving the first example as the most supported thing, which is using standard importlib; then briefly discuss the version file from above, explaining what's in it for a standard Python template, then the strongly discouraged direct import method. In the config doc, update to discuss `importlib` for consistency. Correct some capitalization and other minor formatting bits while we're here.
1 parent 6eb52e6 commit efaddd6

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed

docs/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Callables or other Python objects have to be passed in `setup.py` (via the `use_
2121
`version_file: Path | PathLike[str] | None = None`
2222
: A path to a file that gets replaced with a file containing the current
2323
version. It is ideal for creating a ``_version.py`` file within the
24-
package, typically used to avoid using `pkg_resources.get_distribution`
24+
package, typically used to avoid using `importlib.metadata`
2525
(which adds some overhead).
2626

2727
!!! warning ""

docs/usage.md

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Usage
22

3-
## at build time
3+
## At build time
44

55
The preferred way to configure `setuptools_scm` is to author
66
settings in the `tool.setuptools_scm` section of `pyproject.toml`.
@@ -25,7 +25,9 @@ that support PEP 518 ([pip](https://pypi.org/project/pip) and
2525
[pep517](https://pypi.org/project/pep517/)).
2626
Tools that still invoke `setup.py` must ensure build requirements are installed
2727

28-
### version files
28+
### Version files
29+
30+
Version files can be created with the ``version_file`` directive.
2931

3032
```toml title="pyproject.toml"
3133
...
@@ -34,15 +36,13 @@ version_file = "pkg/_version.py"
3436
```
3537
Where ``pkg`` is the name of your package.
3638

39+
Unless the small overhead of introspecting the version at runtime via
40+
`importlib.metadata` is a concern or you need a version file in an
41+
alternative format such as plain-text (see ``version_file_template``)
42+
you most likely do _not_ need to write a separate version file; see
43+
the runtime discussion below for more details.
3744

38-
```commandline
39-
$ python -m setuptools_scm
40-
41-
# To explore other options, try:
42-
$ python -m setuptools_scm --help
43-
```
44-
45-
## as cli tool
45+
## As cli tool
4646

4747
If you need to confirm which version string is being generated
4848
or debug the configuration, you can install
@@ -65,46 +65,32 @@ $ python -m setuptools_scm ls # output trimmed for brevity
6565
...
6666
```
6767

68-
!!! note "committed files only"
68+
!!! note "Committed files only"
6969

7070
currently only committed files are listed, this might change in the future
7171

7272
!!! warning "sdists/archives don't provide file lists"
7373

74-
currently there is no builtin mechanism
75-
to safely transfer the file lists to sdists or obtaining them from archives
76-
coordination for setuptools and hatch is ongoing
74+
Currently there is no builtin mechanism
75+
to safely transfer the file lists to sdists or obtaining them from archives.
76+
Coordination for setuptools and hatch is ongoing.
7777

78-
## at runtime (strongly discouraged)
79-
80-
the most simple **looking** way to use `setuptools_scm` at runtime is:
81-
82-
```python
83-
from setuptools_scm import get_version
84-
version = get_version()
85-
```
78+
To explore other options, try
8679

87-
88-
In order to use `setuptools_scm` from code that is one directory deeper
89-
than the project's root, you can use:
90-
91-
```python
92-
from setuptools_scm import get_version
93-
version = get_version(root='..', relative_to=__file__)
80+
```commandline
81+
$ python -m setuptools_scm --help
9482
```
9583

84+
## At runtime
9685

97-
## Python package metadata
98-
99-
86+
### Python Metadata
10087

101-
102-
### version at runtime
103-
104-
If you have opted not to hardcode the version number inside the package,
105-
you can retrieve it at runtime from [PEP-0566](https://www.python.org/dev/peps/pep-0566/) metadata using
88+
The standard method to retrieve the version number at runtime is via
89+
[PEP-0566](https://www.python.org/dev/peps/pep-0566/) metadata using
10690
``importlib.metadata`` from the standard library (added in Python 3.8)
107-
or the [`importlib_metadata`](https://pypi.org/project/importlib-metadata/) backport:
91+
or the
92+
[`importlib_metadata`](https://pypi.org/project/importlib-metadata/)
93+
backport for earlier versions:
10894

10995
```python title="package_name/__init__.py"
11096
from importlib.metadata import version, PackageNotFoundError
@@ -116,6 +102,40 @@ except PackageNotFoundError:
116102
pass
117103
```
118104

105+
### Via your version file
106+
107+
If you have opted to create a Python version file via the standard
108+
template, you can import that file, where you will have a ``version``
109+
string and a ``version_tuple`` tuple with elements corresponding to
110+
the version tags.
111+
112+
```python title="Using package_name/_version.py"
113+
import package_name._version as v
114+
115+
print(v.version)
116+
print(v.version_tuple)
117+
```
118+
119+
### Via setuptools_scm (strongly discouraged)
120+
121+
While the most simple **looking** way to use `setuptools_scm` at
122+
runtime is:
123+
124+
```python
125+
from setuptools_scm import get_version
126+
version = get_version()
127+
```
128+
129+
it is strongly discouraged to call directly into `setuptools_scm` over
130+
the standard Python `importlib.metadata`.
131+
132+
In order to use `setuptools_scm` from code that is one directory deeper
133+
than the project's root, you can use:
134+
135+
```python
136+
from setuptools_scm import get_version
137+
version = get_version(root='..', relative_to=__file__)
138+
```
119139

120140
### Usage from Sphinx
121141

@@ -132,7 +152,7 @@ the working directory for good reasons and using the installed metadata
132152
prevents using needless volatile data there.
133153

134154

135-
## with Docker/Podman
155+
### With Docker/Podman
136156

137157

138158
In some situations, Docker may not copy the `.git` into the container when

0 commit comments

Comments
 (0)