-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[CT-1603] [Feature] Evaluate Jinja expressions in var to their returned type rather than string. #6382
Comments
Hey @elongl! Thanks for writing up what you are seeing, providing your work-around, and comparing it to the way tl;drAgreed that it is desirable for the I think the observed behavior is due to the implementation of Ultimately, I don't know if implementing the desired behavior is trivial, non-trivial, or "impossible" -- I'm presuming it depends on how the Jinja is rendered for Reproduction caseI was able to reproduce the same thing as you even when using vars:
is_prod: "{{ (target.name == 'prod') | as_bool }}" Reproduction caseCreated a file named {% if var("is_prod") is boolean %}
✅ {{ var("is_prod") }} is a boolean, just like we want 💪
{% else %}
❌ {{ var("is_prod") }} is not a boolean 🙁
{% endif %} Then: dbt compile -s vars_test And examining the output within
Other info
vars:
is_prod: false But will be a string with this config:
|
@dbeatty10 |
Thanks for being willing to contribute this feature if possible @elongl ! Some further research would be required to know the exact areas that are relevant. In the meantime, here's a couple $ git grep '"vars"' core
core/dbt/config/project.py: "vars": self.vars.to_dict(),
core/dbt/config/renderer.py: self[(k, "vars")] = _dict_if_none
core/dbt/config/renderer.py: if first == "vars":
core/dbt/config/runtime.py: cli_vars: Dict[str, Any] = parse_cli_vars(getattr(args, "vars", "{}"))
core/dbt/config/runtime.py: cli_vars: Dict[str, Any] = parse_cli_vars(getattr(args, "vars", "{}"))
core/dbt/config/runtime.py: cli_vars: Dict[str, Any] = parse_cli_vars(getattr(args, "vars", "{}"))
core/dbt/config/runtime.py: "vars": self.vars.to_dict(),
core/dbt/config/runtime.py: cli_vars: Dict[str, Any] = parse_cli_vars(getattr(args, "vars", "{}"))
core/dbt/parser/manifest.py: getattr(config.args, "vars", "{}") or "{}",
core/dbt/task/debug.py: self.cli_vars = parse_cli_vars(getattr(self.args, "vars", "{}"))
core/dbt/utils.py: if key == "vars" and var_args[key] == "{}": $ git grep "to_project_config" core
core/dbt/config/profile.py: """Unlike to_project_config, this dict is not a mirror of any existing
core/dbt/config/project.py: cfg = self.to_project_config(with_packages=True)
core/dbt/config/project.py: return self.to_project_config(with_packages=True) == other.to_project_config(
core/dbt/config/project.py: def to_project_config(self, with_packages=False):
core/dbt/config/project.py: ProjectContract.validate(self.to_project_config())
core/dbt/config/runtime.py: result = self.to_project_config(with_packages=True)
core/dbt/config/runtime.py: def to_project_config(self, with_packages=False):
core/dbt/config/runtime.py: Overrides dbt.config.Project.to_project_config to omit undefined profile
core/dbt/config/utils.py: return project.to_project_config() if return_dict else project |
I didn't catch this on the first read-through, but what's actually going on here: the entries in the When Basically, this is the same as the request made in #3105. I think it could be resolved by adding Jinja rendering to the In the meantime, I am going to close this issue as a duplicate. |
Is this your first time submitting a feature request?
Describe the feature
If a Jinja expression is used within a var, reading it should return the evaluated type and value.
For example,
Doing
var("is_prod")
in the Jinja context will evaluate to"True"
, notTrue
.However, I noticed that for the
enabled
configuration, this is not the case, meaning that I can do:As part of our package, we introduced feature flags that allow the users to disable certain parts of the functionality.
Users would often enable them only for prod like so:
However, we later found out that this evaluates to
"True"
and"False"
and not the respective boolean types and therefore doing{% if var("disable_something") %}
would always be true because it's a non-empty string.Describe alternatives you've considered
At the moment, we've added code that reads the var and if it's a string of
"True"
or"False"
, convert it to booleans.Who will this benefit?
No response
Are you interested in contributing this feature?
Yes.
Anything else?
No response
The text was updated successfully, but these errors were encountered: