NationalSecurityAgency / lemongraph

Log-based transactional graph engine
Other
1.14k stars 149 forks source link

Fix `QuerySyntaxError.__repr__` #9

Closed delirious-lettuce closed 7 years ago

delirious-lettuce commented 7 years ago

The __repr__ method in QuerySyntaxError raises a TypeError.

# LemonGraph.MatchLGQL
class QuerySyntaxError(Exception):
    ...
    def __repr__(self):
        return 'QuerySyntaxError(%s, %s, %s)' % map(repr, (self.query, self.pos, self.message))
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In[38]: 'QuerySyntaxError(%s, %s, %s)' % map(repr, ('query', 1, 'message'))
Traceback (most recent call last):
    'QuerySyntaxError(%s, %s, %s)' % map(repr, ('query', 1, 'message'))
TypeError: not enough arguments for format string

# Python 2 `map` returns a list
In[39]: map(repr, ('query', 1, 'message'))
Out[39]: 
["'query'", '1', "'message'"]

In[40]: 'QuerySyntaxError(%s, %s, %s)' % ["'query'", '1', "'message'"]
Traceback (most recent call last):
    'QuerySyntaxError(%s, %s, %s)' % ["'query'", '1', "'message'"]
TypeError: not enough arguments for format string

# Tuple, not list, is needed
In[41]: 'QuerySyntaxError(%s, %s, %s)' % ("'query'", '1', "'message'")
Out[41]: 
"QuerySyntaxError('query', 1, 'message')"

# This works, but `map` creates an unnecessary list
In[42]: 'QuerySyntaxError(%s, %s, %s)' % tuple(map(repr, ('query', 1, 'message')))
Out[42]: 
"QuerySyntaxError('query', 1, 'message')"

# Using a tuple comprehension doesn't create that extra list
In[43]: 'QuerySyntaxError(%s, %s, %s)' % tuple(repr(arg) for arg in ('query', 1, 'message'))
Out[43]: 
"QuerySyntaxError('query', 1, 'message')"
delirious-lettuce commented 7 years ago

@NSA-LGDev2 ,

No problem!