From: Markus Armbruster <armbru@redhat.com>
To: John Snow <jsnow@redhat.com>
Cc: qemu-devel@nongnu.org, Michael Roth <michael.roth@amd.com>
Subject: Re: [PATCH v3 5/7] qapi/parser: add QAPIExpression type
Date: Sat, 11 Feb 2023 07:49:36 +0100 [thread overview]
Message-ID: <87fsbc4ry7.fsf@pond.sub.org> (raw)
In-Reply-To: <20230209184758.1017863-6-jsnow@redhat.com> (John Snow's message of "Thu, 9 Feb 2023 13:47:56 -0500")
John Snow <jsnow@redhat.com> writes:
> This patch creates a new type, QAPIExpression, which represents a parsed
> expression complete with QAPIDoc and QAPISourceInfo.
>
> This patch turns parser.exprs into a list of QAPIExpression instead,
> and adjusts expr.py to match.
>
> This allows the types we specify in parser.py to be "remembered" all the
> way through expr.py and into schema.py. Several assertions around
> packing and unpacking this data can be removed as a result.
>
> NB: This necessitates a change of typing for check_if() and
> check_keys(), because mypy does not believe UserDict[str, object] ⊆
> Dict[str, object]. It will, however, accept Mapping or
> MutableMapping. In this case, the immutable form is preferred as an
> input parameter because we don't actually mutate the input.
>
> Without this change, we will observe:
> qapi/expr.py:631: error: Argument 1 to "check_keys" has incompatible
> type "QAPIExpression"; expected "Dict[str, object]"
>
> NB2: Python 3.6 has an oversight for typing UserDict that makes it
> impossible to type for both runtime and analysis time. The problem is
> described in detail at https://github.com/python/typing/issues/60 - this
> workaround can be safely removed when 3.7 is our minimum version.
>
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
Looks good to me, just one question:
[...]
> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
> index 1b006cdc133..87b46db7fba 100644
> --- a/scripts/qapi/parser.py
> +++ b/scripts/qapi/parser.py
> @@ -14,13 +14,14 @@
> # This work is licensed under the terms of the GNU GPL, version 2.
> # See the COPYING file in the top-level directory.
>
> -from collections import OrderedDict
> +from collections import OrderedDict, UserDict
> import os
> import re
> from typing import (
> TYPE_CHECKING,
> Dict,
> List,
> + Mapping,
> Optional,
> Set,
> Union,
> @@ -37,15 +38,30 @@
> from .schema import QAPISchemaFeature, QAPISchemaMember
>
>
> -#: Represents a single Top Level QAPI schema expression.
> -TopLevelExpr = Dict[str, object]
> -
> # Return value alias for get_expr().
> _ExprValue = Union[List[object], Dict[str, object], str, bool]
>
> -# FIXME: Consolidate and centralize definitions for TopLevelExpr,
> -# _ExprValue, _JSONValue, and _JSONObject; currently scattered across
> -# several modules.
> +
> +# FIXME: Consolidate and centralize definitions for _ExprValue,
> +# JSONValue, and _JSONObject; currently scattered across several
> +# modules.
> +
> +
> +# 3.6 workaround: can be removed when Python 3.7+ is our required version.
> +if TYPE_CHECKING:
> + _UserDict = UserDict[str, object]
> +else:
> + _UserDict = UserDict
> +
> +
> +class QAPIExpression(_UserDict):
Can we subclass dict instead?
> + def __init__(self,
> + initialdata: Mapping[str, object],
> + info: QAPISourceInfo,
> + doc: Optional['QAPIDoc'] = None):
> + super().__init__(initialdata)
> + self.info = info
> + self.doc: Optional['QAPIDoc'] = doc
>
>
> class QAPIParseError(QAPISourceError):
[...]
next prev parent reply other threads:[~2023-02-11 6:50 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-09 18:47 [PATCH v3 0/7] qapi: static typing conversion, pt5c John Snow
2023-02-09 18:47 ` [PATCH v3 1/7] qapi: Update flake8 config John Snow
2023-02-09 18:47 ` [PATCH v3 2/7] qapi: update pylint configuration John Snow
2023-02-09 18:47 ` [PATCH v3 3/7] qapi/expr: Split check_expr out from check_exprs John Snow
2023-02-10 12:14 ` Markus Armbruster
2023-02-10 12:33 ` Markus Armbruster
2023-02-10 15:20 ` John Snow
2023-02-11 10:06 ` Markus Armbruster
2023-02-14 17:04 ` John Snow
2023-02-09 18:47 ` [PATCH v3 4/7] qapi/expr: add typing workaround for AbstractSet John Snow
2023-02-10 15:44 ` Markus Armbruster
2023-02-10 16:26 ` John Snow
2023-02-14 17:08 ` John Snow
2023-02-09 18:47 ` [PATCH v3 5/7] qapi/parser: add QAPIExpression type John Snow
2023-02-11 6:49 ` Markus Armbruster [this message]
2023-02-14 16:57 ` John Snow
2023-02-09 18:47 ` [PATCH v3 6/7] qapi: remove _JSONObject John Snow
2023-02-11 6:18 ` Markus Armbruster
2023-02-09 18:47 ` [PATCH v3 7/7] qapi: remove JSON value FIXME John Snow
2023-02-11 6:42 ` 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=87fsbc4ry7.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=jsnow@redhat.com \
--cc=michael.roth@amd.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.