[PATCH v2 05/21] qapi/parser: Assert lexer value is a string

John Snow posted 21 patches 4 years, 9 months ago
Maintainers: Michael Roth <michael.roth@amd.com>, Markus Armbruster <armbru@redhat.com>
There is a newer version of this series
[PATCH v2 05/21] qapi/parser: Assert lexer value is a string
Posted by John Snow 4 years, 9 months ago
The type checker can't narrow the type of the token value to string,
because it's only loosely correlated with the return token.

We know that a token of '#' should always have a "str" value.
Add an assertion.

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

diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index d620706fffb..ba17f1357ad 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -304,6 +304,7 @@ def get_doc(self, info):
         cur_doc = QAPIDoc(self, info)
         self.accept(False)
         while self.tok == '#':
+            assert isinstance(self.val, str)  # comment token MUST have str val
             if self.val.startswith('##'):
                 # End of doc comment
                 if self.val != '##':
-- 
2.30.2


Re: [PATCH v2 05/21] qapi/parser: Assert lexer value is a string
Posted by Markus Armbruster 4 years, 8 months ago
John Snow <jsnow@redhat.com> writes:

> The type checker can't narrow the type of the token value to string,
> because it's only loosely correlated with the return token.
>
> We know that a token of '#' should always have a "str" value.
> Add an assertion.
>
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  scripts/qapi/parser.py | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
> index d620706fffb..ba17f1357ad 100644
> --- a/scripts/qapi/parser.py
> +++ b/scripts/qapi/parser.py
> @@ -304,6 +304,7 @@ def get_doc(self, info):
>          cur_doc = QAPIDoc(self, info)
>          self.accept(False)
>          while self.tok == '#':
> +            assert isinstance(self.val, str)  # comment token MUST have str val

What does the comment add to the assertion?  Isn't it all obvious?  Just
wondering; if you genuinely think it isn't, I'm not going to argue.
Except about the long line, which you could easily avoid:

               assert isinstance(self.val, str)  # comment value MUST be str

>              if self.val.startswith('##'):
>                  # End of doc comment
>                  if self.val != '##':


Re: [PATCH v2 05/21] qapi/parser: Assert lexer value is a string
Posted by John Snow 4 years, 8 months ago
On 5/18/21 6:02 AM, Markus Armbruster wrote:
> John Snow <jsnow@redhat.com> writes:
> 
>> The type checker can't narrow the type of the token value to string,
>> because it's only loosely correlated with the return token.
>>
>> We know that a token of '#' should always have a "str" value.
>> Add an assertion.
>>
>> Signed-off-by: John Snow <jsnow@redhat.com>
>> ---
>>   scripts/qapi/parser.py | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
>> index d620706fffb..ba17f1357ad 100644
>> --- a/scripts/qapi/parser.py
>> +++ b/scripts/qapi/parser.py
>> @@ -304,6 +304,7 @@ def get_doc(self, info):
>>           cur_doc = QAPIDoc(self, info)
>>           self.accept(False)
>>           while self.tok == '#':
>> +            assert isinstance(self.val, str)  # comment token MUST have str val
> 
> What does the comment add to the assertion?  Isn't it all obvious?  Just
> wondering; if you genuinely think it isn't, I'm not going to argue.
> Except about the long line, which you could easily avoid:
> 

Yeah, I just suppose it's an artifact from when I was first reading this 
code. It wasn't necessarily obvious to me that comment tokens -- which 
are sometimes squelched -- must always have a str val that is non-None.

I felt like adding an assertion here required SOME kind of justification 
for WHY it was true. Since the val can be None *and* the lexer can omit 
comments, it seemed not expressly self-evident at the time.

Less obvious than, say, why 'tf' values will definitely be True/False. 
Maybe just my own brain bug.

I'm not as attached to it as other things at this point anymore, having 
spent more time in this file since I first wrote it.

Remove if you'd like. The commit message can perform the duty of 
explaining more deeply if people need.

>                 assert isinstance(self.val, str)  # comment value MUST be str
> 
>>               if self.val.startswith('##'):
>>                   # End of doc comment
>>                   if self.val != '##':