-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Render variables starting with an underscore. #1339
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
Render variables starting with an underscore. #1339
Conversation
I like this very much, as I ran into that very problem as a relatively new user. Perhaps you should add a test and documentation for that as well. Btw. have you checked how that interacts with the replay function / repo updater packages? I'd very much like to see this PR merged soon and making it easy on the developers is a good way. |
All reactions
Sorry, something went wrong.
@con-f-use I'm glad you like it! You're right, I should add a test and some docs, though as far as I could see the underscore variables aren't explicitely tested for or documented (I didn't spend too long looking so may have missed it).
I haven't, but all the tests pass so I assume this is fine. I'm not sure this will be merged given #1256 Perhaps this could go in the 2.0 release @insspb? |
All reactions
Sorry, something went wrong.
Hi @smoothml
def test_should_render_dict(self):
"""
"""
context = {
'cookiecutter': {
'project_name': 'Slartibartfast',
'_project_name': 1123,
'details': {'other_name': '{{cookiecutter.project_name}}'},
}
}
cookiecutter_dict = prompt.prompt_for_config(context, no_input=True)
assert cookiecutter_dict == {
'project_name': 'Slartibartfast',
'_project_name': 1123,
'details': {'other_name': u'Slartibartfast',},
} This code will be good with current implementation. Will fail with your proposal. So... Let do double underscore for user rendered private variables + docs + test. And it can be included in nearest release after this will be done. But I really like this idea. At least things like this will be shorter: https://github.com/ionelmc/cookiecutter-pylibrary/blob/master/cookiecutter.json Currently included in 1.7.1, but as we are plan to make a release in few days maybe it will not be ready for that time. At this case i will move it to 2.0. |
All reactions
-
🎉 1 reaction
Sorry, something went wrong.
Thanks for your comments @insspb. Good point regarding numbers vs. strings. Double underscores work 👍 I'm not sure I'll be able to get this ready in the next few days so getting it in 2.0 is a good aim I think. |
All reactions
-
🚀 1 reaction
Sorry, something went wrong.
I like this change but please add some tests for it and we should merge it in next major release 1.8.0, not the patch one (1.7.x) due to the risk of breaking existing templates. The risk of breaking templates is small and template authors should already have CI/CD configured to test their templates with cookiecutter from master, running jobs at least weekly. We will always wait at least one week before merging a breaking change and tagging a release or pre-release. |
All reactions
-
👍 1 reaction
Sorry, something went wrong.
tests/test_prompt.py
Outdated
@@ -260,6 +260,29 @@ def test_dont_prompt_for_private_context_var(self, monkeypatch): | |||
cookiecutter_dict = prompt.prompt_for_config(context) | |||
assert cookiecutter_dict == {'_copy_without_render': ['*.html']} | |||
|
|||
def test_render_hidden_variables(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstrings missed.
Sorry, something went wrong.
All reactions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, thanks. I haven't finished yet!
Sorry, something went wrong.
All reactions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have made it a draft PR actually. I'll do that.
Sorry, something went wrong.
All reactions
@smoothml still draft? |
All reactions
Sorry, something went wrong.
Yeah, sorry. Been slammed the last couple of weeks. Just docs to go. Will try and get it done this weekend. Sorry for the delay! |
All reactions
Sorry, something went wrong.
Sorry, something went wrong.
Successfully merging this pull request may close these issues.
Currently, variables starting with an underscore (which are not shown to the user on project creation) are not rendered with Jinja2. If you want to create one of these variables based on other variables you cannot. For example, if you are creating a Python package repository and want to base the package name on the project name you might have a configuration like this
Which could create a structure like this
However, the user can change
project_slug
andpackage_slug
. This may be desirable, but (as in my current situation) it may not be. You could create post-generation hooks to fail project creation if these are changed, but this seems inelegant. This PR allows for the configuration to be specified like thisThe user then cannot change these variables, enforcing consistent naming.