qapi: static typing conversion, pt5c
v3 cover qapi: static typing conversion, pt5c
This is part five (c), and focuses on sharing strict types between parser.py and expr.py.
gitlab: https://gitlab.com/jsnow/qemu/-/commits/python-qapi-cleanup-pt5c
Every commit should pass with:
- isort -c qapi/
- flake8 qapi/
- pylint --rcfile=qapi/pylintrc qapi/
- mypy --config-file=qapi/mypy.ini qapi/
V3:
- Squashed a bunch of patches into the QAPIExpression patch
- Added a few 'info' locals whenever there were >= 3 usages to help minimize some churn
- Removed some type redundancy from docstrings
- Brought along the two patches from pt0 that I want merged.
- Removed 'pexpr' entirely, there's no intermediate state where it's needed now.
- Minor style issues.
John Snow (7):
qapi: Update flake8 config
qapi: update pylint configuration qapi/expr: Split check_expr out from check_exprs qapi/expr: add typing workaround for AbstractSet qapi/parser: add QAPIExpression type
qapi: remove _JSONObject
qapi: remove JSON value FIXME
scripts/qapi/.flake8 | 3 +- scripts/qapi/expr.py | 231 +++++++++++++++++++---------------------- scripts/qapi/parser.py | 49 +++++---- scripts/qapi/pylintrc | 1 + scripts/qapi/schema.py | 72 +++++++------ 5 files changed, 181 insertions(+), 175 deletions(-)
Signature · 1 line
2.39.0
PATCH v3 1/7 qapi: Update flake8 config
New versions of flake8 don't like same-line comments. (It's a version newer than what fc37 ships, but it still makes my life easier to fix it now.)
--- scripts/qapi/.flake8 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/qapi/.flake8 +2 -1
@@ -1,2 +1,3 @@ [flake8] -extend-ignore = E722 # Prefer pylint's bare-except checks to flake8's +# Prefer pylint's bare-except checks to flake8's +extend-ignore = E722
Signature · 1 line
2.39.0
PATCH v3 2/7 qapi: update pylint configuration
In reply to John SnowNewer versions of pylint disable the "no-self-use" message by default. Older versions don't, though. If we leave the suppressions in, pylint yelps about useless options. Just tell pylint to shush.
--- scripts/qapi/pylintrc | 1 + 1 file changed, 1 insertion(+)
scripts/qapi/pylintrc +1 -0
@@ -23,6 +23,7 @@ disable=fixme, too-many-statements, too-many-instance-attributes, consider-using-f-string, + useless-option-value, [REPORTS]
Signature · 1 line
2.39.0
PATCH v3 3/7 qapi/expr: Split check_expr out from check_exprs
In reply to John SnowPrimarily, this reduces a nesting level of a particularly long block. It's mostly code movement, but a new docstring is created.
It also has the effect of creating a fairly convenient "catch point" in check_exprs for exception handling without making the nesting level even worse.
---
This patch was originally written as part of my effort to factor out QAPISourceInfo from this file by having expr.py raise a simple exception, then catch and wrap it at the higher level.
This series doesn't do that anymore, but reducing the nesting level still seemed subjectively nice. It's not crucial.
--- scripts/qapi/expr.py | 179 +++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 84 deletions(-)
scripts/qapi/expr.py +95 -84
@@ -595,6 +595,99 @@ def check_event(expr: _JSONObject, info: QAPISourceInfo) -> None: check_type(args, info, "'data'", allow_dict=not boxed) +def check_expr(expr_elem: _JSONObject) -> None: + """ + Validate and normalize a parsed QAPI schema expression. + + :param expr_elem: The parsed expression to normalize and validate. + + :raise QAPISemError: When this expression fails validation. + :return: None, The expression is normalized in-place as needed. + """ + # Expression + assert isinstance(expr_elem['expr'], dict) + for key in expr_elem['expr'].keys(): + assert isinstance(key, str) + expr: _JSONObject = expr_elem['expr'] + + # QAPISourceInfo + assert isinstance(expr_elem['info'], QAPISourceInfo) + info: QAPISourceInfo = expr_elem['info'] + + # Optional[QAPIDoc] + tmp = expr_elem.get('doc') + assert tmp is None or isinstance(tmp, QAPIDoc) + doc: Optional[QAPIDoc] = tmp + + if 'include' in expr: + return + + metas = expr.keys() & {'enum', 'struct', 'union', 'alternate', + 'command', 'event'} + if len(metas) != 1: + raise QAPISemError( + info, + "expression must have exactly one key" + " 'enum', 'struct', 'union', 'alternate'," + " 'command', 'event'") + meta = metas.pop() + + check_name_is_str(expr[meta], info, "'%s'" % meta) + name = cast(str, expr[meta]) + info.set_defn(meta, name) + check_defn_name_str(name, info, meta) + + if doc: + if doc.symbol != name: + raise QAPISemError( + info, "documentation comment is for '%s'" % doc.symbol) + doc.check_expr(expr) + elif info.pragma.doc_required: + raise QAPISemError(info, + "documentation comment required") + + if meta == 'enum': + check_keys(expr, info, meta, + ['enum', 'data'], ['if', 'features', 'prefix']) + check_enum(expr, info) + elif meta == 'union': + check_keys(expr, info, meta, + ['union', 'base', 'discriminator', 'data'], + ['if', 'features']) + normalize_members(expr.get('base')) + normalize_members(expr['data']) + check_union(expr, info) + elif meta == 'alternate': + check_keys(expr, info, meta, + ['alternate', 'data'], ['if', 'features']) + normalize_members(expr['data']) + check_alternate(expr, info) + elif meta == 'struct': + check_keys(expr, info, meta, + ['struct', 'data'], ['base', 'if', 'features']) + normalize_members(expr['data']) + check_struct(expr, info) + elif meta == 'command': + check_keys(expr, info, meta, + ['command'], + ['data', 'returns', 'boxed', 'if', 'features', + 'gen', 'success-response', 'allow-oob', + 'allow-preconfig', 'coroutine']) + normalize_members(expr.get('data')) + check_command(expr, info) + elif meta == 'event': + check_keys(expr, info, meta, + ['event'], ['data', 'boxed', 'if', 'features']) + normalize_members(expr.get('data')) + check_event(expr, info) + else: + assert False, 'unexpected meta type' + + check_if(expr, info, meta) + check_features(expr.get('features'), info) + check_flags(expr, info) + + def check_exprs(exprs: List[_JSONObject]) -> List[_JSONObject]: """ Validate and normalize a list of parsed QAPI schema expressions.
@@ -607,88 +700,6 @@ def check_exprs(exprs: List[_JSONObject]) -> List[_JSONObject]: :raise QAPISemError: When any expression fails validation. :return: The same list of expressions (now modified). """ - for expr_elem in exprs: - # Expression - assert isinstance(expr_elem['expr'], dict) - for key in expr_elem['expr'].keys(): - assert isinstance(key, str) - expr: _JSONObject = expr_elem['expr'] - - # QAPISourceInfo - assert isinstance(expr_elem['info'], QAPISourceInfo) - info: QAPISourceInfo = expr_elem['info'] - - # Optional[QAPIDoc] - tmp = expr_elem.get('doc') - assert tmp is None or isinstance(tmp, QAPIDoc) - doc: Optional[QAPIDoc] = tmp - - if 'include' in expr: - continue - - metas = expr.keys() & {'enum', 'struct', 'union', 'alternate', - 'command', 'event'} - if len(metas) != 1: - raise QAPISemError( - info, - "expression must have exactly one key" - " 'enum', 'struct', 'union', 'alternate'," - " 'command', 'event'") - meta = metas.pop() - - check_name_is_str(expr[meta], info, "'%s'" % meta) - name = cast(str, expr[meta]) - info.set_defn(meta, name) - check_defn_name_str(name, info, meta) - - if doc: - if doc.symbol != name: - raise QAPISemError( - info, "documentation comment is for '%s'" % doc.symbol) - doc.check_expr(expr) - elif info.pragma.doc_required: - raise QAPISemError(info, - "documentation comment required") - - if meta == 'enum': - check_keys(expr, info, meta, - ['enum', 'data'], ['if', 'features', 'prefix']) - check_enum(expr, info) - elif meta == 'union': - check_keys(expr, info, meta, - ['union', 'base', 'discriminator', 'data'], - ['if', 'features']) - normalize_members(expr.get('base')) - normalize_members(expr['data']) - check_union(expr, info) - elif meta == 'alternate': - check_keys(expr, info, meta, - ['alternate', 'data'], ['if', 'features']) - normalize_members(expr['data']) - check_alternate(expr, info) - elif meta == 'struct': - check_keys(expr, info, meta, - ['struct', 'data'], ['base', 'if', 'features']) - normalize_members(expr['data']) - check_struct(expr, info) - elif meta == 'command': - check_keys(expr, info, meta, - ['command'], - ['data', 'returns', 'boxed', 'if', 'features', - 'gen', 'success-response', 'allow-oob', - 'allow-preconfig', 'coroutine']) - normalize_members(expr.get('data')) - check_command(expr, info) - elif meta == 'event': - check_keys(expr, info, meta, - ['event'], ['data', 'boxed', 'if', 'features']) - normalize_members(expr.get('data')) - check_event(expr, info) - else: - assert False, 'unexpected meta type' - - check_if(expr, info, meta) - check_features(expr.get('features'), info) - check_flags(expr, info) - + for expr in exprs: + check_expr(expr) return exprs
Signature · 1 line
2.39.0
Re: [PATCH v3 3/7] qapi/expr: Split check_expr out from check_exprs
Quoted 6 lines · John Snow
Primarily, this reduces a nesting level of a particularly long block. It's mostly code movement, but a new docstring is created.
It also has the effect of creating a fairly convenient "catch point" in
check_exprsfor exception handling without making the nesting level even worse.
I don't get this sentence, I'm afraid. It could tip the balance...
Quoted 10 lines
Signed-off-by John Snow <[email protected]>---
This patch was originally written as part of my effort to factor out QAPISourceInfo from this file by having
expr.pyraise a simple exception, then catch and wrap it at the higher level.This series doesn't do that anymore, but reducing the nesting level still seemed subjectively nice. It's not crucial.
Is reducing the nesting level worth sacrificing git-blame?
If we decide it is: there are two "Checked in check_exprs" comments in need of an update.
Quoted 1 line
Signed-off-by John Snow <[email protected]>
Harmless accident :)
Re: [PATCH v3 3/7] qapi/expr: Split check_expr out from check_exprs
In reply to John SnowAnother observation...
Quoted 32 lines · John Snow
Primarily, this reduces a nesting level of a particularly long block. It's mostly code movement, but a new docstring is created.
It also has the effect of creating a fairly convenient "catch point" in
check_exprsfor exception handling without making the nesting level even worse.Signed-off-by John Snow <[email protected]>---
This patch was originally written as part of my effort to factor out QAPISourceInfo from this file by having
expr.pyraise a simple exception, then catch and wrap it at the higher level.This series doesn't do that anymore, but reducing the nesting level still seemed subjectively nice. It's not crucial.
Signed-off-by John Snow <[email protected]>--- scripts/qapi/expr.py | 179 +++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 84 deletions(-)scripts/qapi/expr.py +1
-0@@ -595,6 +595,99 @@ def check_event(expr: _JSONObject, info: QAPISourceInfo) -> None: check_type(args, info, "'data'", allow_dict=not boxed) +def check_expr(expr_elem: _JSONObject) -> None:
[...]
Quoted 3 lines
def
check_exprs(exprs: List[_JSONObject])-> List[_JSONObject]:
"""
Validate and normalize a list of parsed QAPI schema expressions.
The typing is kind of wrong.
The list is the value of QAPISchemaParser.exprs, which is (losely) typed as List[Dict[str, object]]. It is actually a dict mapping
'expr' -> _ExprValue
'info' -> QAPISourceInfo
'doc' -> Optional[QAPIDoc]
Thet's not what _JSONObject is! Its doc string describes it as "Deserialized JSON objects as returned by the parser", i.e. the same as _ExprValue.
Works only because _JSONObject is a dict mapping arbitrary keys to _JSONObject, str or bool.
This patch spreads the flawed typing to the new function.
Gets healed later in the series.
[...]
Re: [PATCH v3 3/7] qapi/expr: Split check_expr out from check_exprs
Quoted 11 lines · Markus Armbruster
Another observation...
Quoted 6 lines · John Snow
Primarily, this reduces a nesting level of a particularly long block. It's mostly code movement, but a new docstring is created.
It also has the effect of creating a fairly convenient "catch point" in
check_exprsfor exception handling without making the nesting level even worse.
What I mean is: If you want to handle exceptions without this patch, a try/catch will add another nesting level to this hundred line function.
By splitting it, you can use the outer looping function as a choke point to write the handler.
I had a branch once where I use this fact to stop routing "info" into 99% of expr.py. I did this to use an off the shelf JSON validator while preserving your custom source info error reporting that identifies the precise location of the error.
I took that out for now, in the interest of staying focused on the minimum viable to achieve strict typing, but found this change to be helpful anyway.
Admit that it does muddy the waters with git blame, but... my calculus on that might just be different from yours.
(To me, git blame is almost always something I have to trace back through several refactors anyway, so I see the battle as lost before I started.)
I can omit this patch for now if you'd like, it isn't crucial. I just think I'd still "kinda like to have it". I'll leave it up to you.
Quoted 5 lines
Quoted 5 lines
Signed-off-by John Snow <[email protected]>---
Quoted 9 lines
Quoted 8 lines
This patch was originally written as part of my effort to factor out QAPISourceInfo from this file by having
expr.pyraise a simple exception, then catch and wrap it at the higher level.This series doesn't do that anymore, but reducing the nesting level still seemed subjectively nice. It's not crucial.
Signed-off-by John Snow <[email protected]>
Whoops, script doesn't understand when I add notes.
Quoted 34 lines
---
scripts/qapi/expr.py def check_event(expr: _JSONObject, info:
Quoted 8 lines
scripts/qapi/expr.py | 179 +++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 84 deletions(-)scripts/qapi/expr.py +0
-0@@ -595,6 +595,99 @@ def check_event(expr: _JSONObject, info:QAPISourceInfo) -> None:
Quoted 4 lines
scripts/qapi/expr.py +1
-0check_type(args, info, "'data'", allow_dict=not boxed) +def check_expr(expr_elem: _JSONObject) -> None:[...]
Quoted 3 lines
def
check_exprs(exprs: List[_JSONObject])-> List[_JSONObject]:
"""
Validate and normalize a list of parsed QAPI schema expressions.The typing is kind of wrong.
The list is the value of QAPISchemaParser.exprs, which is (losely) typed as List[Dict[str, object]]. It is actually a dict mapping
'expr' -> _ExprValue 'info' -> QAPISourceInfo 'doc' -> Optional[QAPIDoc]Thet's not what
_JSONObjectis! Its doc string describes it as "Deserialized JSON objects as returned by the parser", i.e. the same as_ExprValue.
Quoted 7 lines
Works only because
_JSONObjectis a dict mapping arbitrary keys to_JSONObject, str or bool.This patch spreads the flawed typing to the new function.
Gets healed later in the series.
Oops...!
Symptom of patch reordering that happened some time ago, I think. Mea culpa. Will fix.
(Possibly with some ugly intermediate type that goes away by end of series.)
Quoted 3 lines
[...]
Re: [PATCH v3 3/7] qapi/expr: Split check_expr out from check_exprs
Quoted 20 lines · John Snow
--0000000000003b01fe05f45a096a
Content-Type: text/plain; charset="UTF-8"Quoted 11 lines · Markus Armbruster
Another observation...
Quoted 6 lines · John Snow
Primarily, this reduces a nesting level of a particularly long block. It's mostly code movement, but a new docstring is created.
It also has the effect of creating a fairly convenient "catch point" in
check_exprsfor exception handling without making the nesting level even worse.What I mean is: If you want to handle exceptions without this patch, a try/catch will add another nesting level to this hundred line function.
Quoted 7 lines
By splitting it, you can use the outer looping function as a choke point to write the handler.
I had a branch once where I use this fact to stop routing "info" into 99% of
expr.py. I did this to use an off the shelf JSON validator while preserving your custom source info error reporting that identifies the precise location of the error.
When you have to reindent anyway, the drawback "makes git-blame less useful" evaporates. When the reindent is due to another level of nesting, the benefit "reduces nesting" becomes more valuable.
Quoted 11 lines
I took that out for now, in the interest of staying focused on the minimum viable to achieve strict typing, but found this change to be helpful anyway.
Admit that it does muddy the waters with git blame, but... my calculus on that might just be different from yours.
(To me, git blame is almost always something I have to trace back through several refactors anyway, so I see the battle as lost before I started.)
I can omit this patch for now if you'd like, it isn't crucial. I just think I'd still "kinda like to have it". I'll leave it up to you.
I'd prefer to shelve it for now. Bring it back when we have to reindent anyway.
Quoted 58 lines
Quoted 4 lines
Quoted 4 lines
Signed-off-by John Snow <[email protected]>---
Quoted 9 lines
Quoted 8 lines
This patch was originally written as part of my effort to factor out QAPISourceInfo from this file by having
expr.pyraise a simple exception, then catch and wrap it at the higher level.This series doesn't do that anymore, but reducing the nesting level still seemed subjectively nice. It's not crucial.
Signed-off-by John Snow <[email protected]>Whoops, script doesn't understand when I add notes.
Quoted 34 lines
---
scripts/qapi/expr.py def check_event(expr: _JSONObject, info:
Quoted 8 lines
scripts/qapi/expr.py | 179 +++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 84 deletions(-)scripts/qapi/expr.py +0
-0@@ -595,6 +595,99 @@ def check_event(expr: _JSONObject, info:QAPISourceInfo) -> None:
Quoted 4 lines
scripts/qapi/expr.py +1
-0check_type(args, info, "'data'", allow_dict=not boxed) +def check_expr(expr_elem: _JSONObject) -> None:[...]
Quoted 3 lines
def
check_exprs(exprs: List[_JSONObject])-> List[_JSONObject]:
"""
Validate and normalize a list of parsed QAPI schema expressions.The typing is kind of wrong.
The list is the value of QAPISchemaParser.exprs, which is (losely) typed as List[Dict[str, object]]. It is actually a dict mapping
'expr' -> _ExprValue 'info' -> QAPISourceInfo 'doc' -> Optional[QAPIDoc]Thet's not what
_JSONObjectis! Its doc string describes it as "Deserialized JSON objects as returned by the parser", i.e. the same as_ExprValue.Quoted 7 lines
Works only because
_JSONObjectis a dict mapping arbitrary keys to_JSONObject, str or bool.This patch spreads the flawed typing to the new function.
Gets healed later in the series.
Quoted 6 lines
Oops...!
Symptom of patch reordering that happened some time ago, I think. Mea culpa. Will fix.
(Possibly with some ugly intermediate type that goes away by end of series.)
Only if it's dead easy.
Simply have the commit message point out there's problem being fixed?
Quoted 1 line
Quoted 1 line
[...]
Re: [PATCH v3 3/7] qapi/expr: Split check_expr out from check_exprs
Quoted 49 lines · Markus Armbruster
Quoted 4 lines · John Snow
--0000000000003b01fe05f45a096a
Content-Type: text/plain; charset="UTF-8"On Fri, Feb 10, 2023, 7:33 AM Markus Armbruster <[email protected]>
Quoted 9 lines · wrote:
Quoted 9 lines
Another observation...
Quoted 4 lines · John Snow
Primarily, this reduces a nesting level of a particularly long block. It's mostly code movement, but a new docstring is created.
It also has the effect of creating a fairly convenient "catch point"
in
Quoted 1 line
Quoted 1 line
Quoted 1 line
check_exprsfor exception handling without making the nesting leveleven
Quoted 7 lines
Quoted 2 lines
Quoted 1 line
worse.
What I mean is: If you want to handle exceptions without this patch, a try/catch will add another nesting level to this hundred line function.
By splitting it, you can use the outer looping function as a choke point
to
Quoted 6 lines
write the handler.
I had a branch once where I use this fact to stop routing "info" into 99% of
expr.py. I did this to use an off the shelf JSON validator while preserving your custom source info error reporting that identifies the precise location of the error.When you have to reindent anyway, the drawback "makes git-blame less useful" evaporates. When the reindent is due to another level of nesting, the benefit "reduces nesting" becomes more valuable.
Quoted 1 line
I took that out for now, in the interest of staying focused on the
minimum
Quoted 1 line
viable to achieve strict typing, but found this change to be helpful
anyway.
Quoted 7 lines
Admit that it does muddy the waters with git blame, but... my calculus on that might just be different from yours.
(To me, git blame is almost always something I have to trace back through several refactors anyway, so I see the battle as lost before I started.)
Quoted 7 lines
Quoted 1 line
I can omit this patch for now if you'd like, it isn't crucial. I just
think
Quoted 1 line
I'd still "kinda like to have it". I'll leave it up to you.
I'd prefer to shelve it for now. Bring it back when we have to reindent anyway.
OK. Let me peek ahead to some of my other pending patches and just confirm there's no stronger reason to keep it...
(/me still ~kiiiinda~ wants it, but admits its not seemingly crucial atm)
Quoted 74 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Signed-off-by John Snow <[email protected]>---
This patch was originally written as part of my effort to factor out QAPISourceInfo from this file by having
expr.pyraise a simple exception, then catch and wrap it at the higher level.Quoted 10 lines
Quoted 5 lines
Quoted 4 lines
This series doesn't do that anymore, but reducing the nesting level still seemed subjectively nice. It's not crucial.
Signed-off-by John Snow <[email protected]>Whoops, script doesn't understand when I add notes.
Quoted 2 lines
---
Quoted 1 line
scripts/qapi/expr.py | 179+++++++++++++++++++++++--------------------
Quoted 40 lines
Quoted 32 lines
scripts/qapi/expr.py def check_event(expr: _JSONObject, info:
Quoted 7 lines
1 file changed, 95 insertions(+), 84 deletions(-)scripts/qapi/expr.py +0
-0@@ -595,6 +595,99 @@ def check_event(expr: _JSONObject, info:QAPISourceInfo) -> None:
Quoted 4 lines
scripts/qapi/expr.py +1
-0check_type(args, info, "'data'", allow_dict=not boxed) +def check_expr(expr_elem: _JSONObject) -> None:[...]
Quoted 3 lines
def
check_exprs(exprs: List[_JSONObject])-> List[_JSONObject]:
"""
Validate and normalize a list of parsed QAPI schema expressions.The typing is kind of wrong.
The list is the value of QAPISchemaParser.exprs, which is (losely) typed as List[Dict[str, object]]. It is actually a dict mapping
'expr' -> _ExprValue 'info' -> QAPISourceInfo 'doc' -> Optional[QAPIDoc]Thet's not what
_JSONObjectis! Its doc string describes it as "Deserialized JSON objects as returned by the parser", i.e. the same as_ExprValue.Quoted 7 lines
Works only because
_JSONObjectis a dict mapping arbitrary keys to_JSONObject, str or bool.This patch spreads the flawed typing to the new function.
Gets healed later in the series.
Quoted 6 lines
Oops...!
Symptom of patch reordering that happened some time ago, I think. Mea culpa. Will fix.
(Possibly with some ugly intermediate type that goes away by end of
series.)
Only if it's dead easy.
Simply have the commit message point out there's problem being fixed?
Quoted 1 line
Quoted 1 line
[...]
PATCH v3 4/7 qapi/expr: add typing workaround for AbstractSet
In reply to John Snowmypy can only narrow the type of Mapping[str, ...].keys() & Set[str] to AbstractSet[str] and not a Set[str]. As a result, if the type of an expression is changed to a Mapping[], mypy is unsure if the .pop() is safe.
A forthcoming commit does exactly that, so wrap the expression in a set() constructor to force the intermediate expression to be resolved as a mutable type.
--- scripts/qapi/expr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/qapi/expr.py +2 -2
@@ -622,8 +622,8 @@ def check_expr(expr_elem: _JSONObject) -> None: if 'include' in expr: return - metas = expr.keys() & {'enum', 'struct', 'union', 'alternate', - 'command', 'event'} + metas = set(expr.keys() & { + 'enum', 'struct', 'union', 'alternate', 'command', 'event'}) if len(metas) != 1: raise QAPISemError( info,
Signature · 1 line
2.39.0
Re: [PATCH v3 4/7] qapi/expr: add typing workaround for AbstractSet
Quoted 19 lines · John Snow
mypy can only narrow the type of
Mapping[str, ...].keys() & Set[str]toAbstractSet[str]and not aSet[str]. As a result, if the type of an expression is changed to a Mapping[], mypy is unsure if the .pop() is safe.A forthcoming commit does exactly that, so wrap the expression in a
set()constructor to force the intermediate expression to be resolved as a mutable type.Signed-off-by John Snow <[email protected]>--- scripts/qapi/expr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/qapi/expr.py +0
-0
scripts/qapi/expr.py def check_expr(expr_elem: _JSONObject) -> None: · line 629
Quoted 10 lines
scripts/qapi/expr.py +2
-2@@ -622,8 +622,8 @@ def check_expr(expr_elem: _JSONObject) -> None: if 'include' in expr: return - metas = expr.keys() & {'enum', 'struct', 'union', 'alternate', - 'command', 'event'} + metas = set(expr.keys() & { + 'enum', 'struct', 'union', 'alternate', 'command', 'event'}) if len(metas) != 1: raise QAPISemError( info,
"expression must have exactly one key" " 'enum', 'struct', 'union', 'alternate'," " 'command', 'event'")
meta = metas.pop()
I'm mildly surprised that the result of operator & is considered immutable. How could it *not* be a freshly created set? Oh well.
Passing a set to set() is a no-op, so
Regardless, the code feels a bit too clever to me. It actually did already when I wrote it, but I the less clever ways I could think of were also much more verbose.
The code checks that exactly one of a set of keys is present, and which one it is. Any ideas?
Re: [PATCH v3 4/7] qapi/expr: add typing workaround for AbstractSet
Quoted 36 lines · Markus Armbruster
Quoted 19 lines · John Snow
mypy can only narrow the type of
Mapping[str, ...].keys() & Set[str]toAbstractSet[str]and not aSet[str]. As a result, if the type of an expression is changed to a Mapping[], mypy is unsure if the .pop() is safe.A forthcoming commit does exactly that, so wrap the expression in a
set()constructor to force the intermediate expression to be resolved as a mutable type.Signed-off-by John Snow <[email protected]>--- scripts/qapi/expr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/qapi/expr.py +0
-0scripts/qapi/expr.py def check_expr(expr_elem: _JSONObject) -> None: · line 629
Quoted 10 lines
scripts/qapi/expr.py +2
-2@@ -622,8 +622,8 @@ def check_expr(expr_elem: _JSONObject) -> None: if 'include' in expr: return - metas = expr.keys() & {'enum', 'struct', 'union', 'alternate', - 'command', 'event'} + metas = set(expr.keys() & { + 'enum', 'struct', 'union', 'alternate', 'command', 'event'}) if len(metas) != 1: raise QAPISemError( info,"expression must have exactly one key" " 'enum', 'struct', 'union', 'alternate'," " 'command', 'event'")
meta =metas.pop()
Quoted 3 lines
I'm mildly surprised that the result of operator & is considered immutable. How could it *not* be a freshly created set? Oh well.
I think because it's up to the LHS type to return the result.
Wait, maybe I can just switch the operand order, lol.
Quoted 11 lines
Passing a set to
set()is a no-op, soReviewed-by Markus Armbruster <[email protected]>Regardless, the code feels a bit too clever to me. It actually did already when I wrote it, but I the less clever ways I could think of were also much more verbose.
The code checks that exactly one of a set of keys is present, and which one it is. Any ideas?
Yeah, it feels too clever but I'm not sure I know something more elegant, really. I'll consider it while I address your feedback and prepare a respin.
Quoted 1 line
Re: [PATCH v3 4/7] qapi/expr: add typing workaround for AbstractSet
Quoted 41 lines · John Snow
Quoted 36 lines · Markus Armbruster
Quoted 19 lines · John Snow
mypy can only narrow the type of
Mapping[str, ...].keys() & Set[str]toAbstractSet[str]and not aSet[str]. As a result, if the type of an expression is changed to a Mapping[], mypy is unsure if the .pop() is safe.A forthcoming commit does exactly that, so wrap the expression in a
set()constructor to force the intermediate expression to be resolved as a mutable type.Signed-off-by John Snow <[email protected]>--- scripts/qapi/expr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/qapi/expr.py +0
-0scripts/qapi/expr.py def check_expr(expr_elem: _JSONObject) -> None: · line 629
Quoted 10 lines
scripts/qapi/expr.py +2
-2@@ -622,8 +622,8 @@ def check_expr(expr_elem: _JSONObject) -> None: if 'include' in expr: return - metas = expr.keys() & {'enum', 'struct', 'union', 'alternate', - 'command', 'event'} + metas = set(expr.keys() & { + 'enum', 'struct', 'union', 'alternate', 'command', 'event'}) if len(metas) != 1: raise QAPISemError( info,"expression must have exactly one key" " 'enum', 'struct', 'union', 'alternate'," " 'command', 'event'")
meta =metas.pop()
Quoted 8 lines
Quoted 3 lines
I'm mildly surprised that the result of operator & is considered immutable. How could it *not* be a freshly created set? Oh well.
I think because it's up to the LHS type to return the result.
Wait, maybe I can just switch the operand order, lol.
Good news, I can just switch the operand order. Looks cleaner.
Quoted 9 lines
Quoted 9 lines
Passing a set to
set()is a no-op, soReviewed-by Markus Armbruster <[email protected]>Regardless, the code feels a bit too clever to me. It actually did already when I wrote it, but I the less clever ways I could think of were also much more verbose.
Quoted 7 lines
Quoted 3 lines
The code checks that exactly one of a set of keys is present, and which one it is. Any ideas?
Yeah, it feels too clever but I'm not sure I know something more elegant, really. I'll consider it while I address your feedback and prepare a respin.
Still don't have brighter ideas. A problem for another time.
PATCH v3 5/7 qapi/parser: add QAPIExpression type
In reply to John SnowThis 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.
--- scripts/qapi/expr.py | 89 +++++++++++++++++------------------------- scripts/qapi/parser.py | 54 ++++++++++++++++--------- scripts/qapi/schema.py | 72 ++++++++++++++++++---------------- 3 files changed, 110 insertions(+), 105 deletions(-)
scripts/qapi/expr.py +36 -53
@@ -34,9 +34,9 @@ import re from typing import ( Collection, - Dict, Iterable, List, + Mapping, Optional, Union, cast,
@@ -44,7 +44,7 @@ from .common import c_name from .error import QAPISemError -from .parser import QAPIDoc +from .parser import QAPIExpression from .source import QAPISourceInfo
@@ -53,7 +53,7 @@ # here (and also not practical as long as mypy lacks recursive # types), because the purpose of this module is to interrogate that # type. -_JSONObject = Dict[str, object] +_JSONObject = Mapping[str, object] # See check_name_str(), below.
@@ -229,12 +229,11 @@ def pprint(elems: Iterable[str]) -> str: pprint(unknown), pprint(allowed))) -def check_flags(expr: _JSONObject, info: QAPISourceInfo) -> None: +def check_flags(expr: QAPIExpression) -> None: """ Ensure flag members (if present) have valid values. :param expr: The expression to validate. - :param info: QAPI schema source file information. :raise QAPISemError: When certain flags have an invalid value, or when
@@ -243,18 +242,18 @@ def check_flags(expr: _JSONObject, info: QAPISourceInfo) -> None: for key in ('gen', 'success-response'): if key in expr and expr[key] is not False: raise QAPISemError( - info, "flag '%s' may only use false value" % key) + expr.info, "flag '%s' may only use false value" % key) for key in ('boxed', 'allow-oob', 'allow-preconfig', 'coroutine'): if key in expr and expr[key] is not True: raise QAPISemError( - info, "flag '%s' may only use true value" % key) + expr.info, "flag '%s' may only use true value" % key) if 'allow-oob' in expr and 'coroutine' in expr: # This is not necessarily a fundamental incompatibility, but # we don't have a use case and the desired semantics isn't # obvious. The simplest solution is to forbid it until we get # a use case for it. - raise QAPISemError(info, "flags 'allow-oob' and 'coroutine' " - "are incompatible") + raise QAPISemError( + expr.info, "flags 'allow-oob' and 'coroutine' are incompatible") def check_if(expr: _JSONObject, info: QAPISourceInfo, source: str) -> None:
@@ -447,12 +446,11 @@ def check_features(features: Optional[object], check_if(feat, info, source) -def check_enum(expr: _JSONObject, info: QAPISourceInfo) -> None: +def check_enum(expr: QAPIExpression) -> None: """ Normalize and validate this expression as an ``enum`` definition. :param expr: The expression to validate. - :param info: QAPI schema source file information. :raise QAPISemError: When ``expr`` is not a valid ``enum``. :return: None, ``expr`` is normalized in-place as needed.
@@ -460,6 +458,7 @@ def check_enum(expr: _JSONObject, info: QAPISourceInfo) -> None: name = expr['enum'] members = expr['data'] prefix = expr.get('prefix') + info = expr.info if not isinstance(members, list): raise QAPISemError(info, "'data' must be an array")
@@ -486,12 +485,11 @@ def check_enum(expr: _JSONObject, info: QAPISourceInfo) -> None: check_features(member.get('features'), info) -def check_struct(expr: _JSONObject, info: QAPISourceInfo) -> None: +def check_struct(expr: QAPIExpression) -> None: """ Normalize and validate this expression as a ``struct`` definition. :param expr: The expression to validate. - :param info: QAPI schema source file information. :raise QAPISemError: When ``expr`` is not a valid ``struct``. :return: None, ``expr`` is normalized in-place as needed.
@@ -499,16 +497,15 @@ def check_struct(expr: _JSONObject, info: QAPISourceInfo) -> None: name = cast(str, expr['struct']) # Checked in check_exprs members = expr['data'] - check_type(members, info, "'data'", allow_dict=name) - check_type(expr.get('base'), info, "'base'") + check_type(members, expr.info, "'data'", allow_dict=name) + check_type(expr.get('base'), expr.info, "'base'") -def check_union(expr: _JSONObject, info: QAPISourceInfo) -> None: +def check_union(expr: QAPIExpression) -> None: """ Normalize and validate this expression as a ``union`` definition. :param expr: The expression to validate. - :param info: QAPI schema source file information. :raise QAPISemError: when ``expr`` is not a valid ``union``. :return: None, ``expr`` is normalized in-place as needed.
@@ -517,6 +514,7 @@ def check_union(expr: _JSONObject, info: QAPISourceInfo) -> None: base = expr['base'] discriminator = expr['discriminator'] members = expr['data'] + info = expr.info check_type(base, info, "'base'", allow_dict=name) check_name_is_str(discriminator, info, "'discriminator'")
@@ -531,17 +529,17 @@ def check_union(expr: _JSONObject, info: QAPISourceInfo) -> None: check_type(value['type'], info, source, allow_array=not base) -def check_alternate(expr: _JSONObject, info: QAPISourceInfo) -> None: +def check_alternate(expr: QAPIExpression) -> None: """ Normalize and validate this expression as an ``alternate`` definition. :param expr: The expression to validate. - :param info: QAPI schema source file information. :raise QAPISemError: When ``expr`` is not a valid ``alternate``. :return: None, ``expr`` is normalized in-place as needed. """ members = expr['data'] + info = expr.info if not members: raise QAPISemError(info, "'data' must not be empty")
@@ -557,12 +555,11 @@ def check_alternate(expr: _JSONObject, info: QAPISourceInfo) -> None: check_type(value['type'], info, source, allow_array=True) -def check_command(expr: _JSONObject, info: QAPISourceInfo) -> None: +def check_command(expr: QAPIExpression) -> None: """ Normalize and validate this expression as a ``command`` definition. :param expr: The expression to validate. - :param info: QAPI schema source file information. :raise QAPISemError: When ``expr`` is not a valid ``command``. :return: None, ``expr`` is normalized in-place as needed.
@@ -572,17 +569,16 @@ def check_command(expr: _JSONObject, info: QAPISourceInfo) -> None: boxed = expr.get('boxed', False) if boxed and args is None: - raise QAPISemError(info, "'boxed': true requires 'data'") - check_type(args, info, "'data'", allow_dict=not boxed) - check_type(rets, info, "'returns'", allow_array=True) + raise QAPISemError(expr.info, "'boxed': true requires 'data'") + check_type(args, expr.info, "'data'", allow_dict=not boxed) + check_type(rets, expr.info, "'returns'", allow_array=True) -def check_event(expr: _JSONObject, info: QAPISourceInfo) -> None: +def check_event(expr: QAPIExpression) -> None: """ Normalize and validate this expression as an ``event`` definition. :param expr: The expression to validate. - :param info: QAPI schema source file information. :raise QAPISemError: When ``expr`` is not a valid ``event``. :return: None, ``expr`` is normalized in-place as needed.
@@ -591,37 +587,23 @@ def check_event(expr: _JSONObject, info: QAPISourceInfo) -> None: boxed = expr.get('boxed', False) if boxed and args is None: - raise QAPISemError(info, "'boxed': true requires 'data'") - check_type(args, info, "'data'", allow_dict=not boxed) + raise QAPISemError(expr.info, "'boxed': true requires 'data'") + check_type(args, expr.info, "'data'", allow_dict=not boxed) -def check_expr(expr_elem: _JSONObject) -> None: +def check_expr(expr: QAPIExpression) -> None: """ Validate and normalize a parsed QAPI schema expression. - :param expr_elem: The parsed expression to normalize and validate. + :param expr: The parsed expression to normalize and validate. :raise QAPISemError: When this expression fails validation. :return: None, The expression is normalized in-place as needed. """ - # Expression - assert isinstance(expr_elem['expr'], dict) - for key in expr_elem['expr'].keys(): - assert isinstance(key, str) - expr: _JSONObject = expr_elem['expr'] - - # QAPISourceInfo - assert isinstance(expr_elem['info'], QAPISourceInfo) - info: QAPISourceInfo = expr_elem['info'] - - # Optional[QAPIDoc] - tmp = expr_elem.get('doc') - assert tmp is None or isinstance(tmp, QAPIDoc) - doc: Optional[QAPIDoc] = tmp - if 'include' in expr: return + info = expr.info metas = set(expr.keys() & { 'enum', 'struct', 'union', 'alternate', 'command', 'event'}) if len(metas) != 1:
@@ -637,6 +619,7 @@ def check_expr(expr_elem: _JSONObject) -> None: info.set_defn(meta, name) check_defn_name_str(name, info, meta) + doc = expr.doc if doc: if doc.symbol != name: raise QAPISemError(
@@ -649,24 +632,24 @@ def check_expr(expr_elem: _JSONObject) -> None: if meta == 'enum': check_keys(expr, info, meta, ['enum', 'data'], ['if', 'features', 'prefix']) - check_enum(expr, info) + check_enum(expr) elif meta == 'union': check_keys(expr, info, meta, ['union', 'base', 'discriminator', 'data'], ['if', 'features']) normalize_members(expr.get('base')) normalize_members(expr['data']) - check_union(expr, info) + check_union(expr) elif meta == 'alternate': check_keys(expr, info, meta, ['alternate', 'data'], ['if', 'features']) normalize_members(expr['data']) - check_alternate(expr, info) + check_alternate(expr) elif meta == 'struct': check_keys(expr, info, meta, ['struct', 'data'], ['base', 'if', 'features']) normalize_members(expr['data']) - check_struct(expr, info) + check_struct(expr) elif meta == 'command': check_keys(expr, info, meta, ['command'],
@@ -674,21 +657,21 @@ def check_expr(expr_elem: _JSONObject) -> None: 'gen', 'success-response', 'allow-oob', 'allow-preconfig', 'coroutine']) normalize_members(expr.get('data')) - check_command(expr, info) + check_command(expr) elif meta == 'event': check_keys(expr, info, meta, ['event'], ['data', 'boxed', 'if', 'features']) normalize_members(expr.get('data')) - check_event(expr, info) + check_event(expr) else: assert False, 'unexpected meta type' check_if(expr, info, meta) check_features(expr.get('features'), info) - check_flags(expr, info) + check_flags(expr) -def check_exprs(exprs: List[_JSONObject]) -> List[_JSONObject]: +def check_exprs(exprs: List[QAPIExpression]) -> List[QAPIExpression]: """ Validate and normalize a list of parsed QAPI schema expressions.
scripts/qapi/parser.py +35 -19
102 lines not on this page · raw message
scripts/qapi/schema.py +39 -33
180 lines not on this page · raw message
Signature · 1 line
2.39.0
Re: [PATCH v3 5/7] qapi/parser: add QAPIExpression type
Quoted 20 lines · John Snow
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.pyto match.This allows the types we specify in
parser.pyto be "remembered" all the way throughexpr.pyand intoschema.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()andcheck_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]"
Quoted 7 lines
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 <[email protected]>---
Looks good to me, just one question:
[...]
Quoted 37 lines
scripts/qapi/parser.py +6
-7@@ -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.
scripts/qapi/parser.py line 57
Quoted 10 lines
scripts/qapi/parser.py +10
-0+ + +# 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?
Quoted 10 lines
scripts/qapi/parser.py +7
-0+ 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):
[...]
Re: [PATCH v3 5/7] qapi/parser: add QAPIExpression type
Quoted 75 lines · Markus Armbruster
Quoted 20 lines · John Snow
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.pyto match.This allows the types we specify in
parser.pyto be "remembered" all the way throughexpr.pyand intoschema.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()andcheck_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]"Quoted 7 lines
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 <[email protected]>---
Looks good to me, just one question:
[...]
Quoted 30 lines
scripts/qapi/parser.py +2
-4@@ -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]scripts/qapi/parser.py line 50
Quoted 10 lines
scripts/qapi/parser.py +7
-3-# 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 requiredversion.
Quoted 10 lines
scripts/qapi/parser.py line 57
Quoted 7 lines
scripts/qapi/parser.py +7
-0+if TYPE_CHECKING: + _UserDict = UserDict[str, object] +else: + _UserDict = UserDict + + +class QAPIExpression(_UserDict):Can we subclass dict instead?
...apparently yes?
class QAPIExpression(Dict[str, object]): ...
Only question I have is what the init signature ought to be, though keeping it as what I already have here apparently passes the type checker.
I think we lose the ability to do kwargs init in that case, but mypy doesn't give us a lyskov warning, so maybe it's fine? I'll have to play around with it. dict is implemented at the cpython level, so I'm not as sure of how subclassing it works. They document three distinct signatures for its initializer, which is also a python built-in.
I can rename that initialdata parameter too, pending what I discover above.
Quoted 14 lines
Quoted 10 lines
scripts/qapi/parser.py +7
-0+ 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):[...]
PATCH v3 6/7 qapi: remove _JSONObject
In reply to John SnowWe can remove this alias as it only has two usages now, and no longer pays for the confusion of "yet another type".
--- scripts/qapi/expr.py | 13 +++---------- scripts/qapi/parser.py | 5 ++--- 2 files changed, 5 insertions(+), 13 deletions(-)
scripts/qapi/expr.py +3 -10
@@ -48,14 +48,6 @@ from .source import QAPISourceInfo -# Deserialized JSON objects as returned by the parser. -# The values of this mapping are not necessary to exhaustively type -# here (and also not practical as long as mypy lacks recursive -# types), because the purpose of this module is to interrogate that -# type. -_JSONObject = Mapping[str, object] - - # See check_name_str(), below. valid_name = re.compile(r'(__[a-z0-9.-]+_)?' r'(x-)?'
@@ -192,7 +184,7 @@ def check_defn_name_str(name: str, info: QAPISourceInfo, meta: str) -> None: info, "%s name should not end in 'List'" % meta) -def check_keys(value: _JSONObject, +def check_keys(value: Mapping[str, object], info: QAPISourceInfo, source: str, required: Collection[str],
@@ -256,7 +248,8 @@ def check_flags(expr: QAPIExpression) -> None: expr.info, "flags 'allow-oob' and 'coroutine' are incompatible") -def check_if(expr: _JSONObject, info: QAPISourceInfo, source: str) -> None: +def check_if(expr: Mapping[str, object], + info: QAPISourceInfo, source: str) -> None: """ Validate the ``if`` member of an object.
scripts/qapi/parser.py +2 -3
@@ -42,9 +42,8 @@ _ExprValue = Union[List[object], Dict[str, object], str, bool] -# FIXME: Consolidate and centralize definitions for _ExprValue, -# JSONValue, and _JSONObject; currently scattered across several -# modules. +# FIXME: Consolidate and centralize definitions for _ExprValue and +# JSONValue; currently scattered across several modules. # 3.6 workaround: can be removed when Python 3.7+ is our required version.
Signature · 1 line
2.39.0
Re: [PATCH v3 6/7] qapi: remove _JSONObject
Quoted 4 lines · John Snow
We can remove this alias as it only has two usages now, and no longer pays for the confusion of "yet another type".
Signed-off-by John Snow <[email protected]>
PATCH v3 7/7 qapi: remove JSON value FIXME
In reply to John SnowWith the two major JSON-ish type hierarchies clarified for distinct purposes; QAPIExpression for parsed expressions and JSONValue for introspection data, remove this FIXME as no longer an action item.
In theory, it may be possible to define a completely agnostic one-size-fits-all JSON type hierarchy that any other user could borrow - in practice, it's tough to wrangle the differences between invariant, covariant and contravariant types: input and output parameters demand different properties of such a structure. As such, it's simply more trouble than it's worth.
So, declare this "done enough for now".
--- scripts/qapi/parser.py | 4 ---- 1 file changed, 4 deletions(-)
scripts/qapi/parser.py +0 -4
@@ -42,10 +42,6 @@ _ExprValue = Union[List[object], Dict[str, object], str, bool] -# FIXME: Consolidate and centralize definitions for _ExprValue and -# JSONValue; 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]
Signature · 1 line
2.39.0
Re: [PATCH v3 7/7] qapi: remove JSON value FIXME
Quoted 2 lines · John Snow
With the two major JSON-ish type hierarchies clarified for distinct purposes; QAPIExpression for parsed expressions and JSONValue for
The comment you remove talks about _ExprValue, not QAPIExpression.
Quoted 8 lines
introspection data, remove this FIXME as no longer an action item.
In theory, it may be possible to define a completely agnostic one-size-fits-all JSON type hierarchy that any other user could borrow - in practice, it's tough to wrangle the differences between invariant, covariant and contravariant types: input and output parameters demand different properties of such a structure. As such, it's simply more trouble than it's worth.
I think there's a weightier counter-argument struggling to get out.
As I explained under v2's cover letter, the two types represent things that are only superficially similar.
_ExprValue is the obvious stupid abstract syntax tree for the QAPI schema language, with str and bool leaves (QAPI doesn't support floating-point numbers), OrderedDict and list inner nodes. It is used for parser output.
QAPIExpression augments _ExprValue, adding a QAPISourceInfo (identifying the expression's source) and a QAPIDoc (the expressions documentation). It is used to represent QAPI top-level expressions.
JSONValue is an annotated JSON abstract syntax tree. QAPIExpression and _ExprValue are related to it only insofar as the QAPI schema language is (rather loosely) patterned after JSON.
Moreover, the two ASTs serve different purposes. QAPIExpression and _ExprValue represent *input*: they are produced by a parser and consumed by a semantic analyzer. JSONValue represents *output*: it is produced within a backend so we can separate the JSON text formatting aspect.
Consolidating these two ASTs makes no sense.
Suggest to work this argument into your commit message.
Quoted 1 line
So, declare this "done enough for now".
Agree.
Quoted 20 lines
Signed-off-by John Snow <[email protected]>--- scripts/qapi/parser.py | 4 ---- 1 file changed, 4 deletions(-)scripts/qapi/parser.py +0
-4@@ -42,10 +42,6 @@ _ExprValue = Union[List[object], Dict[str, object], str, bool] -# FIXME: Consolidate and centralize definitions for _ExprValue and -# JSONValue; 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]