aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2021-05-25 17:05:09 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-05-26 15:56:20 +0000
commit86181fa524ddc91e98cf65730809ed9a0f4bdcde (patch)
treea35aa91dcd0e161751baf69da47b7f54bc5162f8
parent8fde785b2f20ac10cb1628e6a110bc11fcbfa3e6 (diff)
pyi_generator: Optimize imports
Imports now import needed things, only. Task-number: PYSIDE-1019 Change-Id: If2dc2d04934123c3a6db7e69388d4c4d37cbc475 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> (cherry picked from commit 4398e8f30c54841d0e5feb74e251b327e8bf010a) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/lib/pyi_generator.py44
1 files changed, 32 insertions, 12 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 4adf2fb01..f81e738b2 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
@@ -141,21 +141,15 @@ class Formatter(Writer):
@contextmanager
def module(self, mod_name):
self.mod_name = mod_name
- support = "PySide6.support" if self.options._pyside_call else "shibokensupport"
- extra = "from PySide6 import PyClassProperty" if self.options._pyside_call else ""
txt = f"""\
# Module `{mod_name}`
- import typing
- from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from shiboken6 import Shiboken
- from {support}.signature.mapping import (
- Virtual, Missing, Invalid, Default, Instance)
- {extra}
+
+ import typing
+ <<IMPORTS>>
"""
self.print(dedent(txt))
- # This line will be replaced by the missing imports postprocess.
- self.print("IMPORTS")
yield
@contextmanager
@@ -216,6 +210,27 @@ def find_imports(text):
return [imp for imp in PySide6.__all__ if f"PySide6.{imp}." in text]
+FROM_IMPORTS = [
+ ("typing", "Any Callable Dict List Optional Tuple Union".split()),
+ ("PySide6", ["PyClassProperty"]),
+ ]
+
+def filter_from_imports(from_struct, text):
+ """
+ Build a reduced new `from` structure (nfs) with found entries, only
+ """
+ nfs = []
+ for mod, imports in from_struct:
+ lis = []
+ nfs.append((mod, lis))
+ for each in imports:
+ if re.search(rf"(\b|@){each}\b", text):
+ lis.append(each)
+ if not lis:
+ nfs.pop()
+ return nfs
+
+
def find_module(import_name, outpath, from_pyside):
"""
Find a module either directly by import, or use the full path,
@@ -278,10 +293,15 @@ def generate_pyi(import_name, outpath, options):
if not line:
break
line = line.rstrip()
- # we remove the IMPORTS marker and insert imports if needed
- if line == "IMPORTS":
+ # we remove the "<<IMPORTS>>" marker and insert imports if needed
+ if line == "<<IMPORTS>>":
+ text = outfile.getvalue()
+ for mod, imports in filter_from_imports(FROM_IMPORTS, text):
+ import_args = ', '.join(imports)
+ wr.print(f"from {mod} import {import_args}")
+ wr.print()
if need_imports:
- for mod_name in find_imports(outfile.getvalue()):
+ for mod_name in find_imports(text):
imp = "PySide6." + mod_name
if imp != import_name:
wr.print("import " + imp)