diff options
author | Christian Tismer <tismer@stackless.com> | 2019-08-22 18:57:11 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2019-08-29 15:44:07 +0200 |
commit | d4acbacd7a655e3ff9e17663a23ed0d822b84850 (patch) | |
tree | cfe5b2666c5e98c848b22ef7b493c775cff5aebc /sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib | |
parent | 4d63dfffb661115f58cca60c80c4649ba982e01b (diff) |
signature: Support typing.Optional[T] and refine a bit
The signature was missing "typing.Optional[T]" which has to be wrapped
around any argument with a default value of "None".
This is the only case where the repr of a type looks different than
it was written, because it renders as "typing.Union[T, NoneType]".
Solving that by redefining a few typing structures was way too
hard and too error prone. It was finally solved by a regex replacemet
that is run as a post process in generate_pyi.py .
The enumerations are now even more complete, since toplevel enums
are also included. This had the effect that enums with Python
keywords were revealed, and so the function "createEnumItem" had
to be modified.
The order of creation was also changed to avoid name clashes.
The overall structure was improved, and instead of parsing the
generated signatures to find out if something is a class method,
this is now very cleanly implemented as an inquiry to get_signature().
I tried to make sense of the flags structure that comes with many
enums. PyQt5 has a standard set of "__...__" methods without useful
signature information. I could mimick that as well, but that would
create a whole lot of pointless extra information. We should decide
later if it makes sense to include that. Right now the flags
structures show the class name, only.
This patch will be merged with the 5.14 branch. The additions of this
patch could fortunately be placed into areas which do almost not
overlap with the 5.14 signature additions.
Change-Id: Ie513e15917b04d746ab597fb7a9eb1fd766f7c73
Fixes: PYSIDE-1079
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib')
-rw-r--r-- | sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py | 45 | ||||
-rw-r--r-- | sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py | 19 |
2 files changed, 52 insertions, 12 deletions
diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py index 42b123046..b026a5d20 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py @@ -71,6 +71,13 @@ class ExactEnumerator(object): def __init__(self, formatter, result_type=dict): self.fmt = formatter self.result_type = result_type + self.fmt.level = 0 + self.fmt.after_enum = self.after_enum + self._after_enum = False + + def after_enum(self): + ret = self._after_enum + self._after_enum = False def module(self, mod_name): __import__(mod_name) @@ -80,10 +87,12 @@ class ExactEnumerator(object): functions = inspect.getmembers(module, inspect.isroutine) ret = self.result_type() self.fmt.class_name = None - for func_name, func in functions: - ret.update(self.function(func_name, func)) for class_name, klass in members: ret.update(self.klass(class_name, klass)) + if isinstance(klass, EnumType): + self.enum(klass) + for func_name, func in functions: + ret.update(self.function(func_name, func)) return ret def klass(self, class_name, klass): @@ -95,7 +104,7 @@ class ExactEnumerator(object): bases_list = [] for base in klass.__bases__: name = base.__name__ - if name == "object": + if name in ("object", "type"): pass else: modname = base.__module__ @@ -103,30 +112,41 @@ class ExactEnumerator(object): bases_list.append(name) class_str = "{}({})".format(class_name, ", ".join(bases_list)) with self.fmt.klass(class_name, class_str): - ret = self.function("__init__", klass) + ret = self.result_type() # class_members = inspect.getmembers(klass) # gives us also the inherited things. class_members = sorted(list(klass.__dict__.items())) subclasses = [] + functions = [] for thing_name, thing in class_members: if inspect.isclass(thing): subclass_name = ".".join((class_name, thing_name)) subclasses.append((subclass_name, thing)) - else: + elif inspect.isroutine(thing): func_name = thing_name.split(".")[0] # remove ".overload" - ret.update(self.function(func_name, thing)) + functions.append((func_name, thing)) + self.fmt.level += 1 for subclass_name, subclass in subclasses: ret.update(self.klass(subclass_name, subclass)) if isinstance(subclass, EnumType): self.enum(subclass) - return ret + ret = self.function("__init__", klass) + for func_name, func in functions: + func_kind = get_signature(func, "__func_kind__") + modifier = func_kind if func_kind in ( + "staticmethod", "classmethod") else None + ret.update(self.function(func_name, func, modifier)) + self.fmt.level -= 1 + return ret - def function(self, func_name, func): + def function(self, func_name, func, modifier=None): + self.fmt.level += 1 ret = self.result_type() signature = getattr(func, '__signature__', None) if signature is not None: - with self.fmt.function(func_name, signature) as key: + with self.fmt.function(func_name, signature, modifier) as key: ret[key] = signature + self.fmt.level -= 1 return ret def enum(self, subclass): @@ -138,6 +158,7 @@ class ExactEnumerator(object): if type(type(value)) is EnumType: with self.fmt.enum(class_name, enum_name, int(value)): pass + self._after_enum = True def stringify(signature): @@ -160,7 +181,7 @@ class SimplifyingEnumerator(ExactEnumerator): is desired. """ - def function(self, func_name, func): + def function(self, func_name, func, modifier=None): ret = self.result_type() signature = get_signature(func, 'existence') sig = stringify(signature) if signature is not None else None @@ -177,11 +198,11 @@ class HintingEnumerator(ExactEnumerator): hinting stubs. Only default values are replaced by "...". """ - def function(self, func_name, func): + def function(self, func_name, func, modifier=None): ret = self.result_type() signature = get_signature(func, 'hintingstub') if signature is not None: - with self.fmt.function(func_name, signature) as key: + with self.fmt.function(func_name, signature, modifier) as key: ret[key] = signature return ret diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py index b34bfb404..3b0825049 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py @@ -43,6 +43,8 @@ from __future__ import print_function, absolute_import tool.py Some useful stuff, see below. +On the function with_metaclass see the answer from Martijn Pieters on +https://stackoverflow.com/questions/18513821/python-metaclass-understanding-the-with-metaclass """ from textwrap import dedent @@ -132,4 +134,21 @@ def build_brace_pattern(level, separators=""): indent = idx * " ", **locals()) return pattern.replace("C", "{").replace("D", "}") + +# Copied from the six module: +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + # This requires a bit of explanation: the basic idea is to make a dummy + # metaclass for one level of class instantiation that replaces itself with + # the actual metaclass. + class metaclass(type): + + def __new__(cls, name, this_bases, d): + return meta(name, bases, d) + + @classmethod + def __prepare__(cls, name, this_bases): + return meta.__prepare__(name, bases) + return type.__new__(metaclass, 'temporary_class', (), {}) + # eof |