diff options
Diffstat (limited to 'sources')
3 files changed, 82 insertions, 6 deletions
diff --git a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py index 6f86df8c8..b31b161a3 100644 --- a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py +++ b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py @@ -53,6 +53,12 @@ def is_inconsistent_overload(signatures): return count != 0 and count != len(signatures) +def is_relevant_type(thing): + t = str(type(thing)) + return (("PySide" in t or "getset_descriptor" in t) + and "QMetaObject" not in t) + + class ExactEnumerator: """ ExactEnumerator enumerates all signatures in a module as they are. @@ -178,7 +184,9 @@ class ExactEnumerator: # Support attributes that have PySide types as values, # but we skip the 'staticMetaObject' that needs # to be defined at a QObject level. - elif "PySide" in str(type(thing)) and "QMetaObject" not in str(type(thing)): + # PYSIDE-3034: added public variables, extracted helper function to + # avoid repetitive calls of str(type(thing)) + elif is_relevant_type(thing): if class_name not in attributes: attributes[class_name] = {} attributes[class_name][thing_name] = thing diff --git a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py index cdb4d9575..a84eb38dd 100644 --- a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py +++ b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py @@ -32,6 +32,38 @@ from shibokensupport.signature.lib.tool import build_brace_pattern indent = " " * 4 +TYPE_MAP = { + # Qt integer types + "qint64": "int", + "qint32": "int", + "qint16": "int", + "qsizetype": "int", + "quint32": "int", + "quint64": "int", + "size_t": "int", + "uint": "int", + "ushort": "int", + "ulong": "int", + "unsigned char": "int", + "unsigned int": "int", + + # Qt floating types + "qreal": "float", + + # Qt string-like + "QString": "str", + "QStringList": "typing.List[str]", + "QChar": "str", + + # Qt containers (minimal) + "QList": "typing.List", + "QVariant": "typing.Any", + + # C strings + "char*": "str", + "const char*": "str", +} + class Writer: def __init__(self, outfile, *args): @@ -86,6 +118,29 @@ class Formatter(Writer): inspect.formatannotation = cls.backup return stringized + @classmethod + def normalize_type(cls, type_repr: str) -> str: + if not type_repr: + return "typing.Any" + if type_repr in {"void", "void*"}: + return "typing.Any" + if any(x in type_repr for x in ("QRhi", ".ComponentType", ".Semantic")): + return "int" + if ( " " in type_repr and + not any(x in type_repr for x in ("*", "::", "<", ">", "[", "]"))): + return "typing.Any" + if type_repr.startswith("QList["): + inner = type_repr[len("QList["):-1] + inner = cls.normalize_type(inner) + return f"typing.List[{inner}]" + if type_repr.startswith("QMap[") or type_repr.startswith("QHash["): + inner = type_repr[type_repr.find("[") + 1:-1] + key, value = map(str.strip, inner.split(",", 1)) + key = cls.normalize_type(key) + value = cls.normalize_type(value) + return f"typing.Dict[{key}, {value}]" + return TYPE_MAP.get(type_repr, type_repr) + # Adding a pattern to substitute "Union[T, NoneType]" by "Optional[T]" # I tried hard to replace typing.Optional by a simple override, but # this became _way_ too much. @@ -221,7 +276,12 @@ class Formatter(Writer): spaces = indent * self.level # PYSIDE-2903: Use a fully qualified name in the type comment. full_name = f"{type(attr_value).__module__}.{type(attr_value).__qualname__}" - self.print(f"{spaces}{attr_name:25} = ... # type: {full_name}") + if full_name == "builtins.getset_descriptor": + # PYSIDE-3034: Public variable types added to __doc__ + type_repr = self.normalize_type(attr_value.__doc__) + else: + type_repr = full_name + self.print(f"{spaces}{attr_name:25} = ... # type: {type_repr}") yield @contextmanager diff --git a/sources/shiboken6_generator/generator/shiboken/cppgenerator.cpp b/sources/shiboken6_generator/generator/shiboken/cppgenerator.cpp index 4854ebf79..e87a53bb8 100644 --- a/sources/shiboken6_generator/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6_generator/generator/shiboken/cppgenerator.cpp @@ -480,10 +480,17 @@ static QString BuildEnumFlagInfo(const AbstractMetaEnum &cppEnum) } static void writePyGetSetDefEntry(TextStream &s, const QString &name, - const QString &getFunc, const QString &setFunc) + const QString &getFunc, const QString &setFunc, const QString &doc={}) { - s << "{const_cast<char *>(\"" << mangleName(name) << "\"), " << getFunc << ", " - << (setFunc.isEmpty() ? NULL_PTR : setFunc) << ", nullptr, nullptr},\n"; + s << "{\"" << mangleName(name) << "\", " << getFunc << ", " + << (setFunc.isEmpty() ? NULL_PTR : setFunc) << ", "; + + if (doc.isEmpty()) + s << "nullptr"; + else + s << "\"" << doc << "\""; + + s << ", nullptr},\n"; } static bool generateRichComparison(const GeneratorContext &c) @@ -922,8 +929,9 @@ void CppGenerator::generateClass(TextStream &s, const QString setter = canGenerateSetter ? cpythonSetterFunctionName(metaField) : QString(); const auto names = metaField.definitionNames(); + const QString doc = metaField.type().pythonSignature(); for (const auto &name : names) - writePyGetSetDefEntry(s, name, getter, setter); + writePyGetSetDefEntry(s, name, getter, setter, doc); } } |
