Skip to content

Commit 5ba072b

Browse files
committed
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 408b1ab commit 5ba072b

15 files changed

+61
-2229
lines changed

.flake8

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref-names: $Format:%D$

.gitattributes

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ jobs:
140140
141141
# Install dependencies from PyPI.
142142
python -mpip install --upgrade $PRE \
143-
cycler kiwisolver numpy pillow pyparsing python-dateutil \
143+
cycler kiwisolver numpy pillow pyparsing python-dateutil setuptools-scm \
144144
-r requirements/testing/travis_all.txt \
145145
${{ matrix.extra-requirements }}
146146

.gitignore

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 24 deletions
This file was deleted.

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def _check_dependencies():
196196
try:
197197
SHA = subprocess.check_output(
198198
['git', 'describe', '--dirty']).decode('utf-8').strip()
199-
# Catch the case where git is not installed locally, and use the versioneer
199+
# Catch the case where git is not installed locally, and use the setuptools_scm
200200
# version number instead
201201
except (subprocess.CalledProcessError, FileNotFoundError):
202202
SHA = matplotlib.__version__

doc/devel/contributing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ New modules and files: installation
323323

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

328328
C/C++ extensions
329329
----------------

doc/devel/release_guide.rst

Lines changed: 5 additions & 5 deletions
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

Lines changed: 29 additions & 10 deletions
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
@@ -717,6 +733,7 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
717733
fail_on_error : bool, default: False
718734
Whether invalid entries should result in an exception or a warning.
719735
"""
736+
import matplotlib as mpl
720737
rc_temp = {}
721738
with _open_file_or_url(fname) as fd:
722739
try:
@@ -763,7 +780,10 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
763780
version, name=key, alternative=alt_key, obj_type='rcparam',
764781
addendum="Please update your matplotlibrc.")
765782
else:
766-
version = 'master' if '.post' in __version__ else f'v{__version__}'
783+
# __version__ must be looked up as an attribute to trigger the
784+
# module-level __getattr__.
785+
version = ('master' if '.post' in mpl.__version__
786+
else f'v{mpl.__version__}')
767787
_log.warning("""
768788
Bad key %(key)s in file %(fname)s, line %(line_no)s (%(line)r)
769789
You probably need to get an updated matplotlibrc file from
@@ -1378,7 +1398,6 @@ def inner(ax, *args, data=None, **kwargs):
13781398
return inner
13791399

13801400

1381-
_log.debug('matplotlib version %s', __version__)
13821401
_log.debug('interactive is %s', is_interactive())
13831402
_log.debug('platform is %s', sys.platform)
13841403
_log.debug('loaded modules: %s', list(sys.modules))

0 commit comments

Comments
 (0)