Search code examples
pythonpython-3.xwarningscythonsuppress-warnings

Suppress warnings from Cython


I cythonize *.pyx files manually using

cython -3 -Wextra mymodule.pyx

I use the -Wextra option to generate additional warnings, useful for cleaning up redundant code pieces. However, many warnings of the form

warning: mymodule.pyx:123:45: local variable 'x' might be referenced before assignment

are printed, which I do not care for. I understand why it is not obvious from the perspective of the compiler, but under no circumstance is it ever possible for x to not be assigned before referencing, in my particular situation.

I would therefore like to keep using -Wextra but filter out this type of warning, similar to the -Wno option of gcc. I have however not been able to track down such a feature.


Solution

  • Warnings in cython are controlled via compiler directives; they only seem to be partially documented currently, but you can see the full list in the Cython source.

    In this case you want warn.maybe_uninitialized, passed to the --directive option.

    $ cython test.pyx -Wextra
    warning: test.pyx:7:12: local variable 'x' might be referenced before assignment
    
    $ cython test.pyx -Wextra --directive warn.maybe_uninitialized=False
    # no warning