diff options
author | Christian Tismer <tismer@stackless.com> | 2019-02-01 11:12:43 +0100 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2019-02-05 14:17:48 +0000 |
commit | 3748e8fa9830e97466f12104501d4acf104260a5 (patch) | |
tree | 67dc037767dce0f87568b4a27beac69010742026 /sources/shiboken2/generator | |
parent | 295061d00be3dedf60d8adc97bd14ff46931ef4c (diff) |
Type Hints: Handle Container Types Correctly
An error was reported that we had a bug in this .pyi line:
def parseErrors(self) -> PySide2.QtScxml.QScxmlError: ...
which actually had to be a list like
def parseErrors(self) -> typing.List[PySide2.QtScxml.QScxmlError]: ...
A deeper look revealed that we had very many other examples,
also in the argument lists, were we did not handle containers
properly. (For example, 90 times in QtCore!)
This is a leftover from the type error messages which
were generated in C++, and never really understood.
This is now a clean rewrite of the C++ part to expose all
information and an extension of the Python parser that
systematically uses the container types from the typing
module.
The implementation became a bit more complex because of
Python 2: We need to provide our own copy of the public
typing module, because it is not safe to assume that this module
can be loaded. Importing it from support.signature gave a
problem because we now need to find the names of instances
to produce List[...], which needed to be implemented in the
loader.
Implemented the "Pair" type now as a native generic type.
This is more correct than the former implementation and
shorter in the .pyi files.
Additionally, an own definition of "Char" was provided for
brevity. This was not important to implement and could also
be done with "int", but it is helpful for the future to know how
to implement own types (and it was fun).
Task-number: PYSIDE-921
Task-number: PYSIDE-795
Change-Id: I0e565b38d7b4fff39dd606950f9f8d97c45acbf5
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/shiboken2/generator')
-rw-r--r-- | sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index d15f3a951..56f53f304 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -4573,7 +4573,11 @@ void CppGenerator::writeSignatureInfo(QTextStream &s, const AbstractMetaFunction QStringList args; const AbstractMetaArgumentList &arguments = f->arguments(); for (const AbstractMetaArgument *arg : arguments) { + AbstractMetaType *argType = getTypeWithoutContainer(arg->type()); QString strArg = resolveRetOrArgType(arg->type()); + // PYSIDE-921: Handle container returntypes correctly. + if (argType != arg->type()) + strArg += QLatin1Char('[') + resolveRetOrArgType(argType) + QLatin1Char(']'); if (!arg->defaultValueExpression().isEmpty()) { strArg += QLatin1Char('='); QString e = arg->defaultValueExpression(); @@ -4587,10 +4591,12 @@ void CppGenerator::writeSignatureInfo(QTextStream &s, const AbstractMetaFunction // mark the multiple signatures as such, to make it easier to generate different code if (multiple) s << idx-- << ':'; - // now calculate the return type. s << funcName << '(' << args.join(QLatin1Char(',')) << ')'; AbstractMetaType *returnType = getTypeWithoutContainer(f->type()); - if (returnType) + // PYSIDE-921: Handle container returntypes correctly. + if (returnType != f->type()) + s << "->" << resolveRetOrArgType(f->type()) << '[' << resolveRetOrArgType(returnType) << ']'; + else if (returnType) s << "->" << resolveRetOrArgType(returnType); s << endl; } |