|
102 | 102 | import tempfile |
103 | 103 | import warnings |
104 | 104 |
|
| 105 | +import numpy |
| 106 | + |
105 | 107 | # cbook must import matplotlib only within function |
106 | 108 | # 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 |
108 | 110 | from matplotlib.cbook import MatplotlibDeprecationWarning, sanitize_sequence |
109 | 111 | from matplotlib.cbook import mplDeprecation # deprecated |
110 | 112 | from matplotlib.rcsetup import validate_backend, cycler |
111 | 113 |
|
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 |
119 | 114 |
|
120 | 115 | _log = logging.getLogger(__name__) |
121 | 116 |
|
|
135 | 130 | }""" |
136 | 131 |
|
137 | 132 |
|
| 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 | + |
138 | 154 | def _check_versions(): |
139 | 155 |
|
140 | 156 | # 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): |
724 | 740 | fail_on_error : bool, default: False |
725 | 741 | Whether invalid entries should result in an exception or a warning. |
726 | 742 | """ |
| 743 | + import matplotlib as mpl |
727 | 744 | rc_temp = {} |
728 | 745 | with _open_file_or_url(fname) as fd: |
729 | 746 | try: |
@@ -770,7 +787,10 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False): |
770 | 787 | version, name=key, alternative=alt_key, obj_type='rcparam', |
771 | 788 | addendum="Please update your matplotlibrc.") |
772 | 789 | 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__}') |
774 | 794 | _log.warning(""" |
775 | 795 | Bad key %(key)s in file %(fname)s, line %(line_no)s (%(line)r) |
776 | 796 | You probably need to get an updated matplotlibrc file from |
@@ -1385,7 +1405,6 @@ def inner(ax, *args, data=None, **kwargs): |
1385 | 1405 | return inner |
1386 | 1406 |
|
1387 | 1407 |
|
1388 | | -_log.debug('matplotlib version %s', __version__) |
1389 | 1408 | _log.debug('interactive is %s', is_interactive()) |
1390 | 1409 | _log.debug('platform is %s', sys.platform) |
1391 | 1410 | _log.debug('loaded modules: %s', list(sys.modules)) |
0 commit comments