aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py')
-rw-r--r--sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py123
1 files changed, 51 insertions, 72 deletions
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 eb2e49770..ce12dd6c8 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
@@ -1,44 +1,10 @@
LICENSE_TEXT = """
-#############################################################################
-##
-## Copyright (C) 2021 The Qt Company Ltd.
-## Contact: https://www.qt.io/licensing/
-##
-## This file is part of Qt for Python.
-##
-## $QT_BEGIN_LICENSE:LGPL$
-## Commercial License Usage
-## Licensees holding valid commercial Qt licenses may use this file in
-## accordance with the commercial license agreement provided with the
-## Software or, alternatively, in accordance with the terms contained in
-## a written agreement between you and The Qt Company. For licensing terms
-## and conditions see https://www.qt.io/terms-conditions. For further
-## information use the contact form at https://www.qt.io/contact-us.
-##
-## GNU Lesser General Public License Usage
-## Alternatively, this file may be used under the terms of the GNU Lesser
-## General Public License version 3 as published by the Free Software
-## Foundation and appearing in the file LICENSE.LGPL3 included in the
-## packaging of this file. Please review the following information to
-## ensure the GNU Lesser General Public License version 3 requirements
-## will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-##
-## GNU General Public License Usage
-## Alternatively, this file may be used under the terms of the GNU
-## General Public License version 2.0 or (at your option) the GNU General
-## Public license version 3 or any later version approved by the KDE Free
-## Qt Foundation. The licenses are as published by the Free Software
-## Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-## included in the packaging of this file. Please review the following
-## information to ensure the GNU General Public License requirements will
-## be met: https://www.gnu.org/licenses/gpl-2.0.html and
-## https://www.gnu.org/licenses/gpl-3.0.html.
-##
-## $QT_END_LICENSE$
-##
-#############################################################################
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
"""
+# flake8: noqa E:402
+
"""
pyi_generator.py
@@ -50,7 +16,6 @@ import io
import logging
import os
import re
-import subprocess
import sys
import typing
@@ -58,7 +23,6 @@ from pathlib import Path
from contextlib import contextmanager
from textwrap import dedent
-from shiboken6 import Shiboken
from shibokensupport.signature.lib.enum_sig import HintingEnumerator
from shibokensupport.signature.lib.tool import build_brace_pattern
@@ -96,6 +60,7 @@ class Formatter(Writer):
unrelated tasks of enumeration and formatting apart.
"""
def __init__(self, outfile, options, *args):
+ # XXX Find out which of these patches is still necessary!
self.options = options
Writer.__init__(self, outfile, *args)
# patching __repr__ to disable the __repr__ of typing.TypeVar:
@@ -111,8 +76,12 @@ class Formatter(Writer):
"""
def _typevar__repr__(self):
return f"typing.{self.__name__}"
- typing.TypeVar.__repr__ = _typevar__repr__
-
+ # This is no longer necessary for modern typing versions.
+ # Ignore therefore if the repr is read-only and cannot be changed.
+ try:
+ typing.TypeVar.__repr__ = _typevar__repr__
+ except TypeError:
+ pass
# 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.
@@ -121,6 +90,7 @@ class Formatter(Writer):
pattern = fr"\b Union \s* \[ \s* {brace_pat} \s*, \s* NoneType \s* \]"
replace = r"Optional[\1]"
optional_searcher = re.compile(pattern, flags=re.VERBOSE)
+
def optional_replacer(source):
return optional_searcher.sub(replace, str(source))
self.optional_replacer = optional_replacer
@@ -138,8 +108,6 @@ class Formatter(Writer):
txt = f"""\
# Module `{mod_name}`
- from shiboken6 import Shiboken
-
<<IMPORTS>>
"""
self.print(dedent(txt))
@@ -190,13 +158,17 @@ class Formatter(Writer):
self.print(f"{spaces}{enum_name:25}: {class_name} = ... # {hexval}")
yield
+ @contextmanager
+ def attribute(self, attr_name, attr_value):
+ spaces = indent * self.level
+ self.print(f"{spaces}{attr_name:25} = ... # type: {type(attr_value).__qualname__}")
+ yield
-def get_license_text():
- with sourcepath.open() as f:
- lines = f.readlines()
- license_line = next((lno for lno, line in enumerate(lines)
- if "$QT_END_LICENSE$" in line))
- return "".join(lines[:license_line + 3])
+ @contextmanager
+ def signal(self, class_name, sig_name, sig_str):
+ spaces = indent * self.level
+ self.print(f"{spaces}{sig_name:25}: ClassVar[{class_name}] = ... # {sig_str}")
+ yield
def find_imports(text):
@@ -204,11 +176,16 @@ def find_imports(text):
FROM_IMPORTS = [
- ("typing", "Any Callable Dict List Optional overload Tuple Union".split()),
- ("PySide6.QtCore", ["PyClassProperty"]),
(None, ["builtins"]),
+ (None, ["os"]),
+ (None, ["enum"]),
+ ("collections.abc", ["Iterable"]),
+ ("typing", sorted(typing.__all__)),
+ ("PySide6.QtCore", ["PyClassProperty", "Signal", "SignalInstance"]),
+ ("shiboken6", ["Shiboken"]),
]
+
def filter_from_imports(from_struct, text):
"""
Build a reduced new `from` structure (nfs) with found entries, only
@@ -218,8 +195,15 @@ def filter_from_imports(from_struct, text):
lis = []
nfs.append((mod, lis))
for each in imports:
- if re.search(rf"(\b|@){each}\b", text):
- lis.append(each)
+ # PYSIDE-1603: We search text that is a usage of the class `each`,
+ # but only if the class is not also defined here.
+ if (f"class {each}(") not in text:
+ if re.search(rf"(\b|@){each}\b([^\s\(:]|\n)", text):
+ lis.append(each)
+ # Search if a type is present in the return statement
+ # of function declarations: '... -> here:'
+ if re.search(rf"->.*{each}.*:", text):
+ lis.append(each)
if not lis:
nfs.pop()
return nfs
@@ -258,12 +242,10 @@ def generate_pyi(import_name, outpath, options):
obj = getattr(top, plainname) if import_name != plainname else top
if not getattr(obj, "__file__", None) or Path(obj.__file__).is_dir():
raise ModuleNotFoundError(f"We do not accept a namespace as module `{plainname}`")
- module = sys.modules[import_name]
outfile = io.StringIO()
fmt = Formatter(outfile, options)
fmt.print(LICENSE_TEXT.strip())
- need_imports = options._pyside_call and not USE_PEP563
if USE_PEP563:
fmt.print("from __future__ import annotations")
fmt.print()
@@ -290,6 +272,12 @@ def generate_pyi(import_name, outpath, options):
# we remove the "<<IMPORTS>>" marker and insert imports if needed
if line == "<<IMPORTS>>":
text = outfile.getvalue()
+ wr.print("import " + import_name)
+ for mod_name in find_imports(text):
+ imp = "PySide6." + mod_name
+ if imp != import_name:
+ wr.print("import " + imp)
+ wr.print()
for mod, imports in filter_from_imports(FROM_IMPORTS, text):
import_args = ', '.join(imports)
if mod is None:
@@ -298,27 +286,16 @@ def generate_pyi(import_name, outpath, options):
else:
wr.print(f"from {mod} import {import_args}")
wr.print()
- if need_imports:
- for mod_name in find_imports(text):
- imp = "PySide6." + mod_name
- if imp != import_name:
- wr.print("import " + imp)
- # Do not import Shiboken which is handled already.
- if import_name != "Shiboken":
- wr.print("import " + import_name)
wr.print()
+ wr.print("NoneType: TypeAlias = type[None]")
wr.print()
else:
wr.print(line)
if not options.quiet:
options.logger.info(f"Generated: {outfilepath}")
- if options and (options.check or options.is_ci):
- # Python 3.7 and up: We can check the file directly if the syntax is ok.
- if USE_PEP563:
- subprocess.check_output([sys.executable, os.fspath(outfilepath)])
-if __name__ == "__main__":
+def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=dedent("""\
@@ -330,11 +307,10 @@ if __name__ == "__main__":
pyi_generator will try to generate an interface "<module>.pyi".
"""))
parser.add_argument("module",
- help="The full path name of an importable module binary (.pyd, .so)")
+ help="The full path name of an importable module binary (.pyd, .so)") # noqa E:128
parser.add_argument("--quiet", action="store_true", help="Run quietly")
- parser.add_argument("--check", action="store_true", help="Test the output")
parser.add_argument("--outpath",
- help="the output directory (default = location of module binary)")
+ help="the output directory (default = location of module binary)") # noqa E:128
options = parser.parse_args()
module = options.module
outpath = options.outpath
@@ -352,4 +328,7 @@ if __name__ == "__main__":
options.logger = logger
generate_pyi(module, outpath, options=options)
+
+if __name__ == "__main__":
+ main()
# eof