Skip to content

Commit b6672db

Browse files
whyvineetmhucka
andauthored
Refactor version retrieval in setup.py to read from __init__.py (#986)
Resolves #914 by having setup.py read the version number from `__init__.py`. --------- Co-authored-by: Michael Hucka <[email protected]>
1 parent 965e17d commit b6672db

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

release/setup.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,42 @@
2424
from many other contributors within Google.
2525
"""
2626

27+
import re
2728
import sys
2829
from datetime import date
30+
from pathlib import Path
2931

3032
from setuptools import find_packages, setup
3133
from setuptools.command.install import install
3234
from setuptools.dist import Distribution
3335

34-
CUR_VERSION = "0.7.7"
36+
37+
def read_version():
38+
"""Return the package version from tensorflow_quantum/__init__.py."""
39+
40+
# Need to account for 2 situations: when setup.py is copied to a build
41+
# directory, and when setup.py is in a 'release/' subdirectory.
42+
here = Path(__file__).resolve().parent
43+
possible_paths = [
44+
here / "tensorflow_quantum" / "__init__.py",
45+
here.parent / "tensorflow_quantum" / "__init__.py",
46+
]
47+
48+
for init_path in possible_paths:
49+
if init_path.is_file():
50+
content = init_path.read_text(encoding="utf-8")
51+
# Look for __version__ = 'X.Y.Z' at the start of a line.
52+
version_match = re.search(r'^__version__\s*=\s*[\'"]([^\'"]+)[\'"]',
53+
content, re.MULTILINE)
54+
if version_match:
55+
return version_match.group(1)
56+
57+
raise RuntimeError(
58+
"Could not find a valid __version__ definition. Checked:\n" +
59+
"\n".join(f" - {p}" for p in possible_paths))
60+
61+
62+
CUR_VERSION = read_version()
3563

3664
DOCLINES = __doc__.split("\n")
3765

0 commit comments

Comments
 (0)