Skip to content

Commit aee4dc9

Browse files
committed
Ensure version inference falls back to PKG-INFO when building wheel from sdist
1 parent b2ed1e6 commit aee4dc9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ def _get_version() -> str:
3636
version = subprocess.check_output(cmd, encoding="utf-8")
3737
return _normalization.best_effort_version(version, "{safe}.dev+{sanitized}")
3838
except subprocess.CalledProcessError: # e.g.: git not installed or history missing
39+
if os.path.exists("PKG-INFO"): # building wheel from sdist
40+
with open("PKG-INFO", encoding="utf-8") as fp:
41+
if match := re.search(r"^Version: (\d+\.\d+\.\d+.*)$", fp.read(), re.M):
42+
return match[1]
3943
with open("NEWS.rst", encoding="utf-8") as fp:
40-
match = re.search(r"v\d+\.\d+.\d+", fp.read()) # latest version
44+
match = re.search(r"v\d+\.\d+\.\d+", fp.read()) # latest version
4145
return f"{match[0] if match else '0.0.0'}.dev+{time.strftime('%Y%m%d')}"
4246

4347

setuptools/tests/test_setuptools.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ def test_findall_missing_symlink(tmpdir):
265265
assert found == []
266266

267267

268+
def test_setuptools_own_dists_infers_version(setuptools_sdist, setuptools_wheel):
269+
# Sanity check
270+
# Validates that `setup.py:_get_version` works as expected
271+
assert "0.0.0" not in setuptools_sdist.name
272+
# Validates that `setup.py:_get_version` guarantees the fallback
273+
# code path is finding something in PKG-INFO:
274+
assert "0.0.0" not in setuptools_wheel.name
275+
276+
268277
@pytest.mark.xfail(reason="unable to exclude tests; #4475 #3260")
269278
def test_its_own_wheel_does_not_contain_tests(setuptools_wheel):
270279
with ZipFile(setuptools_wheel) as zipfile:

0 commit comments

Comments
 (0)