Type hints: Operators declared outside class

Florian Bruhin me at the-compiler.org
Sat Jul 8 23:38:09 BST 2023


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20230709/43819c26/attachment.sig>


More information about the PyQt mailing list