Show HN: Pyvers – Python library for handling BCB changes and multiple back ends

1 vmoens 0 6/9/2025, 12:18:46 AM github.com ↗
pyvers is a lightweight Python library that lets you write version-specific implementations of functions to handle breaking changes in dependencies or switch between different backends (e.g., CPU/GPU, numpy/jax/torch) without cluttering your code with conditionals.

Example use case: NumPy 2.0 changed bool8 to bool_. With pyvers, you can handle both versions elegantly:

```python @implement_for("numpy", from_version=None, to_version="2.0.0") def create_mask(arr): np = get_backend("numpy") return np.array([x > 0 for x in arr], dtype=np.bool8)

@implement_for("numpy", from_version="2.0.0") def create_mask(arr): np = get_backend("numpy") return np.array([x > 0 for x in arr], dtype=np.bool_) ```

The README.md gives you a more complete example, including using Jax as a backend.

Other real-world examples: - Switching between SciPy (CPU) and CuPy (GPU) sparse matrix operations - Using various libraries for linalg depending on resources: torch, numpy, jax - Handling gym → gymnasium breaking changes in RL libraries

The library is battle-tested: we've been using it in production in several robotic projects to handle BC-breaking changes in our dependencies for the past four years.

GitHub: https://github.com/vmoens/pyvers PyPI: `pip install pyvers`

Comments (0)

No comments yet