Skip to content

Commit eaa64c0

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 41c42b8 commit eaa64c0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/websockets/version.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,20 @@ 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:
46+
version_file = [f for f in files if f.name == file_path.name][0]
47+
if version_file.locate() == file_path:
48+
return version
49+
50+
# Read version from git if available.
3951
try:
4052
description = subprocess.run(
4153
["git", "describe", "--dirty", "--tags", "--long"],
@@ -61,12 +73,6 @@ def get_version(tag: str) -> str:
6173
remainder = remainder.replace("-", ".") # required by PEP 440
6274
return f"{tag}.dev{distance}+{remainder}"
6375

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

0 commit comments

Comments
 (0)