Skip to content

Add inheritance and composition section in user docs #2165

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

francesco086
Copy link
Contributor

Contributes to #934

Copy link

codecov bot commented May 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.03%. Comparing base (aad1f7d) to head (569a267).
Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2165   +/-   ##
=======================================
  Coverage   98.03%   98.03%           
=======================================
  Files          55       55           
  Lines        6005     6005           
=======================================
  Hits         5887     5887           
  Misses        118      118           
Flag Coverage Δ
unittests 98.03% <ø> (ø)

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@francesco086 francesco086 force-pushed the doc/inheritance-and-composition branch from ffef11b to 63b12a5 Compare May 23, 2025 13:59
Co-authored-by: fpaterour-ippon <82809424+fpaterour-ippon@users.noreply.github.com>
Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

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

Thanks for volunteering to write documentation on this complex topic, @francesco086! 🙏 I have a few suggestions, curious what you think.

Regarding solutions: Meta-templates is another one, although I don't know how relevant it actually is in practice.

Copy link
Member

Choose a reason for hiding this comment

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

How about renaming the file to reusing.md (intentionally as a gerund for consistency with some other files like configuring.md) to keep the URL path concise and also less specific to a particular reuse approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My feeling is that reusing is too generic, and it is especially confusing in the context of copier. The first thing that would come to my mind is "Of course! I have some code that I want to re-use by creating a template! Let's see how".

What about composing, composing_templates, or inheriting_and_composing_templates (too long perhaps)? Alternatively, reusing_templates may be explicit enough.

I leave the final decision to you / the maintainers, just let me know what do you think is best and I will apply 😄

@@ -0,0 +1,36 @@
# Template Inheritance and Composition
Copy link
Member

Choose a reason for hiding this comment

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

For consistency with the renamed file:

Suggested change
# Template Inheritance and Composition
# Template Reuse

@@ -0,0 +1,36 @@
# Template Inheritance and Composition

## What is Inheritance, what is Composition?
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this is more concise?

Suggested change
## What is Inheritance, what is Composition?
## Inheritance vs. Composition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, but it miss the key point that this section is "What are we talking about", and not only comparing them.

In my head this section was the WHAT. Then there is the WHY, and finally HOW (solutions).


Template Inheritance is a way to create a new template based on an existing one. It
allows you to reuse code and structure from the parent template while adding or
overriding specific parts in the child template. This is useful for creating variations
Copy link
Member

Choose a reason for hiding this comment

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

I think it's mostly about extensions, not so much variations – perhaps a matter of definition. Variations – in my mind – can often be addressed by conditional questions and conditional rendering in the same template. WDYT?

Suggested change
overriding specific parts in the child template. This is useful for creating variations
overriding specific parts in the child template. This is useful for creating extensions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with what is in your head, but this is exactly the reason why I think inheritance is useful, to avoid the ugly if/else.

By the way this is exactly what I did in my example, where we did not agree that it was inheritance.
I would like to mention that if you look carefully at the example you may notice that I did start with if/else (this is the key question), but it became a horror to maintain the template. Very complex, very hard to read, and very slow tests. This is why on the next flavor I had to add, I decided for this "inheritance" approach, which worked decently, except for the drawbacks you already clarified very well, especially when it comes to copier update.

But you made a good point, in my opinion we should mention both. An alternative term for "variation" is "flavor" by the way.

Do you think we can agree to write "This is useful for creating extensions and variations"?

Comment on lines 12 to 14
Template Composition, on the other hand, is a way to combine multiple templates,
independent of each other, into a single new template. In this way you can join the
functionalities of multiple templates, by adding some "glue code", and avoid repetition.
Copy link
Member

Choose a reason for hiding this comment

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

Do you have an approach like yours using tasks that run copier copy in mind? I've mostly thought of "composition" along the lines of https://copier.readthedocs.io/en/stable/configuring/#applying-multiple-templates-to-the-same-subproject. There, template composition means generating output from multiple templates in the same target directory, but there is no new template that is a composition of other templates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is up to discussion!

The solution you mentioned is what I consider the "manual" solution. But of course leaves the problem of "what if it is very common to compose always the same 3 templates" and want to make it more convenient for a user to get the 3 together and don't need to manually check that they have compatible versions?

But yes, I think that using tasks could be a way, of course with the drawbacks already mentioned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To avoid forgetting about it, I also added this to the solutions


## Solutions

### Git submodules + jinja template inheritance
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
### Git submodules + jinja template inheritance
### Git submodules + Jinja template inheritance

🤓

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🥷

@@ -12,6 +12,7 @@ nav:
- Generating a project: "generating.md"
- Updating a project: "updating.md"
- Settings: "settings.md"
- Template Inheritance and Compostion: "inheritance_and_composition.md"
Copy link
Member

Choose a reason for hiding this comment

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

See above:

Suggested change
- Template Inheritance and Compostion: "inheritance_and_composition.md"
- Reusing templates: "reusing.md"

Comment on lines 20 to 22
## Why copier does not natively support inheritance and composition?

<!-- TODO: add content, re-use content from https://github.com/copier-org/copier/issues/934#issuecomment-1518964035 -->
Copy link
Member

Choose a reason for hiding this comment

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

I anticipate that a proper solution might require Copier to support it natively. At least for the fork-like workflow, I think that specified template versions in parent template migrations need to be mapped to child template versions – at the very least. But it's too early to make a definitive statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see that coming too! But I think @yajo had some very valid points. So, let's see where do we end up to, and let's try to keep what he said in mind.

Copy link
Contributor Author

@francesco086 francesco086 May 28, 2025

Choose a reason for hiding this comment

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

Reading carefully what he wrote once again, it seems to me that everything reduces to the UNIX philosophy of "do one thing well", or am I wrong?

I filled the WHY section

@francesco086
Copy link
Contributor Author

Regarding solutions: Meta-templates is another one, although I don't know how relevant it actually is in practice.

Right! I think it is not really a solution for inheritance and composition tbh, but I added it to the list, and we will discuss it at due time

francesco086 and others added 6 commits May 28, 2025 19:09
Co-authored-by: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com>
Co-authored-by: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com>
Co-authored-by: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com>
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.

3 participants