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

Changed behaviour between 2.1.1 and 2.2 in dictionary variables #1954

Open
sandroden opened this issue Oct 3, 2023 · 3 comments
Open

Changed behaviour between 2.1.1 and 2.2 in dictionary variables #1954

sandroden opened this issue Oct 3, 2023 · 3 comments

Comments

@sandroden
Copy link

I have a template that has this cookiecutter.json:

 "pg_ports": {"port_list": [] },

I'm using cookiecutter from python scripts and send values as extra_context as:

extra_context = {
    ....
    'pg_ports':` {'port_list': ['5452:5432']},
}

It worked as expected in 2.1.1 and stopped working in 2.2+
the error I get is:

Traceback (most recent call last):
  File "/home/local/scripts/jmb", line 11, in <module>
    load_entry_point('jmb.tools', 'console_scripts', 'jmb')()
  File "/usr/lib/python3/dist-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/misc/src/hg/thunder/jmb2/jmb-tools3/jmb/tools/conf/docker.py", line 120, in compose_from_yaml
    cookiecutter(url, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/cookiecutter/main.py", line 128, in cookiecutter
    extra_context=extra_context,
  File "/usr/local/lib/python3.7/dist-packages/cookiecutter/generate.py", line 130, in generate_context
    apply_overwrites_to_context(obj, extra_context)
  File "/usr/local/lib/python3.7/dist-packages/cookiecutter/generate.py", line 82, in apply_overwrites_to_context
    apply_overwrites_to_context(context_value, overwrite)
  File "/usr/local/lib/python3.7/dist-packages/cookiecutter/generate.py", line 64, in apply_overwrites_to_context
    f"{overwrite} provided for multi-choice variable {variable}, "
ValueError: ['5452:5432'] provided for multi-choice variable port_list, but valid choices are []

I'm working in Linux with python3.7, to 3.10

It seems to me that cookiecutter wrongly interprets the index in the value of the dictionary variable as a choice list, that is not.

If I understand correctly the docs "The dictionary values can, themselves, be other dictionaries and lists - the data structure can be as deep as you need.", it should not be interpreted as a list.

Please let me know if I'm wrong and in fact what I want to obtain should be done differently.

@sandroden sandroden changed the title disruptive behaviour between 2.1.1 and 2.2 in dictionary variables Changed behaviour between 2.1.1 and 2.2 in dictionary variables Oct 3, 2023
@sandroden
Copy link
Author

sandroden commented Oct 3, 2023

It seems that using "" instead of [] is accepted.

"pg_ports": {"port_list": "" },

I can't read this in the docs...
Which is the intended syntax?

@david-abn
Copy link
Contributor

It is returning False on this condition

if set(overwrite).issubset(set(context_value))

where overwrite is the value you provide ['5452:5432'], and context_value is inside cookiecutter.json, an empty list []

If you modify cookiecutter.json to include '5452:5432' as an element, it should work

This is new logic added 3 months ago in 6badb2e

@sandroden
Copy link
Author

I set the possible values in choice variable, but when using dictionary variables I'm free to use whatever I want "The dictionary values can, themselves, be other dictionaries and lists - the data structure can be as deep as you need."
Are you saying that the now you are forced to use choice values even in nested values of a dict variable?
If that's the desired behaviour it should be stated in a clearer way in my opinion.

My workaround was to use an empty string as default and it seems to work, at least when using it from python in a non-interactive mode.

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