Repository navigation
Generate PEP 621 authors in the app pyproject.toml - #282
garvitshrma wants to merge 3 commits into
Conversation
a0652de to
4ec2705
Compare
freakboy3742
left a comment
There was a problem hiding this comment.
A couple of tweaks needed to this template - I suspect it's passing CI because it's running against the mainline briefcase, so it's defaulting to using "author = 'Jane Developer'" in the template context, and that's not showing up because that's the default here as well.
| license-files = ["LICENSE"] | ||
| author = "{{ cookiecutter.author|escape_toml }}" | ||
| author_email = "{{ cookiecutter.author_email }}" | ||
| {{ cookiecutter.authors }} |
There was a problem hiding this comment.
I don't think this will work - it will output:
[{"name": "Alice", "email": "alice@example.com"},{"name": "Bob", "email": "bob@example.com"}]
Which (a) isn't pleasant to read, and (b) isn't valid toml.
There was a problem hiding this comment.
authors isn't a list - it's a pre-rendered TOML block passed as a string, built by
authors_toml() in beeware/briefcase#3131 (src/briefcase/commands/new.py) at line 44 to 50. Two authors
produce:
authors = [
{name = "Alice", email = "alice@example.com"},
{name = "Bob", email = "bob@example.com"},
]
It has to be a string because cookiecutter 2.7.1 can't carry a list of dicts through its
context - a list is treated as a choice variable, so "authors": [{...}] overridden with a
list raises TypeError: unhashable type: 'dict'.
| "description": "An app that does lots of stuff", | ||
| "author": "Jane Developer", | ||
| "author_email": "jane@example.com", | ||
| "copyright_holder": "Jane Developer", |
There was a problem hiding this comment.
Where is this value coming from? Why not use "name of first author", or "comma separated name of all authors"?
There was a problem hiding this comment.
It’s the default used when the template is rendered without Briefcase. Same role as "Project Awesome" on the line above.
When Briefcase renders the template it passes its own value, which wins over this default. That value comes from copyright_holder() in briefcase/commands/new.py:
return ", ".join(author["name"] for author in authors if author.get("name"))
it's all authors comma separated.
freakboy3742
left a comment
There was a problem hiding this comment.
Looks like I reviewed this without noticing the corresponding Briefcase changes - apologies for the crossed wires.
The approach you've taken is a creative fix; but it's not really the right fix. We shouldn't be passing pre-rendered output into a template - a template should accept data, and then use that data to generate output.
I think the issue that has tripped you up here is that you can't use a "list of dicts" as the value in a Cookiecutter template, and the natural form for authors as a cookiecutter key is a list of dicts. I'm guessing you've hit that limitation, and sought a workaround by pre-rendering the two values that need a list-of-dicts.
However, you can use a dictionary as a cookiecutter value. That's the better fix here - and it also gives us an entry to future PEP 621 work.
If we add a pep621 key to cookiecutter.json, we can accept authors in the "natural" form - and also provide a hook for any future PEP621 keys:
"pep621": {
"authors": [
{"name": "Jane Developer", "email": "jane@example.com"}
]
},
You can then access cookiecutter.pep621.authors in the template.
Now - rendering the copyright holder and the TOML inline table form of an author might be slightly complicated to do in a Jinja template - but at that point, we can add a cookiecutter filter to help with the rendering process. The escape_toml that you moved in the Briefcase PR is an example of that - it allows you to say {{ value|escape_toml }} in a template, and know that the value will be a valid TOML string. We could add a toml_inline_table and lexical_join filters - the former turning a dictionary into a one line TOML inline table; and lexical join doing the "join all the parts of this list with a comma, except for the last one which is joined with an 'and'".
I hope that explanation makes sense; let me know if you need any more details.
|
Made changes to accept the data instead of pre rendered output. |
The generated
pyproject.tomlnow uses a PEP 621authorslist instead of the deprecatedauthorandauthor_emailsettings.Companion to beeware/briefcase#3131. Refs #1235.
I have now named
authorascopyright_holder, which completely removesauthorPR Checklist:
Assisted-by: Claude Opus 5