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

Phil Thompson phil at riverbankcomputing.com
Mon Nov 26 21:57:32 GMT 2018


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.

Happy to discuss further...

Phil


More information about the PyQt mailing list