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

fails to parse assignment of list filtered with 'list' built-in filter #14

Open
MikeCook9994 opened this issue Sep 26, 2018 · 0 comments

Comments

@MikeCook9994
Copy link

MikeCook9994 commented Sep 26, 2018

I have this one line of jinja2 that contains no html. It is a valid template that jinja2 is able to parse. I can additionally write some additional parts of the template to actually use the assigned value here and am able to render it

{% set filteredList = ThisIsAListOfObjects | selectattr("SomeProperty", "equalto", "SomeValue") | list %}

When inferring this I receive the following stack trace:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 116, in meet
    merge(self.predicted_struct, actual_struct)
  File "C:\Python34\lib\site-packages\jinja2schema\mergers.py", line 66, in merge
    raise MergeException(fst, snd)
jinja2schema.exceptions.MergeException: unnamed variable (used as scalar on lines 1) conflicts with unnamed variable (used as list on lines: )

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print(jinja2schema.infer(data))
  File "C:\Python34\lib\site-packages\jinja2schema\core.py", line 66, in infer
    return infer_from_ast(parse(template), config=config, ignore_constants=True)
  File "C:\Python34\lib\site-packages\jinja2schema\core.py", line 49, in infer_from_ast
    rv = visit(ast, {}, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\util.py", line 20, in visit
    structure = visit_many(node.body, macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\util.py", line 37, in visit_many
    structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\util.py", line 14, in visit
    structure = visit_stmt(node, macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\stmt.py", line 54, in visit_stmt
    return visitor(ast, macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\stmt.py", line 134, in visit_assign
    predicted_struct=Unknown.from_ast(var_ast, order_nr=config.ORDER_OBJECT.get_next())), macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 156, in visit_expr
    return visitor(ast, ctx, macroses, config=config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 534, in visit_filter
    ), macroses, config=config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 156, in visit_expr
    return visitor(ast, ctx, macroses, config=config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 509, in visit_filter
    ctx.meet(List(Unknown()), ast)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 118, in meet
    raise UnexpectedExpression(self.predicted_struct, actual_ast, actual_struct)
jinja2schema.exceptions.UnexpectedExpression: conflict on the line 1
got: AST node jinja2.nodes.Filter of structure [<unknown>]
expected structure: <scalar>

Here is the AST jinja2 gives from parsing:

Template(
  body=[
    Assign(
      target=Name(
        name='filteredList', 
        ctx='store'
      ), 
      node=Filter(
        node=Filter(
          node=Name(
            name='ThisIsAListOfObjects', 
            ctx='load'
          ), 
          name='selectattr', 
          args=[
            Const(value='SomeProperty'), 
            Const(value='equalto'), 
            Const(value='SomeValue')
          ], 
          kwargs=[], 
          dyn_args=None, 
          dyn_kwargs=None
        ), 
        name='list', 
        args=[], 
        kwargs=[], 
        dyn_args=None, 
        dyn_kwargs=None
      )
    )
  ]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant