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

It renders list variable only as first item of it #1975

Open
dinjazelena opened this issue Nov 8, 2023 · 3 comments
Open

It renders list variable only as first item of it #1975

dinjazelena opened this issue Nov 8, 2023 · 3 comments

Comments

@dinjazelena
Copy link

  • Cookiecutter version: 2.4.0
  • Template project url:
  • Python version: 3.10
  • Operating System: ubuntu

Description:

When i have something like this:

animal:
  fly: [bird]
  walk: [cat]
food: [meat, worms]

I try to put this in cookiecutter template generation:

    """Generate usecase project structure from cookiecutter template."""
    rich_print("Generating project from cookiecutter template.")
    cookiecutter(
        template=str(ROOT_PATH / "template"),
        no_input=True,
        extra_context={
            "animal": config["animal"],
            "food": config["food"],
 
        },
    )

Then in post_gen_project.py i try to tweak a bit:

ANIMAL: str = "{{ cookiecutter.animal }}"
FOOD: str = "{{ cookiecutter.food }}"

print(ANIMAL)  # -> {"fly": ["bird"], "walk": ["cat"]}
print(FOOD) # -> meat

Its crazy, i have never seen such a thing, like there is no place in between that i reference these variables, and when variable is type of list it only takes first time with cookiecutter, checked everystep.. No idea what can i do more

@noklam
Copy link

noklam commented Nov 10, 2023

I literally run into the same problem just now and I am so glad there is an open issue. After digging, I found that
at this point the context is wrong.

with import_patch:
result = generate_files(
repo_dir=repo_dir,
context=context,

I am still trying to understand the source code, there is context["cookiecutter"] which stores the first element of a list while there is an internal one context["_cookietcutter"] that keep the original list.

It's most likely this function is buggy.

def prompt_for_config(context, no_input=False):
"""Prompt user to enter a new config.
:param dict context: Source for field names and sample values.
:param no_input: Do not prompt for user input and use only values from context.
"""
cookiecutter_dict = OrderedDict([])
env = StrictEnvironment(context=context)

Which has this logic of treating list as a choice.

if isinstance(raw, list):
# We are dealing with a choice variable
val = prompt_choice_for_config(
cookiecutter_dict, env, key, raw, no_input, prompts, prefix
)

@noklam
Copy link

noklam commented Nov 10, 2023

As a workaround, use {{ _cookiecutter.<var> }} instead of {{ cookiecutter.<var> }}. This preserve the original list, right now I think cookiecutter assume a list is a choice so it will only return one of the choices.

@dinjazelena
Copy link
Author

Thanks!

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

2 participants