diff mbox series

[07/22] qapi/parser: assert object keys are strings

Message ID 20210422030720.3685766-8-jsnow@redhat.com (mailing list archive)
State New, archived
Headers show
Series qapi: static typing conversion, pt5a | expand

Commit Message

John Snow April 22, 2021, 3:07 a.m. UTC
The single quote token implies the value is a string. Assert this to be
the case.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 scripts/qapi/parser.py | 2 ++
 1 file changed, 2 insertions(+)

Comments

Markus Armbruster April 25, 2021, 7:27 a.m. UTC | #1
John Snow <jsnow@redhat.com> writes:

> The single quote token implies the value is a string. Assert this to be
> the case.
>
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  scripts/qapi/parser.py | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
> index 6b443b1247e..8d1fe0ddda5 100644
> --- a/scripts/qapi/parser.py
> +++ b/scripts/qapi/parser.py
> @@ -246,6 +246,8 @@ def get_members(self):
>              raise QAPIParseError(self, "expected string or '}'")
>          while True:
>              key = self.val
> +            assert isinstance(key, str)  # Guaranteed by tok == "'"
> +
>              self.accept()
>              if self.tok != ':':
>                  raise QAPIParseError(self, "expected ':'")

The assertion is correct, but I wonder why mypy needs it.  Can you help?
John Snow April 26, 2021, 5:46 p.m. UTC | #2
On 4/25/21 3:27 AM, Markus Armbruster wrote:
> John Snow <jsnow@redhat.com> writes:
> 
>> The single quote token implies the value is a string. Assert this to be
>> the case.
>>
>> Signed-off-by: John Snow <jsnow@redhat.com>
>> ---
>>   scripts/qapi/parser.py | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
>> index 6b443b1247e..8d1fe0ddda5 100644
>> --- a/scripts/qapi/parser.py
>> +++ b/scripts/qapi/parser.py
>> @@ -246,6 +246,8 @@ def get_members(self):
>>               raise QAPIParseError(self, "expected string or '}'")
>>           while True:
>>               key = self.val
>> +            assert isinstance(key, str)  # Guaranteed by tok == "'"
>> +
>>               self.accept()
>>               if self.tok != ':':
>>                   raise QAPIParseError(self, "expected ':'")
> 
> The assertion is correct, but I wonder why mypy needs it.  Can you help?
> 

The lexer value can also be True/False (Maybe None? I forget) based on 
the Token returned. Here, since the token was the single quote, we know 
that value must be a string.

Mypy has no insight into the correlation between the Token itself and 
the token value, because that relationship is not expressed via the type 
system.

--js
Markus Armbruster April 27, 2021, 6:13 a.m. UTC | #3
John Snow <jsnow@redhat.com> writes:

> On 4/25/21 3:27 AM, Markus Armbruster wrote:
>> John Snow <jsnow@redhat.com> writes:
>> 
>>> The single quote token implies the value is a string. Assert this to be
>>> the case.
>>>
>>> Signed-off-by: John Snow <jsnow@redhat.com>
>>> ---
>>>   scripts/qapi/parser.py | 2 ++
>>>   1 file changed, 2 insertions(+)
>>>
>>> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
>>> index 6b443b1247e..8d1fe0ddda5 100644
>>> --- a/scripts/qapi/parser.py
>>> +++ b/scripts/qapi/parser.py
>>> @@ -246,6 +246,8 @@ def get_members(self):
>>>               raise QAPIParseError(self, "expected string or '}'")
>>>           while True:
>>>               key = self.val
>>> +            assert isinstance(key, str)  # Guaranteed by tok == "'"
>>> +
>>>               self.accept()
>>>               if self.tok != ':':
>>>                   raise QAPIParseError(self, "expected ':'")
>> 
>> The assertion is correct, but I wonder why mypy needs it.  Can you help?
>> 
>
> The lexer value can also be True/False (Maybe None? I forget) based on 

Yes, None for tokens like '{'.

> the Token returned. Here, since the token was the single quote, we know 
> that value must be a string.
>
> Mypy has no insight into the correlation between the Token itself and 
> the token value, because that relationship is not expressed via the type 
> system.

I understand that mypy can't prove implications like if self.tok == "'",
then self.val is a str.

What I'm curious about is why key needs to be known to be str here.
Hmm, is it so return expr type-checks once you add -> OrderedDict[str,
object] to the function?
John Snow April 27, 2021, 2:15 p.m. UTC | #4
On 4/27/21 2:13 AM, Markus Armbruster wrote:
> John Snow <jsnow@redhat.com> writes:
> 
>> On 4/25/21 3:27 AM, Markus Armbruster wrote:
>>> John Snow <jsnow@redhat.com> writes:
>>>
>>>> The single quote token implies the value is a string. Assert this to be
>>>> the case.
>>>>
>>>> Signed-off-by: John Snow <jsnow@redhat.com>
>>>> ---
>>>>    scripts/qapi/parser.py | 2 ++
>>>>    1 file changed, 2 insertions(+)
>>>>
>>>> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
>>>> index 6b443b1247e..8d1fe0ddda5 100644
>>>> --- a/scripts/qapi/parser.py
>>>> +++ b/scripts/qapi/parser.py
>>>> @@ -246,6 +246,8 @@ def get_members(self):
>>>>                raise QAPIParseError(self, "expected string or '}'")
>>>>            while True:
>>>>                key = self.val
>>>> +            assert isinstance(key, str)  # Guaranteed by tok == "'"
>>>> +
>>>>                self.accept()
>>>>                if self.tok != ':':
>>>>                    raise QAPIParseError(self, "expected ':'")
>>>
>>> The assertion is correct, but I wonder why mypy needs it.  Can you help?
>>>
>>
>> The lexer value can also be True/False (Maybe None? I forget) based on
> 
> Yes, None for tokens like '{'.
> 
>> the Token returned. Here, since the token was the single quote, we know
>> that value must be a string.
>>
>> Mypy has no insight into the correlation between the Token itself and
>> the token value, because that relationship is not expressed via the type
>> system.
> 
> I understand that mypy can't prove implications like if self.tok == "'",
> then self.val is a str.
> 
> What I'm curious about is why key needs to be known to be str here.
> Hmm, is it so return expr type-checks once you add -> OrderedDict[str,
> object] to the function?
> 

Oh, yes.
diff mbox series

Patch

diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 6b443b1247e..8d1fe0ddda5 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -246,6 +246,8 @@  def get_members(self):
             raise QAPIParseError(self, "expected string or '}'")
         while True:
             key = self.val
+            assert isinstance(key, str)  # Guaranteed by tok == "'"
+
             self.accept()
             if self.tok != ':':
                 raise QAPIParseError(self, "expected ':'")