Skip to content

false negative SyntaxError failures on f-strings #69

@atloo1

Description

@atloo1

in a post_gen_project.py hook I do

import subprocess

LICENSE_IDENTIFIER_TO_FILENAME = {
    'Apache-2.0': 'apache_2.txt',
    'BSD-3-Clause': 'bsd_3_clause.txt',
    'MIT': 'mit.txt',
    'OSL-3.0': 'osl_3.txt',
    'Proprietary': 'proprietary.txt',
}
MV_CHOSEN_LICENSE_COMMAND = [
    'mv',
    f"licenses/{LICENSE_IDENTIFIER_TO_FILENAME['{{cookiecutter.project_license}}']}",
    './LICENSE',
]
RM_OTHER_LICENSES_COMMAND = [
    'rm',
    '-rf',
    'licenses/',
]

def main():
    """use selected license"""
    subprocess.run(MV_CHOSEN_LICENSE_COMMAND)
    subprocess.run(RM_OTHER_LICENSES_COMMAND)

if __name__ == '__main__':
    main()
    sys.exit(0)

My preferred, still valid, f-string syntax w/o double quotes f'licenses/{LICENSE_IDENTIFIER_TO_FILENAME['{{cookiecutter.project_license}}']}' breaks tests:

    def test_cookiecutter(cookies):
        result = cookies.bake()
>       assert result.exit_code == 0
E       AssertionError: assert -1 == 0
E        +  where -1 = <Result FailedHookException('Hook script failed (exit status: 1)')>.exit_code

tests/test.py:16: AssertionError
--------------------------------------------------------------- Captured stdout call ---------------------------------------------------------------
<Result FailedHookException('Hook script failed (exit status: 1)')>
--------------------------------------------------------------- Captured stderr call ---------------------------------------------------------------
  File "/tmp/tmpk56yxsr6.py", line 55
    f'licenses/{LICENSE_IDENTIFIER_TO_FILENAME['Apache-2.0']}',
                                                ^
SyntaxError: f-string: unmatched '['
---------------------------------------------------------------- Captured log call -----------------------------------------------------------------
ERROR    cookiecutter.hooks:hooks.py:164 Stopping generation because post_gen_project hook script didn't exit successfully

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions