[PyQt] Type annotations for QStrings: str or Optional[str]?

Florian Bruhin me at the-compiler.org
Tue Nov 27 09:59:53 GMT 2018


On Mon, Nov 26, 2018 at 09:57:32PM +0000, Phil Thompson wrote:
> On 26 Nov 2018, at 8:41 pm, Florian Bruhin <me at the-compiler.org> wrote:
> > 
> > Hi,
> > 
> > sip/QtCore/qstring.sip says:
> > 
> >  %MappedType QString /AllowNone,TypeHint="str",TypeHintValue="''"/
> > 
> > At least where Qt functions take a QString as argument, passing None
> > should always be allowed, so I think Optional[str] would be more
> > appropriate?
> 
> More accurate, yes..
> 
> > Where they return a QString, I'm not so sure - what happens if Qt
> > returns a null QString? Even if PyQt maps that to None, it seems rather
> > cumbersome to have Optional[str] there, as that'd mean you'd need to
> > check every QString return value for None-ness (which isn't really done
> > in practice, as often you wouldn't expect to ever get None back).
> 
> Both null and empty QStrings are converted to ''.
> 
> IMHO 'str' rather than 'Optional[str]' is less clunky, particularly in
> docstrings. I'm not sure null QStrings are even still used.

Fair point. FWIW, in my codebase it turned up with
QApplication::setOrganizationName() and QFileDialog::getOpenFileNames
(its "caption" argument). From a quick look at Qt's sources, it seems to
check for .isEmpty() in both cases, so I suppose I could just use ""
there instead.

Florian

-- 
https://www.qutebrowser.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20181127/8c3fc140/attachment.sig>


More information about the PyQt mailing list