[QGIS-Developer] Python's "optimized mode" and type checks

Sebastian M. Ernst ernst at pleiszenburg.de
Wed Jul 15 09:01:31 PDT 2020


Hi all,

have there ever been considerations with respect to QGIS' Python
integration and Python's "optimized" mode [see 1, 2, 3]?

In a nutshell, the only real-world implication of this mode is the fact
the Python begins to ignore "assert" statements completely. They are not
anymore executed. Unfortunately, it has become a bit of a bad practice
to rely on "assert" statements - at least in certain projects (not QGIS).

Would it hurt anyone to run QGIS with Python's interpreter in "optimized
mode" by default? As far as the QGIS code base goes, I did not find
anything critical that could break. However, I am not entirely sure
about plugins and custom proprietary code in the QGIS ecosystem. From
experience, a lot of developers have never heard about this feature, so
it usually causes some surprises when tampered with for the first time.

Why? QGIS's Python code could become statically typed via Typeguard [4].
Typeguard offers full & in-depth type checks at run-time, which is
pretty amazing. The only downside is that it makes Python code run slow
like hell. On the upside, Typeguard deactivates itself if Python is
running in "optimized mode" - then, the execution speed is back to
normal. It could therefore become a debugging feature of QGIS that is by
default deactivated for normal "end users".

Best regards,
Sebastian


1: https://docs.python.org/3/using/cmdline.html#cmdoption-o
2: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONOPTIMIZE
3: https://docs.python.org/3/c-api/init.html#c.Py_OptimizeFlag
4: https://typeguard.readthedocs.io/en/latest/


More information about the QGIS-Developer mailing list