Skip to content

Commit be52811

Browse files
anntzertacaswell
authored andcommitted
Switch to setuptools_scm.
A few noteworthy points: - The contents in the sdist now exactly match what `git archive` would include; this avoids having to additionally keep track of things in MANIFEST.in (as noted in contributing.rst). - The `__version__` of an editable install is no longer recomputed at each import (as was done with `versioneer`, but only whenever the project is (re)installed; this can actually save quite a bit of time when working with many editable installs as each version recomputation requires shelling out a subprocess. (If really wanted we could keep the old behavior by optionally adding a runtime dependency on setuptools_scm and calling `setuptools_scm.get_version` in `matplotlib/__init__.py`.)
1 parent 9c98ab0 commit be52811

15 files changed

+61
-2223
lines changed

.flake8

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ exclude =
3535
doc/gallery
3636
doc/tutorials
3737
# External files.
38-
versioneer.py
3938
tools/gh_api.py
4039
tools/github_stats.py
4140
.tox

.git_archival.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref-names: $Format:%D$

.gitattributes

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
*.ppm binary
33
*.svg binary
44
*.svg linguist-language=true
5-
lib/matplotlib/_version.py export-subst
5+
.git_archival.txt export-subst

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ jobs:
141141
142142
# Install dependencies from PyPI.
143143
python -mpip install --upgrade $PRE \
144-
cycler kiwisolver numpy pillow pyparsing python-dateutil \
144+
cycler kiwisolver numpy pillow pyparsing python-dateutil setuptools-scm \
145145
-r requirements/testing/all.txt \
146146
${{ matrix.extra-requirements }}
147147

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pip-wheel-metadata/*
4242
# tox testing tool
4343
.tox
4444
setup.cfg
45+
# generated by setuptools_scm
46+
lib/matplotlib/_version.py
4547

4648
# OS generated files #
4749
######################

MANIFEST.in

-18
This file was deleted.

doc/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _check_dependencies():
198198
try:
199199
SHA = subprocess.check_output(
200200
['git', 'describe', '--dirty']).decode('utf-8').strip()
201-
# Catch the case where git is not installed locally, and use the versioneer
201+
# Catch the case where git is not installed locally, and use the setuptools_scm
202202
# version number instead
203203
except (subprocess.CalledProcessError, FileNotFoundError):
204204
SHA = matplotlib.__version__

doc/devel/contributing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ New modules and files: installation
322322

323323
* If you have added new files or directories, or reorganized existing
324324
ones, make sure the new files are included in the match patterns in
325-
:file:`MANIFEST.in`, and/or in *package_data* in :file:`setup.py`.
325+
in *package_data* in :file:`setupext.py`.
326326

327327
C/C++ extensions
328328
----------------

doc/devel/release_guide.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,12 @@ Finally, push the tag to GitHub::
176176

177177
Congratulations, the scariest part is done!
178178

179-
.. [#] The tarball that is provided by GitHub is produced using `git
180-
archive <https://git-scm.com/docs/git-archive>`__. We use
181-
`versioneer <https://github.com/warner/python-versioneer>`__
182-
which uses a format string in
179+
.. [#] The tarball that is provided by GitHub is produced using `git archive`_.
180+
We use setuptools_scm_ which uses a format string in
183181
:file:`lib/matplotlib/_version.py` to have ``git`` insert a
184182
list of references to exported commit (see
185183
:file:`.gitattributes` for the configuration). This string is
186-
then used by ``versioneer`` to produce the correct version,
184+
then used by ``setuptools_scm`` to produce the correct version,
187185
based on the git tag, when users install from the tarball.
188186
However, if there is a branch pointed at the tagged commit,
189187
then the branch name will also be included in the tarball.
@@ -195,6 +193,8 @@ Congratulations, the scariest part is done!
195193
196194
git archive v2.0.0 -o matplotlib-2.0.0.tar.gz --prefix=matplotlib-2.0.0/
197195
196+
.. _git archive: https://git-scm.com/docs/git-archive
197+
.. _setuptools_scm: https://github.com/pypa/setuptools_scm
198198

199199
If this is a final release, also create a 'doc' branch (this is not
200200
done for pre-releases)::

lib/matplotlib/__init__.py

+29-10
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,15 @@
102102
import tempfile
103103
import warnings
104104

105+
import numpy
106+
105107
# cbook must import matplotlib only within function
106108
# definitions, so it is safe to import from it here.
107-
from . import _api, cbook, docstring, rcsetup
109+
from . import _api, _version, cbook, docstring, rcsetup
108110
from matplotlib.cbook import MatplotlibDeprecationWarning, sanitize_sequence
109111
from matplotlib.cbook import mplDeprecation # deprecated
110112
from matplotlib.rcsetup import validate_backend, cycler
111113

112-
import numpy
113-
114-
# Get the version from the _version.py versioneer file. For a git checkout,
115-
# this is computed based on the number of commits since the last tag.
116-
from ._version import get_versions
117-
__version__ = str(get_versions()['version'])
118-
del get_versions
119114

120115
_log = logging.getLogger(__name__)
121116

@@ -135,6 +130,27 @@
135130
}"""
136131

137132

133+
def __getattr__(name):
134+
if name == "__version__":
135+
import setuptools_scm
136+
global __version__ # cache it.
137+
# Only shell out to a git subprocess if really needed, and not on a
138+
# shallow clone, such as those used by CI, as the latter would trigger
139+
# a warning from setuptools_scm.
140+
root = Path(__file__).resolve().parents[2]
141+
if (root / ".git").exists() and not (root / ".git/shallow").exists():
142+
__version__ = setuptools_scm.get_version(
143+
root=root,
144+
version_scheme="post-release",
145+
local_scheme="node-and-date",
146+
fallback_version=_version.version,
147+
)
148+
else: # Get the version from the _version.py setuptools_scm file.
149+
__version__ = _version.version
150+
return __version__
151+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
152+
153+
138154
def _check_versions():
139155

140156
# Quickfix to ensure Microsoft Visual C++ redistributable
@@ -724,6 +740,7 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
724740
fail_on_error : bool, default: False
725741
Whether invalid entries should result in an exception or a warning.
726742
"""
743+
import matplotlib as mpl
727744
rc_temp = {}
728745
with _open_file_or_url(fname) as fd:
729746
try:
@@ -770,7 +787,10 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
770787
version, name=key, alternative=alt_key, obj_type='rcparam',
771788
addendum="Please update your matplotlibrc.")
772789
else:
773-
version = 'master' if '.post' in __version__ else f'v{__version__}'
790+
# __version__ must be looked up as an attribute to trigger the
791+
# module-level __getattr__.
792+
version = ('master' if '.post' in mpl.__version__
793+
else f'v{mpl.__version__}')
774794
_log.warning("""
775795
Bad key %(key)s in file %(fname)s, line %(line_no)s (%(line)r)
776796
You probably need to get an updated matplotlibrc file from
@@ -1385,7 +1405,6 @@ def inner(ax, *args, data=None, **kwargs):
13851405
return inner
13861406

13871407

1388-
_log.debug('matplotlib version %s', __version__)
13891408
_log.debug('interactive is %s', is_interactive())
13901409
_log.debug('platform is %s', sys.platform)
13911410
_log.debug('loaded modules: %s', list(sys.modules))

0 commit comments

Comments
 (0)