Skip to content

Commit 42a389f

Browse files
committed
Avoid reading the wrong version.
This was causing builds to fail on Read the Docs since sphinx-autobuild added websockets as a dependency.
1 parent 6c248ff commit 42a389f

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/websockets/version.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,22 @@ def get_version(tag: str) -> str:
3434
file_path = pathlib.Path(__file__)
3535
root_dir = file_path.parents[0 if file_path.name == "setup.py" else 2]
3636

37-
# Read version from git if available. This prevents reading stale
38-
# information from src/websockets.egg-info after building a sdist.
37+
# Read version from package metadata if it is installed.
38+
try:
39+
version = importlib.metadata.version("websockets")
40+
except ImportError:
41+
pass
42+
else:
43+
# Check that this file belongs to the installed package.
44+
files = importlib.metadata.files("websockets")
45+
if files is not None:
46+
version_path = [p for p in files if p.name == file_path.name][
47+
0
48+
].locate()
49+
if version_path == file_path:
50+
return version
51+
52+
# Read version from git if available.
3953
try:
4054
description = subprocess.run(
4155
["git", "describe", "--dirty", "--tags", "--long"],
@@ -61,12 +75,6 @@ def get_version(tag: str) -> str:
6175
remainder = remainder.replace("-", ".") # required by PEP 440
6276
return f"{tag}.dev{distance}+{remainder}"
6377

64-
# Read version from package metadata if it is installed.
65-
try:
66-
return importlib.metadata.version("websockets")
67-
except ImportError:
68-
pass
69-
7078
# Avoid crashing if the development version cannot be determined.
7179
return f"{tag}.dev0+gunknown"
7280

0 commit comments

Comments
 (0)