[PyQt] Floats being accepted for int arguments

Phil Thompson phil at riverbankcomputing.com
Tue Nov 27 10:15:08 GMT 2018


On 27 Nov 2018, at 9:51 am, Florian Bruhin <me at the-compiler.org> wrote:
> 
> Hi,
> 
> turns out running mypy on a big PyQt codebase results in some things to
> talk about :D
> 
> Apparently, PyQt accepts floats where C++ takes ints and silently
> truncates them:
> 
>>>> from PyQt5.QtGui import QFont
>>>> f = QFont()
>>>> f.setWeight(5.8)
>>>> f.weight()
>  5
> 
> Similar Python methods usually accept floats if f.is_integer() is true,
> but raise a ValueError otherwise:

I disagree with "usually".

>>>> import math
>>>> math.factorial(5)
>  120
>>>> math.factorial(5.0)
>  120
>>>> math.factorial(5.5)
>  Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>  ValueError: factorial() only accepts integral values
> 
> Is that behavior intended?

It calls PyLong_AsLongLong(), so yes.

Phil


More information about the PyQt mailing list