Type hints: Operators declared outside class

Phil Thompson phil at riverbankcomputing.com
Wed Jul 12 12:11:17 BST 2023


On 08/07/2023 23:38, Florian Bruhin wrote:
> Hey,
> 
> If an operator is designed outside a class, such as e.g. in
> QtCore/qsize.sip:
> 
>     QSize operator+(const QSize &s1, const QSize &s2);
> 
> in the type annotations, this turns into:
> 
>     def __add__(self, s1: 'QSize', s2: 'QSize') -> 'QSize': ...
> 
> which is however incorrect, and results in e.g.:
> 
>     from PyQt6.QtCore import QSize
>     print(QSize(1, 2) + QSize(3, 4))
> 
> to be flagged as wrong with:
> 
>     error: Too few arguments for "__add__" of "QSize"  [call-arg]
> 
> This seems to be an issue with almost all operators on various 
> different
> classes.
> 
> Note that it's not necessarly always the first argument, e.g. in 
> QMargin
> there is:
> 
>     @typing.overload
>     def __add__(self, lhs: 'QMargins', rhs: int) -> 'QMargins': ...
>     @typing.overload
>     def __add__(self, lhs: int, rhs: 'QMargins') -> 'QMargins': ...
> 
> where the second one should probably just be dropped.
> 
> Florian

Fixed in the next SIP snapshot.

Thanks,
Phil


More information about the PyQt mailing list