You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using get_user_config(), the method fails with an exception instead of returning default values, if the .cookiecutterrc file exists, but is empty.
Stacktrace:
>>> get_user_config()
Traceback (most recent call last):
File "/local/home/a.pirogov/.cache/pypoetry/virtualenvs/fair-python-cookiecutter-Y_dY9N1_-py3.8/lib/python3.8/site-packages/cookiecutter/config.py", line 115, in get_user_config
env_config_file = os.environ['COOKIECUTTER_CONFIG']
File "/local/home/a.pirogov/.pyenv/versions/3.8.17/lib/python3.8/os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: 'COOKIECUTTER_CONFIG'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/local/home/a.pirogov/.cache/pypoetry/virtualenvs/fair-python-cookiecutter-Y_dY9N1_-py3.8/lib/python3.8/site-packages/cookiecutter/config.py", line 121, in get_user_config
return get_config(USER_CONFIG_PATH)
File "/local/home/a.pirogov/.cache/pypoetry/virtualenvs/fair-python-cookiecutter-Y_dY9N1_-py3.8/lib/python3.8/site-packages/cookiecutter/config.py", line 69, in get_config
config_dict = merge_configs(DEFAULT_CONFIG, yaml_dict)
File "/local/home/a.pirogov/.cache/pypoetry/virtualenvs/fair-python-cookiecutter-Y_dY9N1_-py3.8/lib/python3.8/site-packages/cookiecutter/config.py", line 44, in merge_configs
for k, v in overwrite.items():
AttributeError: 'NoneType' object has no attribute 'items'
What I've run:
Just have an empty .cookiecutterrc and run get_user_config().
Expected behavior:
This exception should not happen, a missing .cookiecutterrc should be treated the same as an empty one.
The text was updated successfully, but these errors were encountered:
I've been able to reproduce this error, and have observed the following:
pyyaml.safe_load(stream) returns None if the YAML stream is empty [1]; an empty YAML stream is a stream that contains no YAML documents [2], and an empty file contains no YAML documents
the implementation of cookiecutter.config.get_config(config_path) does not check if None is returned, and instead, passes the result along to cookiecutter.config.merge_configs(default, overwrite) without checking
in addition, the current implementation assumes that the top level of the YAML config file is an object (which would be transformed to a dict), but it could also be an array (which would be transformed to a list), which would also result in an Exception
pyyaml works as described in (1), and the mistake has been made in the cookiecutter package. To address (2) and (3), I would like to propose to add the following checks to cookiecutter.config.get_config(config_path):
if the result returned by pyyaml.safe_load(stream) is None, then the result is replaced by an empty dictionary
if the result returned by pyyaml.safe_load(stream) is not a dict (or None), then raise an InvalidConfiguration exception to inform the user that something is wrong with their configuration file, even though it's a valid YAML file
I'll make a pull request for a fix and the necessary tests.
Description:
When using
get_user_config()
, the method fails with an exception instead of returning default values, if the.cookiecutterrc
file exists, but is empty.Stacktrace:
What I've run:
Just have an empty
.cookiecutterrc
and runget_user_config()
.Expected behavior:
This exception should not happen, a missing
.cookiecutterrc
should be treated the same as an empty one.The text was updated successfully, but these errors were encountered: