Skip to content

fix: refuse bw2data 4 next to bw2io < 0.9 before opening a project - #1

Merged
lbougan merged 1 commit into
mainfrom
fix/bw2io-preflight
Sep 28, 2026
Merged

lbougan merged 1 commit into
mainfrom
fix/bw2io-preflight

Conversation

@lbougan

@lbougan lbougan commented Sep 28, 2026

Copy link
Copy Markdown
Member

Summary

A user running uv run sentier-brightway db --project X with Python 3.11, bw2data 4.7 and bw2io 0.8.12 got:

BW2 = bd.__version__ < (4,)
TypeError: '<' not supported between instances of 'str' and 'tuple'

The line is in bw2io 0.8.12 (remote.py), not here. bw2io 0.8.x expects the tuple __version__ of legacy bw2data; against bw2data 4 it fails at import time. bw2data 4 reaches it on its own: opening a project runs the automatic update fix_migrations_filename, which does import bw2io. The update fires on projects last touched by an older bw2data, so an existing Activity Browser project triggers it while a fresh one (our tests) does not.

This PR adds sentier_brightway.preflight:

  • check_environment(bd) reads the installed bw2io version from package metadata (never imports it) and raises IncompatibleEnvironmentError when bw2data >= 4 sits next to bw2io < 0.9. The message names the fix: pip install -U "bw2io>=0.9.3".
  • writer.write() calls it before projects.set_current, so no project is created.
  • README: one paragraph under "Brightway project" on the two supported generations and the refused mix.

Legacy stacks (bw2data 3.x + bw2io 0.8.x, stock Activity Browser 2.11) and bw2data 4 + bw2io 0.9 are untouched. bw2io absent is fine too, bw2data only warns then.

Test plan

  • tests/test_preflight.py: 6 unit cases (string/tuple versions, 4.7.dev1, absent bw2io, legacy stack, bw2io 0.9.3/0.9.17)
  • tests/test_writer.py: the guard fires before set_current, project never created, fix named
  • uv run --extra testing pytest : 295 passed
  • uv run --extra dev pre-commit run --all-files : clean
  • Reproduced the original crash in a scratch venv (bw2data 4.7 + bw2io 0.8.12, existing project) and confirmed bw2io 0.9.17 imports cleanly next to bw2data 4.7

bw2data 4 applies automatic project updates on set_current, and one of
them (fix_migrations_filename) imports bw2io. bw2io 0.8.x was written for
legacy bw2data and dies at import against bw2data 4 with

    BW2 = bd.__version__ < (4,)
    TypeError: '<' not supported between instances of 'str' and 'tuple'

A user hit this running `sentier-brightway db --project X` with bw2data
4.7 and bw2io 0.8.12 on an existing project. The mix is common in
Activity Browser environments where bw2data was upgraded on its own.

New preflight.check_environment() reads the installed bw2io version from
package metadata (never imports it) and raises IncompatibleEnvironmentError
naming the fix, pip install -U "bw2io>=0.9.3". writer.write() calls it
before set_current, so nothing is created. Six unit tests cover string
and tuple versions, dev suffixes, absent bw2io and the legacy stack; a
writer test asserts the project is never created. README documents the
two supported generations and the refused mix.
@lbougan lbougan self-assigned this Sep 28, 2026
@lbougan
lbougan merged commit 1711750 into main Sep 28, 2026
4 checks passed
@lbougan
lbougan deleted the fix/bw2io-preflight branch September 28, 2026 11:13
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.

1 participant