Skip to content

version.py: add comment with workaround to run pytest from scratch.#925

Draft
marc-hb wants to merge 1 commit intozephyrproject-rtos:mainfrom
marc-hb:pkg-wa
Draft

version.py: add comment with workaround to run pytest from scratch.#925
marc-hb wants to merge 1 commit intozephyrproject-rtos:mainfrom
marc-hb:pkg-wa

Conversation

@marc-hb
Copy link
Collaborator

@marc-hb marc-hb commented Feb 20, 2026

importlib.metadata.version("west") requires a west package. After a surprisingly long time, I realized there's none when running pytest from scratch. That's because (un)installations seem to always leave one behind. So this "accidentally" tends to work. As a workaround, recommend the very well tried and tested tactic of... installing - and uninstalling.

cc: @thorsten-klein

importlib.metadata.version("west") requires a west package.
After a surprisingly long time, I realized there's none when
running pytest from scratch. That's because (un)installations seem to
always leave one behind. So this "accidentally" tends to work.
As a workaround, recommend the very well tried and tested tactic of...
installing - and uninstalling.
@marc-hb marc-hb marked this pull request as ready for review February 20, 2026 21:21
@codecov
Copy link

codecov bot commented Feb 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.95%. Comparing base (2d883de) to head (ea1bea9).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #925      +/-   ##
==========================================
- Coverage   86.01%   85.95%   -0.06%     
==========================================
  Files          11       11              
  Lines        3453     3453              
==========================================
- Hits         2970     2968       -2     
- Misses        483      485       +2     
Files with missing lines Coverage Δ
src/west/version.py 100.00% <ø> (ø)

... and 1 file with indirect coverage changes

@pdgendt
Copy link
Collaborator

pdgendt commented Feb 23, 2026

importlib.metadata.version("west") requires a west package. After a surprisingly long time, I realized there's none when running pytest from scratch.

How exactly do you get into this situation?

@marc-hb
Copy link
Collaborator Author

marc-hb commented Feb 23, 2026

How exactly do you get into this situation?

Easy:

pip uninstall west
pipx uninstall west
cd west && git clean -fdx # DANGEROUS! Alternative: rm -rf west/*egg*
pytest ...

@marc-hb
Copy link
Collaborator Author

marc-hb commented Feb 23, 2026

I realized there's none when running pytest from scratch.

Emphasis in case you missed this.

uv ... pytest ... does not have that problem because uv "installs" first. uv is great but it adds a layer of isolation which makes it hard to bisect cpython or ruamel (#908), or to run tests with WINE (#923), or debug issues in general.

uv isolation is either a blessing or a curse depending on what you're doing.

"All problems in computer science can be solved by another level of indirection."

"Except for too many layers of indirection!"

@thorsten-klein
Copy link
Contributor

What about maintaining the version in the python code and setuptools will read it from there?

 pyproject.toml      | 5 ++++-
 src/west/version.py | 4 +---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/pyproject.toml b/pyproject.toml
index a4b12b5..c12af43 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
 
 [project]
 name = "west"
-version = "1.5.99"
+dynamic = ["version"]
 authors = [{name = "Zephyr Project", email = "devel@lists.zephyrproject.org"}]
 description = "Zephyr RTOS Project meta-tool"
 classifiers = [
@@ -62,6 +62,9 @@ package-dir = {"" = "src"}
 zip-safe = false
 include-package-data = true
 
+[tool.setuptools.dynamic]
+version = {attr = "west.version.__version__"}
+
 [tool.setuptools.packages.find]
 where = ["src"]
 namespaces = false
diff --git a/src/west/version.py b/src/west/version.py
index b6ddd08..6043d6d 100644
--- a/src/west/version.py
+++ b/src/west/version.py
@@ -5,9 +5,7 @@
 # This is the Python 3 version of option 3 in:
 # https://packaging.python.org/guides/single-sourcing-package-version/#single-sourcing-the-version
 
-import importlib.metadata
-
-__version__ = importlib.metadata.version("west")
+__version__ = "1.5.99"
 #
 # MAINTAINERS:
 #

@marc-hb
Copy link
Collaborator Author

marc-hb commented Feb 23, 2026

What about maintaining the version in the python code and setuptools will read it from there?

I'm really not a packaging expert but LGTM. In fact, having the version in a file called version.py looks even better: ever "closer" to the versioned source itself and "package-technology-independent". This unsurprisingly solves the issue of running pytest without a package!

What is the "best practice" for this? Assuming there is one...

@marc-hb marc-hb marked this pull request as draft February 23, 2026 22:17
@pdgendt
Copy link
Collaborator

pdgendt commented Feb 24, 2026

What is the "best practice" for this? Assuming there is one...

Clicking on the link in the version.py file leads to https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html#dynamic-metadata which describes the changes from @thorsten-klein

It would be a cleaner solution definitely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants