From c47bd81681fd36b1858286f9fc2dd0d77554cc89 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 21 Jul 2024 14:41:49 +0200 Subject: [PATCH] Avoid reading the wrong version. This was causing builds to fail on Read the Docs since sphinx-autobuild added websockets as a dependency. --- src/websockets/version.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/websockets/version.py b/src/websockets/version.py index 145c7a9e..46ae34a4 100644 --- a/src/websockets/version.py +++ b/src/websockets/version.py @@ -34,8 +34,20 @@ def get_version(tag: str) -> str: file_path = pathlib.Path(__file__) root_dir = file_path.parents[0 if file_path.name == "setup.py" else 2] - # Read version from git if available. This prevents reading stale - # information from src/websockets.egg-info after building a sdist. + # Read version from package metadata if it is installed. + try: + version = importlib.metadata.version("websockets") + except ImportError: + pass + else: + # Check that this file belongs to the installed package. + files = importlib.metadata.files("websockets") + if files: + version_file = [f for f in files if f.name == file_path.name][0] + if version_file.locate() == file_path: + return version + + # Read version from git if available. try: description = subprocess.run( ["git", "describe", "--dirty", "--tags", "--long"], @@ -61,12 +73,6 @@ def get_version(tag: str) -> str: remainder = remainder.replace("-", ".") # required by PEP 440 return f"{tag}.dev{distance}+{remainder}" - # Read version from package metadata if it is installed. - try: - return importlib.metadata.version("websockets") - except ImportError: - pass - # Avoid crashing if the development version cannot be determined. return f"{tag}.dev0+gunknown"