Python 3.8.1 deprecation warning

If you look at the following code, there is a deprecation warning for:

colors.SetColor("BkgColor", [0.3, 0.6, 0.3, 1.0])

As you can see from the code, I tried several alternatives including vtk.vtkColor4ub and vtk.vtkColor4d. The thinking here was that I could use either of these to circumvent the deprecation warning. However vtk.vtkColor4d raises an ambiguous call error. Does anyone know why this happens? I can’t see anything obvious in the actual VTK code.

Code is here:

#!/usr/bin/python

import vtk


def main():
    colors = vtk.vtkNamedColors()

    # Set the background color.
    # For Python 3.8.1
    # These generate: DeprecationWarning: an integer is required (got type float)
    #   Implicit conversion to integers using __int__ is deprecated,
    #    and may be removed in a future version of Python.
    colors.SetColor("BkgColor", [0.3, 0.6, 0.3, 1.0])
    colors.SetColor("BkgColor", 0.3, 0.6, 0.3, 1.0)
    #  No deprecation message this is expected as the values are now integer
    colors.SetColor("BkgColor", [int(i * 255) for i in [0.3, 0.6, 0.3, 1.0]])
    # Try with vtk.vtkColor4ub or vtk.vtkColor4d
    x = vtk.vtkColor4ub([int(i * 255) for i in [0.3, 0.6, 0.3, 1.0]])
    colors.SetColor("BkgColor", x)
    #  Fails with: TypeError: ambiguous call, multiple overloaded methods match the arguments
    # colors.SetColor("BkgColor", vtk.vtkColor4d(0.3, 0.6, 0.3, 1.0))

    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)
    renderer.SetBackground(colors.GetColor3d("BkgColor"))

    renderWindowInteractor.Initialize()
    renderWindow.Render()
    renderWindowInteractor.Start()


if __name__ == "__main__":
    main()

@dgobbi

Are you sure the error is thrown by vtkColor4d constructor? vtkNamedColors::SetColor() has several overloads too, hence it can also be the source of ambiguity, especially with “free floating” square brackets (which iterable type is being infered?). I’ve seen people using, for example, List[] in place of [], to solve ambiguities.

I can fix the DeprecationWarning by adding an explicit check for the __int__() method to the wrapper code before it attempts the conversion.

The “ambiguous call” error also occurs with Python 3.7 and earlier versions. It shouldn’t happen, because vtkNamedColors has an an overload specifically for vtkColor4d. It might be a bug in the overload resolution code, I’ll investigate.

Thankyou for looking into this @dgobbi.

See !6379 for the DeprecationWarning fix.

I’ve found the reason for the ambiguity error, but will have to consider the best way to resolve it. The ambiguity is between these two methods:

void SetColor(const vtkStdString& name, const double rgba[4]);
void SetColor(const vtkStdString& name, const vtkColor4d& rgba);

It occurs because the Python-wrapped vtkColor4d is a Python sequence of 4 floats, and can therefore satisfy either of these two overloads. Of course, the vtkColor4d overload should take precedence here.