qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: Michael Roth <michael.roth@amd.com>,
	qemu-devel@nongnu.org, Eduardo Habkost <ehabkost@redhat.com>,
	Cleber Rosa <crosa@redhat.com>
Subject: Re: [PATCH v3 02/16] qapi/expr.py: Check for dict instead of OrderedDict
Date: Thu, 25 Feb 2021 15:04:00 -0500	[thread overview]
Message-ID: <b1ff239d-f4d5-26ed-0a94-13096e78a903@redhat.com> (raw)
In-Reply-To: <87r1l4toq0.fsf@dusky.pond.sub.org>

On 2/25/21 5:40 AM, Markus Armbruster wrote:
> John Snow <jsnow@redhat.com> writes:
> 
>> On 2/24/21 4:30 AM, Markus Armbruster wrote:
>>> John Snow <jsnow@redhat.com> writes:
>>>
>>>> OrderedDict is a subtype of dict, so we can check for a more general
>>>> form. These functions do not themselves depend on it being any
>>>> particular type.
>>>
>>> True.  The actual arguments can only be OrderedDict, though.  I think we
>>> refrained from relaxing to dict in these helpers because we felt
>>> "staying ordered" is clearer.
>>>
>>
>> As a habit, I tend towards declaring the least specific type possible
>> for input and declaring the most specific type possible for output.
> 
> This maximimizes generality, which can be quite worthwhile.  Maximizing
> generality by default is not a bad habit, I guess.  But cases exist
> where generality is not needed, and other considerations take
> precedence.
> 
>>> We're *this* close to mooting the point, because
>>>
>>>       Changed in version 3.7: Dictionary order is guaranteed to be
>>>       insertion order. This behavior was an implementation detail of
>>>       CPython from 3.6.
>>>
>>> https://docs.python.org/3.7/library/stdtypes.html
>>>
>>> Is messing with it necessary for later work?  If not, is it a worthwhile
>>> improvement?
>>>
>>
>> Not strictly necessary, but if the expression checkers here don't
>> *require* the type be an ordereddict, why bother to enforce that here?
>>
>> It's just a bid to slacken the type (my type hints will look for Dict,
>> not OrderedDict) and leave the use of OrderedDict as an "implementation
>> detail" that only parser.py knows about.
> 
> "Orderedness" is anything but a detail only parser.py knows about.
> 
> Example:
> 
>      { 'command': 'blockdev-insert-medium',
>        'data': { 'id': 'str',
>                  'node-name': 'str'} }
> 
> AST:
> 
>      OrderedDict([('command', 'blockdev-insert-medium'),
>                   ('data',
>                    OrderedDict([('id', {'type': 'str'}),
>                                 ('node-name', {'type': 'str'})]))])
> 
> For the inner dictionary, order matters, because the difference between
> 
>      void qmp_blockdev_insert_medium(const char *id, const char *node_name,
>                                      Error **errp);
> 
> and
> 
>      void qmp_blockdev_insert_medium(const char *node_name, const char *id,
>                                      Error **errp);
> 
> matters.
> 
> For the outer dictionary, order carries no semantic meaning.
> 
> My point is: parser.py fundamentally builds *ordered* dicts.  We're
> certainly free to relax them to more general types wherever
> "orderedness" is not needed.  However, one of the main aims of this
> typing exercise is to make the code easier to read, and I doubt making
> things more general helps there.
> 

I primarily I saw the writing on the wall that we *will* be abandoning 
the use of OrderedDict and so I preferred to type in terms of just Dict, 
using the fact that Dict < OrderedDict anyway, asserting that parser.py 
uses OrderedDict as an "implementation detail".

Later, parser.py may abandon its use of OrderedDict without changes to 
the rest of the code.

The alternative is to use OrderedDict everywhere here in expr.py, but I 
would *prefer* not to, as it will inhibit prototyping and 
experimentation efforts where we might use a parser that doesn't use 
OrderedDict.

What I absolutely did not want to do was type in terms of Dict[str, 
object] but then use isinstance checks for OrderedDict.

My preference is still, I think, to just go all-in on dict. I am 
personally comfortable trusting that parser.py creates an ordered 
implementation of the type.

As for these specific checks:

- normalize_members doesn't assert that it has an OrderedDict, it only 
normalizes *if* it gets one. I don't think this is helpful behavior.

- check_type has an error message that doesn't square with the check: we 
can give it a dict and it will pretend like we didn't give it one. I 
don't think that's helpful either.

> Related: the type aliases for the AST you define later in this series.
> I figure relaxing these to more general types where possible would
> actually reduce their value.  TopLevelExpression tells me more than
> dict.
> 
> I'm not against relaxing types per se.  Judicious relaxation is often
> useful to keep code more generally useful.  When to relax isn't always
> obvious.
> 
>> (I needed to change it for prototyping using an off-the-shelf parser, so
>> it was annoying to have it check for a stronger type if it doesn't
>> absolutely have to.)
> 
> If your off-the-shelf parse doesn't preserve order, it's not fit for the
> purpose :)
> 

It does, but in 3.6 that might be relying on CPython details. This is a 
pretty frustrating place to be in, support-wise.

>>>> Signed-off-by: John Snow <jsnow@redhat.com>
>>>> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
>>>> Reviewed-by: Cleber Rosa <crosa@redhat.com>



  reply	other threads:[~2021-02-25 20:06 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-23  0:33 [PATCH v3 00/16] qapi: static typing conversion, pt3 John Snow
2021-02-23  0:33 ` [PATCH v3 01/16] qapi/expr.py: Remove 'info' argument from nested check_if_str John Snow
2021-02-23  0:33 ` [PATCH v3 02/16] qapi/expr.py: Check for dict instead of OrderedDict John Snow
2021-02-24  9:30   ` Markus Armbruster
2021-02-24 21:23     ` John Snow
2021-02-25 10:40       ` Markus Armbruster
2021-02-25 20:04         ` John Snow [this message]
2021-03-01 16:48           ` Markus Armbruster
2021-02-23  0:33 ` [PATCH v3 03/16] qapi/expr.py: constrain incoming expression types John Snow
2021-02-24 10:01   ` Markus Armbruster
2021-02-24 21:46     ` John Snow
2021-02-25 11:56       ` Markus Armbruster
2021-02-25 20:43         ` John Snow
2021-03-02  5:23           ` Markus Armbruster
2021-02-23  0:33 ` [PATCH v3 04/16] qapi/expr.py: Add assertion for union type 'check_dict' John Snow
2021-02-24 10:35   ` Markus Armbruster
2021-02-24 21:54     ` John Snow
2021-03-24 21:09     ` John Snow
2021-03-25  5:46       ` Markus Armbruster
2021-03-25 19:42         ` John Snow
2021-02-23  0:33 ` [PATCH v3 05/16] qapi/expr.py: move string check upwards in check_type John Snow
2021-02-23  0:33 ` [PATCH v3 06/16] qapi/expr.py: Check type of 'data' member John Snow
2021-02-24 10:39   ` Markus Armbruster
2021-02-24 22:06     ` John Snow
2021-02-25 12:02       ` Markus Armbruster
2021-02-23  0:33 ` [PATCH v3 07/16] qapi/expr.py: Add casts in a few select cases John Snow
2021-02-24 12:32   ` Markus Armbruster
2021-02-24 22:24     ` John Snow
2021-02-25 12:07       ` Markus Armbruster
2021-02-25 22:10         ` John Snow
2021-02-23  0:34 ` [PATCH v3 08/16] qapi/expr.py: add type hint annotations John Snow
2021-02-24 15:27   ` Markus Armbruster
2021-02-24 22:30     ` John Snow
2021-02-25 12:08       ` Markus Armbruster
2021-02-25 13:56   ` Markus Armbruster
2021-02-25 20:54     ` John Snow
2021-03-02  5:29       ` Markus Armbruster
2021-02-23  0:34 ` [PATCH v3 09/16] qapi/expr.py: Consolidate check_if_str calls in check_if John Snow
2021-02-25 14:23   ` Markus Armbruster
2021-02-25 21:34     ` John Snow
2021-03-02  5:57       ` Markus Armbruster
2021-02-23  0:34 ` [PATCH v3 10/16] qapi/expr.py: Remove single-letter variable John Snow
2021-02-25 14:03   ` Markus Armbruster
2021-02-25 21:56     ` John Snow
2021-02-23  0:34 ` [PATCH v3 11/16] qapi/expr.py: enable pylint checks John Snow
2021-02-23  0:34 ` [PATCH v3 12/16] qapi/expr.py: Add docstrings John Snow
2021-02-23  0:34 ` [PATCH v3 13/16] qapi/expr.py: Modify check_keys to accept any Collection John Snow
2021-02-25 15:41   ` Markus Armbruster
2021-02-23  0:34 ` [PATCH v3 14/16] qapi/expr.py: Use tuples instead of lists for static data John Snow
2021-02-23  0:34 ` [PATCH v3 15/16] qapi/expr.py: move related checks inside check_xxx functions John Snow
2021-02-25 15:28   ` Markus Armbruster
2021-03-25  5:17     ` John Snow
2021-03-25 13:28       ` Markus Armbruster
2021-02-23  0:34 ` [PATCH v3 16/16] qapi/expr.py: Use an expression checker dispatch table John Snow

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=b1ff239d-f4d5-26ed-0a94-13096e78a903@redhat.com \
    --to=jsnow@redhat.com \
    --cc=armbru@redhat.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@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 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).