aromanovich / jinja2schema Goto Github PK
View Code? Open in Web Editor NEWType inference for Jinja2 templates
License: Other
Type inference for Jinja2 templates
License: Other
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
)
)
]
)
Hi,
Since this library isn’t actively maintained, Are there any alternative libraries available!?
Hello, thanks for this package!
I wonder why its not Number instead of Scalar here
node_struct = Number.from_ast(ast.node, order_nr=config.ORDER_OBJECT.get_next())
instead of
node_struct = Scalar.from_ast(ast.node, order_nr=config.ORDER_OBJECT.get_next())
Thanks!
{% if logging is defined %}
{% if logging.console is defined %}
console: {{ logging.console }}
{% endif %}
{% if logging.monitor is defined %}
monitor: {{ logging.monitor }}
{% endif %}
{% endif %}
Results in:
>>> print(json.dumps(schema, indent=2))
{
"type": "object",
"properties": {
"logging": {
"title": "logging",
"type": "object",
"properties": {
"monitor": {
"title": "monitor",
"anyOf": [
{
"type": "boolean"
},
{
"type": "null"
},
{
"type": "number"
},
{
"type": "string"
}
]
},
"console": {
"title": "console",
"anyOf": [
{
"type": "boolean"
},
{
"type": "null"
},
{
"type": "number"
},
{
"type": "string"
}
]
}
},
"required": [
"monitor",
"console"
]
}
},
"required": []
}
Instead of:
"required": [
"monitor",
"console"
]
it rather should be empty.
In the visitors/stmt.py there is a couple spots that contain the iteritems
method. I hit this code when I have an includes
in my Jinja2 template. If I edit the code in my virtualenvironment to just use items
then I run into this error: stmt visitor for <class 'jinja2.nodes.Include'> is not found
.
The iteritems is here: https://github.com/aromanovich/jinja2schema/blob/master/jinja2schema/visitors/stmt.py#L49
And I run into the exception here: https://github.com/aromanovich/jinja2schema/blob/master/jinja2schema/visitors/stmt.py#L53
Thank you
In the situation of using jinja2schema + jsonforms (https://github.com/brutusin/json-forms) to generate an html form, is it possible to describe this field? Or generate "description" for this variable field title?
Currently using {{variable_description | default("description") }}, which generates a changeable form including the descriptions. It works but a non-changeable field is more desirable.
Thanks!
Apparently jinja2schema
does not expand argument expansion using tuples.
jinja2schema.infer
on the following throws an AttributeError: 'Tuple' object has no attribute 'name'
:
CREATE TABLE IF NOT EXISTS `{{ table_name }}` (
{%- for field, (type, not_null, is_primary_key, default_value) in fields.items() %}
`{{ field }}` {{ type }}
{%- if not_null %} NOT NULL{% endif -%}
{%- if is_primary_key %} PRIMARY KEY{% endif -%}
{%- if default_value %} DEFAULT {{ default_value }}{% endif -%}
{%- if not loop.last %},{% endif %}
{%- endfor %}
);
Apparently a few of the Jinja2 builtin filters are not supported by jinja2schema. I came across this while trying to explore jinja2schema and using my templates with "lower" filter.
The following simple snippet can be used to reproduce the issue.
from jinja2schema import infer
infer("{{var|lower}}")
Looking at the list of jinja2 builtin filters, and jinja2schema visit_filters method, here is the list of filters for which support is not available: 'lower', 'max', 'min', 'reverse', 'tojson', 'unique'.
Python version - 3.7.6
Jinja2 version - 2.11.2
jinja2schema versio - 0.1.4
Windows 10
How to use jinja2schema with custom filter?
Is there a way to list things for jinja2schema
to ignore as it goes through the AST?
I was able to get it working if I hacked in
@visits_expr(nodes.Call)
def visit_call(ast, ctx, macroses=None, config=default_config):
// my code to short circuit evaluating the node
if ast.node.node.name in ('loop'):
return Unknown(), Unknown()
in jinja2schema/visitors/expr.py
Stacktrace
return infer(template_source, config=Config(PACKAGE_NAME='documents', TEMPLATE_DIR=settings.TEMPLATE_PATH))
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/core.py", line 66, in infer
return infer_from_ast(parse(template), config=config, ignore_constants=True)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/core.py", line 49, in infer_from_ast
rv = visit(ast, {}, config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 19, in visit
structure = visit_many(node.body, macroses, config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 36, in visit_many
structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 14, in visit
structure = visit_stmt(node, macroses, config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 54, in visit_stmt
return visitor(ast, macroses, config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 59, in visit_for
body_struct = visit_many(ast.body, macroses, config, predicted_struct_cls=Scalar)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 36, in visit_many
structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 14, in visit
structure = visit_stmt(node, macroses, config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 54, in visit_stmt
return visitor(ast, macroses, config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 153, in visit_output
return visit_many(ast.nodes, macroses, config, predicted_struct_cls=Scalar)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 36, in visit_many
structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 17, in visit
_, structure = visit_expr(node, ctx, macroses, config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/expr.py", line 156, in visit_expr
return visitor(ast, ctx, macroses, config=config)
File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/expr.py", line 402, in visit_call
raise InvalidExpression(ast, '"{0}" call is not supported'.format(ast.node.attr))
jinja2schema.exceptions.InvalidExpression: line 367: "cycle" call is not supported
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.