Skip to content
New issue

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

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

Already on GitHub? # to your account

Support using multi-line when in tasks #2012

Open
lebrice opened this issue Mar 5, 2025 · 1 comment
Open

Support using multi-line when in tasks #2012

lebrice opened this issue Mar 5, 2025 · 1 comment

Comments

@lebrice
Copy link

lebrice commented Mar 5, 2025

Actual Situation

Hello there. I might be mistaken, so please do correct me if I'm wrong, but it seems like the when clause in tasks is checked by rendering the templated string, and then casting it to bool using copier.tools.cast_to_bool.

This boolean casting seems to treat a string containing only spaces or newlines as True, which prevents multi-line when clauses.

I tried using multi-line Jinja like this, to no avail :(

Desired Situation

I'd like to be able to use multi-line when conditions, for example:

examples_to_include:
  type: str
  multiselect: true
  choices:
    Image Classification (PyTorch): "image_classifier"
    Image Classification (Jax): "jax_image_classifier"
    Proximal Policy Optimization (Jax): "jax_ppo"
    LLM Fine-tuning (PyTorch, 🤗): "llm_finetuning"
    Text classification (PyTorch, 🤗): "text_classifier"
  default:
    [
      "image_classifier",
      "jax_image_classifier",
      "jax_ppo",
      "llm_finetuning",
      "text_classifier",
    ]
_tasks:
  - command: uv remove transformers datasets
    when: |
      {% if (text_classifier' not in examples_to_include and
             'llm_finetuning' not in examples_to_include ) %}
        true
      {% endif %}

Proposed solution

Can we make cast_to_bool treat a string with only whitespace as False?
I tried to find tests for the cast_to_bool function, but couldn't find any that directly called it with different values to check the results.

@lkubb
Copy link
Contributor

lkubb commented Mar 5, 2025

While I agree the value should be stripped before evaluation, you can workaround this by relying on Jinja whitespace control.

_tasks:
  - command: uv remove transformers datasets
    when: |
      {%- if (text_classifier' not in examples_to_include and
             'llm_finetuning' not in examples_to_include ) -%}
        true
      {%- endif -%}

I think the proper way is to do it like this though:

_tasks:
  - command: uv remove transformers datasets
    when: >-
      {{ text_classifier' not in examples_to_include and
          'llm_finetuning' not in examples_to_include }}

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants