Mocking Python version for testing, use of Python Warning to raise error for specific Python Version

Mocking Python version for testing, use of Python Warning to raise error for specific Python Version
python
Published

November 16, 2022

Background

To release a new open bound version of library so it can be instsalled in any Python versio, while making sure that users are aware this is not supported yet. This article explains well Why setting upper bound for version is a bad idea because it is not supported yet . TL;DR, if you cannot install it, you cannot even test if it works or not. In Python ecosystem, you relies on many libraries and it will take long time until all depedencies are updated, which in reality most likely it has not break anything at all.

The Approach

We use warnings and simplefilter. To your surprise, warnings can be triggered as an Exception with the flag -W, i.e. python my_program.py -W UserWarnings or using the Python environment variable PYTHONWARNINGS

See the standard Python Docs about warnings.warn yourself > warnings.warn(message, category=None, stacklevel=1, source=None) Issue a warning, or maybe ignore it or raise an exception.

(TBD, I am still working on the solution but I just need to write it down to document my state of mind lol)