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

fix: ignore empty YAML documents in copier.yml #1538

Merged
merged 1 commit into from
Feb 28, 2024

Conversation

sisp
Copy link
Member

@sisp sisp commented Feb 28, 2024

I've fixed loading copier.yml with empty YAML documents. The fix is simple: yaml.load_all() returns a sequence of YAML documents, and empty documents are represented by None, so I'm filtering None items using filter(None, yaml.load_all(...)). filter(None, <sequence>) actually filters any falsy items – not only Nones –, but that's no problem in this case.

Before, Copier raised an error

TypeError: 'NoneType' object is not subscriptable

which is not very helpful for debugging. Now, an empty YAML document is simply ignored. Of course, there's little sense in having empty YAML documents, but it's easy to have one unintentionally, e.g. when the file ends with the --- marker:

question:
  type: str
---
!include common.yml
---

In this regard, the YAML document syntax is a bit confusing IMO because a leading --- marker at the beginning of the file does not cause an empty YAML document. I think Copier should not be strict about it and ease template developers' lives. See also the confusion about this in #1496.

Copy link

codecov bot commented Feb 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.30%. Comparing base (2f72105) to head (3f0675f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1538      +/-   ##
==========================================
+ Coverage   97.28%   97.30%   +0.02%     
==========================================
  Files          48       48              
  Lines        4564     4568       +4     
==========================================
+ Hits         4440     4445       +5     
+ Misses        124      123       -1     
Flag Coverage Δ
unittests 97.30% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@pawamoy pawamoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again simple fix, makes lives easier, LGTM 🙂

@sisp
Copy link
Member Author

sisp commented Feb 28, 2024

Thanks for the quick review, @pawamoy! 🙏

@sisp sisp merged commit 14865bf into copier-org:master Feb 28, 2024
21 checks passed
@sisp sisp deleted the fix/ignore-empty-yaml-documents branch February 28, 2024 12:06
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

Successfully merging this pull request may close these issues.

2 participants