Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to analyze list comprehension code #412

Closed
lantlani opened this issue Jul 12, 2017 · 3 comments · Fixed by #447
Closed

Fail to analyze list comprehension code #412

lantlani opened this issue Jul 12, 2017 · 3 comments · Fixed by #447
Labels

Comments

@lantlani
Copy link

Chalice does not deploy below code with error message 'key_name'.
If I change the code from item['payload'][key_name] to item['payload']['data'], it works well.

I have used version 1.0.0b1.

@app.route('/data/{node_name}', methods=['GET'], cors=True, authorizer=authorizer)
def retrieve_data(node_name):
    key_name = 'data'
    response['Items'] = [
        { 'Timestamp' : 10, 'payload' : { 'data' : 1}},
        { 'Timestamp' : 11, 'payload' : { 'data' : 2}}
    ]
    stamp = [item['Timestamp'] for item in response['Items']]
    temps = [item['payload'][key_name] for item in response['Items']]
    hums = [item['payload'][key_name] for item in response['Items']]
@Segflow
Copy link

Segflow commented Jul 13, 2017

How response in created? is it global?
Running this code works for me

@app.route('/test')
def test_api():
    response = {}
    key_name = 'data'
    response['Items'] = [
        {'Timestamp' : 10, 'payload' : { 'data' : 1}},
        {'Timestamp' : 11, 'payload' : { 'data' : 2}}
    ]
    stamp = [item['Timestamp'] for item in response['Items']]
    temps = [item['payload'][key_name] for item in response['Items']]
    hums = [item['payload'][key_name] for item in response['Items']]
    
    return {
        'stamp': stamp,
        'temps': temps,
    }

Result: {"stamp": [10, 11], "temps": [1, 2]}

@lantlani
Copy link
Author

@Segflow thanks for your comment.
For your question, response is local variable.
Does your environment run gen-policy your code?

I could execute my code in local option, however, I cannot run 'deploy' or 'gen-policy'.
As far as I know,
Whenever I try to deploy this code to AWS, Chalice run analyse the code.
This process does not finished with above example.

@lantlani
Copy link
Author

lantlani commented Jul 19, 2017

I guess this issue is related to below codes.

def _get_matching_sub_namespace(self, name):
        # type: (str) -> symtable.SymbolTable
        namespaces = [
            t for t in self._symbol_table.get_sub_namespaces()
            if t.get_name() == name]
        if not namespaces:
            return
        # We're making a simplification and using the genexpr subnamespace.
        # This has potential to miss a client call but we don't do
        # inference on node.generators so this doesn't matter for now.
        child_scope = namespaces[0]
        return child_scope

To handle list comprehension for temps, it seems to use namespaces[1] instead of namespaces[0].
However, I do not have proper understanding for AST related code to fix this issue.
So I changed my code as below, I could avoid problem.
I am not sure this could be related to my python environment, which is anaconda virtual environment with python 3.6.

        temps = []
        hums = []
        for item in response['Items']:
            temps.append(item['payload'][temp_key])
            hums.append(item['payload'][hum_key])

stealthycoin pushed a commit to stealthycoin/chalice that referenced this issue Jul 31, 2017
The crash was caused by selecting the wrong child symbol table for the
list comprehension. Previously we were always selecting the first one in
the list of subtables. This is wrong if there is more than 1 list
comprehension, since each one gets its own subtable. This is works fine
if the list comprehension only uses symbols defined within itself or
symbols that are shared between it and the first list comprehension in
the enclosing scope, since that will resolve to the same value. The
specific error case we are fixing is when there is a list comprehension
that is not the first one that references a symbol from the enclosing
function scope that the first list comprehension does not reference.

Both the symbol tables and the ListComp node are annotated with a line
number attribute, by matching these up we can select the correct
subtable to go with our list comprehension. This is still naive, but is
better than what we had before.

fixes aws#412
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants