diff options
| author | Christian Tismer <tismer@stackless.com> | 2024-11-08 12:30:52 +0100 |
|---|---|---|
| committer | Christian Tismer <tismer@stackless.com> | 2024-11-08 20:05:01 +0100 |
| commit | 1ef1fefc26038a80ba81a860cff5024db44dca37 (patch) | |
| tree | a24ca8deae268b6b0071020ab21c1c1820543027 | |
| parent | 3ea6979779ec7c51742233d4a384cdb5aad86490 (diff) | |
type hints: add __hash__, fix __copy__, __repr__ and __dir__
The other missing things are sequence and mapping methods
which need some more support from cppgenerator. Will be
added later.
typing.Self is only known since Python 3.11 .
We emulate that by patching it into typing and adjusting
the Python version for mypy.
Task-number: PYSIDE-2846
Change-Id: Icb1060c7bba355061d8f134c8e76fb14369d7dda
Pick-to: 6.8
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
6 files changed, 19 insertions, 9 deletions
diff --git a/sources/pyside6/tests/pysidetest/mypy_correctness_test.py b/sources/pyside6/tests/pysidetest/mypy_correctness_test.py index 5f94f82ab..fd253095b 100644 --- a/sources/pyside6/tests/pysidetest/mypy_correctness_test.py +++ b/sources/pyside6/tests/pysidetest/mypy_correctness_test.py @@ -52,7 +52,9 @@ class MypyCorrectnessTest(unittest.TestCase): def testMypy(self): self.assertTrue(HAVE_MYPY) - cmd = [sys.executable, "-m", "mypy", "--cache-dir", self.cache_dir, self.pyside_dir] + insert_version = ["--python-version", "3.11"] if sys.version_info[:2] < (3, 11) else [] + cmd = ([sys.executable, "-m", "mypy", "--cache-dir", self.cache_dir] + + insert_version + [self.pyside_dir]) time_pre = time.time() ret = subprocess.run(cmd, capture_output=True) time_post = time.time() diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 4fbbf9881..ff919fd7b 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -884,7 +884,7 @@ void CppGenerator::generateClass(TextStream &s, s << closeExternC; if (hasHashFunction(metaClass)) - writeHashFunction(s, classContext); + writeHashFunction(s, signatureStream, classContext); // Write tp_traverse and tp_clear functions. writeTpTraverseFunction(s, metaClass); @@ -4681,7 +4681,7 @@ void CppGenerator::writeSequenceMethods(TextStream &s, const AbstractMetaArgument *lastArg = func->arguments().isEmpty() ? nullptr : &func->arguments().constLast(); writeCodeSnips(s, snips,TypeSystem::CodeSnipPositionAny, TypeSystem::TargetLangCode, func, false /* uses PyArgs */, lastArg); - s<< outdent << "}\n\n"; + s << outdent << "}\n\n"; } if (!injectedCode) @@ -4853,7 +4853,7 @@ QString CppGenerator::writeCopyFunction(TextStream &s, const QString className = chopType(cpythonTypeName(metaClass)); const QString funcName = className + u"__copy__"_s; - signatureStream << fullPythonClassName(metaClass) << ".__copy__()\n"; + signatureStream << fullPythonClassName(metaClass) << ".__copy__(self)->typing.Self\n"; definitionStream << PyMethodDefEntry{u"__copy__"_s, funcName, {"METH_NOARGS"_ba}, {}} << ",\n"; @@ -6861,9 +6861,10 @@ void CppGenerator::writeReturnValueHeuristics(TextStream &s, const AbstractMetaF } } -void CppGenerator::writeHashFunction(TextStream &s, const GeneratorContext &context) +void CppGenerator::writeHashFunction(TextStream &s, TextStream &t, const GeneratorContext &context) { const auto metaClass = context.metaClass(); + t << fullPythonClassName(metaClass) << ".__hash__(self)->int\n"; const char hashType[] = "Py_hash_t"; s << "static " << hashType << ' ' << cpythonBaseName(metaClass) << "_HashFunc(PyObject *self)\n{\n" << indent; diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h index 7c5017b2f..387d81ba9 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.h +++ b/sources/shiboken6/generator/shiboken/cppgenerator.h @@ -531,7 +531,7 @@ private: static bool shouldGenerateGetSetList(const AbstractMetaClassCPtr &metaClass); static bool hasHashFunction(const AbstractMetaClassCPtr &c); - static void writeHashFunction(TextStream &s, const GeneratorContext &context); + static void writeHashFunction(TextStream &s, TextStream &t, const GeneratorContext &context); /// Write default implementations for sequence protocol void writeDefaultSequenceMethods(TextStream &s, const GeneratorContext &context) const; diff --git a/sources/shiboken6/generator/shiboken/cppgenerator_smartpointer.cpp b/sources/shiboken6/generator/shiboken/cppgenerator_smartpointer.cpp index c18e6d256..6245b24a8 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator_smartpointer.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator_smartpointer.cpp @@ -204,7 +204,7 @@ void CppGenerator::generateSmartPointerClass(TextStream &s, s << closeExternC; if (hasHashFunction(metaClass)) - writeHashFunction(s, classContext); + writeHashFunction(s, signatureStream, classContext); // Write tp_traverse and tp_clear functions. writeTpTraverseFunction(s, metaClass); 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 453bd2471..fdaada3a0 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 @@ -267,10 +267,11 @@ class ExactEnumerator(object): decorator = f"builtins.{decorator}" signature = self.get_signature(func, decorator) # PYSIDE-2846: Special cases of signatures which inherit from object. + _self = inspect.Parameter("self", inspect._POSITIONAL_OR_KEYWORD) if func_name == "__dir__": - signature = inspect.Signature([], return_annotation=typing.Iterable[str]) + signature = inspect.Signature([_self], return_annotation=typing.Iterable[str]) elif func_name == "__repr__": - signature = inspect.Signature([], return_annotation=str) + signature = inspect.Signature([_self], return_annotation=str) if signature is not None: aug_ass = func in self.mypy_aug_ass_errors with self.fmt.function(func_name, signature, decorator, aug_ass) as key: diff --git a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/mapping.py b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/mapping.py index a29e25ff3..95245b8af 100644 --- a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/mapping.py +++ b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/mapping.py @@ -29,6 +29,12 @@ class ellipsis(object): return "..." +if not hasattr(typing, "Self"): + @typing._SpecialForm + def Self(self, parameters): + raise TypeError(f"{self} is not subscriptable") + typing.Self = Self + ellipsis = ellipsis() Point = typing.Tuple[int, int] Variant = typing.Any |
