From: John Snow <jsnow@redhat.com>
To: Cleber Rosa <crosa@redhat.com>
Cc: qemu-devel@nongnu.org, Eduardo Habkost <ehabkost@redhat.com>,
Markus Armbruster <armbru@redhat.com>
Subject: Re: [PATCH v2 05/11] qapi/introspect.py: add preliminary type hint annotations
Date: Mon, 7 Dec 2020 16:29:56 -0500 [thread overview]
Message-ID: <bcdf0009-127e-d1c1-986f-3c581fdd8fdb@redhat.com> (raw)
In-Reply-To: <20201107021142.GA2208@localhost.localdomain>
On 11/6/20 9:12 PM, Cleber Rosa wrote:
> On Mon, Oct 26, 2020 at 03:42:45PM -0400, John Snow wrote:
>> The typing of _make_tree and friends is a bit involved, but it can be
>> done with some stubbed out types and a bit of elbow grease. The
>> forthcoming patches attempt to make some simplifications, but having the
>> type hints in advance may aid in review of subsequent patches.
>>
>>
>> Some notes on the abstract types used at this point, and what they
>> represent:
>>
>> - TreeValue represents any object in the type tree. _make_tree is an
>> optional call -- not every node in the final type tree will have been
>> passed to _make_tree, so this type encompasses not only what is passed
>> to _make_tree (dicts, strings) or returned from it (dicts, strings, a
>> 2-tuple), but any recursive value for any of the dicts passed to
>> _make_tree -- which includes lists, strings, integers, null constants,
>> and so on.
>>
>> - _DObject is a type alias I use to mean "A JSON-style object,
>> represented as a Python dict." There is no "JSON" type in Python, they
>> are converted natively to recursively nested dicts and lists, with
>> leaf values of str, int, float, None, True/False and so on. This type
>> structure is not possible to accurately portray in mypy yet, so a
>> placeholder is used.
>>
>> In this case, _DObject is being used to refer to SchemaInfo-like
>> structures as defined in qapi/introspect.json, OR any sub-object
>> values they may reference. We don't have strong typing available for
>> those, so a generic alternative is used.
>>
>> - Extra refers explicitly to the dict containing "extra" information
>> about a node in the tree. mypy does not offer per-key typing for dicts
>> in Python 3.6, so this is the best we can do here.
>>
>> - Annotated refers to (one of) the return types of _make_tree:
>> It represents a 2-tuple of (TreeValue, Extra).
>>
>>
>> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
>> Signed-off-by: John Snow <jsnow@redhat.com>
>> ---
>> scripts/qapi/introspect.py | 157 ++++++++++++++++++++++++++++---------
>> scripts/qapi/mypy.ini | 5 --
>> scripts/qapi/schema.py | 2 +-
>> 3 files changed, 121 insertions(+), 43 deletions(-)
>>
>> diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
>> index 63f721ebfb6..803288a64e7 100644
>> --- a/scripts/qapi/introspect.py
>> +++ b/scripts/qapi/introspect.py
>> @@ -10,7 +10,16 @@
>> See the COPYING file in the top-level directory.
>> """
>>
>> -from typing import Optional, Sequence, cast
>> +from typing import (
>> + Any,
>> + Dict,
>> + List,
>> + Optional,
>> + Sequence,
>> + Tuple,
>> + Union,
>> + cast,
>> +)
>>
>> from .common import (
>> c_name,
>> @@ -20,13 +29,56 @@
>> )
>> from .gen import QAPISchemaMonolithicCVisitor
>> from .schema import (
>> + QAPISchema,
>> QAPISchemaArrayType,
>> QAPISchemaBuiltinType,
>> + QAPISchemaEntity,
>> + QAPISchemaEnumMember,
>> + QAPISchemaFeature,
>> + QAPISchemaObjectType,
>> + QAPISchemaObjectTypeMember,
>> QAPISchemaType,
>> + QAPISchemaVariant,
>> + QAPISchemaVariants,
>> )
>> +from .source import QAPISourceInfo
>>
>>
>> -def _make_tree(obj, ifcond, features, extra=None):
>> +# This module constructs a tree-like data structure that is used to
>> +# generate the introspection information for QEMU. It behaves similarly
>> +# to a JSON value.
>> +#
>> +# A complexity over JSON is that our values may or may not be annotated.
>> +#
>> +# Un-annotated values may be:
>> +# Scalar: str, bool, None.
>> +# Non-scalar: List, Dict
>> +# _Value = Union[str, bool, None, Dict[str, Value], List[Value]]
>
> Here (and in a few other places) you mention `_Value`, but then define it as
> `_value` (lowercase).
>
This was maybe too subtle: I didn't intend for it to be the "same" as
_value; this is the "idealized version". And then I went and re-used the
same exact name of "TreeValue", which muddied the waters.
Let's just err back on the side of synchronicity and say:
# _value = Union[str, bool, None, Dict[str, TreeValue], List[TreeValue]]
# TreeValue = Union[_value, Annotated[_value]]
>> +#
>> +# With optional annotations, the type of all values is:
>> +# TreeValue = Union[_Value, Annotated[_Value]]
>> +#
>> +# Sadly, mypy does not support recursive types, so we must approximate this.
>> +_stub = Any
>> +_scalar = Union[str, bool, None]
>> +_nonscalar = Union[Dict[str, _stub], List[_stub]]
>
> Why not use `Any` here instead of declaring/using `_stub`?
>
This was my way of calling visual attention to the exact places in the
type tree we are breaking recursion with a stubbed type.
I wanted to communicate that we didn't WANT the any type, but we were
forced to accept a "stub" type. (Which we implement with the Any type.)
>> +_value = Union[_scalar, _nonscalar]
>> +TreeValue = Union[_value, 'Annotated']
>> +
>
> Maybe declare `Annotations` first, then `Annotated`, then *use*
> `Annotated` proper here?
>
As long as that doesn't lose clarity and synchronicity with the little
comment blurb, or cause problems later in the patch. I will see if there
was a reason I couldn't.
Otherwise, it's not really too important to shuffle around things to
avoid strings; it doesn't change anything for mypy or for the runtime.
> - Cleber.
>
Thanks!
--js
next prev parent reply other threads:[~2020-12-07 21:33 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-26 19:42 [PATCH v2 00/11] qapi: static typing conversion, pt2 John Snow
2020-10-26 19:42 ` [PATCH v2 01/11] [DO-NOT-MERGE] docs: replace single backtick (`) with double-backtick (``) John Snow
2020-10-26 19:42 ` [PATCH v2 02/11] [DO-NOT-MERGE] docs/sphinx: change default role to "any" John Snow
2020-10-26 19:42 ` [PATCH v2 03/11] [DO-NOT-MERGE] docs: enable sphinx-autodoc for scripts/qapi John Snow
2020-10-26 19:42 ` [PATCH v2 04/11] qapi/introspect.py: add assertions and casts John Snow
2020-11-06 18:59 ` Cleber Rosa
2020-10-26 19:42 ` [PATCH v2 05/11] qapi/introspect.py: add preliminary type hint annotations John Snow
2020-11-07 2:12 ` Cleber Rosa
2020-12-07 21:29 ` John Snow [this message]
2020-11-13 16:48 ` Markus Armbruster
2020-12-07 23:48 ` John Snow
2020-12-16 7:51 ` Markus Armbruster
2020-12-16 17:49 ` Eduardo Habkost
2020-12-17 6:51 ` Markus Armbruster
2020-12-17 1:35 ` John Snow
2020-12-17 7:00 ` Markus Armbruster
2020-10-26 19:42 ` [PATCH v2 06/11] qapi/introspect.py: add _gen_features helper John Snow
2020-11-07 4:23 ` Cleber Rosa
2020-11-16 8:47 ` Markus Armbruster
2020-12-07 23:57 ` John Snow
2020-12-15 16:55 ` Markus Armbruster
2020-12-15 18:49 ` John Snow
2020-10-26 19:42 ` [PATCH v2 07/11] qapi/introspect.py: Unify return type of _make_tree() John Snow
2020-11-07 5:08 ` Cleber Rosa
2020-12-15 0:22 ` John Snow
2020-11-16 9:46 ` Markus Armbruster
2020-12-08 0:06 ` John Snow
2020-12-16 6:35 ` Markus Armbruster
2020-10-26 19:42 ` [PATCH v2 08/11] qapi/introspect.py: replace 'extra' dict with 'comment' argument John Snow
2020-11-07 5:10 ` Cleber Rosa
2020-11-16 9:55 ` Markus Armbruster
2020-12-08 0:12 ` John Snow
2020-10-26 19:42 ` [PATCH v2 09/11] qapi/introspect.py: create a typed 'Annotated' data strutcure John Snow
2020-11-07 5:45 ` Cleber Rosa
2020-11-16 10:12 ` Markus Armbruster
2020-12-08 0:21 ` John Snow
2020-12-16 7:08 ` Markus Armbruster
2020-12-17 1:30 ` John Snow
2020-10-26 19:42 ` [PATCH v2 10/11] qapi/introspect.py: improve readability of _tree_to_qlit John Snow
2020-11-07 5:54 ` Cleber Rosa
2020-11-16 10:17 ` Markus Armbruster
2020-12-15 15:25 ` John Snow
2020-10-26 19:42 ` [PATCH v2 11/11] qapi/introspect.py: Add docstring to _tree_to_qlit John Snow
2020-11-07 5:57 ` Cleber Rosa
2020-11-02 15:40 ` [PATCH v2 00/11] qapi: static typing conversion, pt2 John Snow
2020-11-04 9:51 ` Marc-André Lureau
2020-12-15 15:52 ` John Snow
2020-11-16 13:17 ` introspect.py output representation (was: [PATCH v2 00/11] qapi: static typing conversion, pt2) Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bcdf0009-127e-d1c1-986f-3c581fdd8fdb@redhat.com \
--to=jsnow@redhat.com \
--cc=armbru@redhat.com \
--cc=crosa@redhat.com \
--cc=ehabkost@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).