-
-
Notifications
You must be signed in to change notification settings - Fork 175
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
feat: Add _copier_conf.operation
variable
#1733
base: master
Are you sure you want to change the base?
Conversation
_copier_conf.operation
variable_copier_conf.operation
variable
2a53803
to
eea53da
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs would need to reflect this change too.
@@ -234,7 +238,7 @@ def _cleanup(self) -> None: | |||
for method in self._cleanup_hooks: | |||
method() | |||
|
|||
def _check_unsafe(self, mode: Literal["copy", "update"]) -> None: | |||
def _check_unsafe(self, mode: Operation) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to pass ˋmodeˋ if it is in ˋself.operationˋ, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I did it like that, but noticed that this would cause a behavior change (at least in theory):
During _apply_update()
, self.operation
is update
, but it calls on run_copy()
several times, which would pass copy
to _check_unsafe()
before this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. However, that is correct. You will notice that there are calls to replace
. In those calls, you can replace some configuration for the sub-worker that is created. Could you please try doing it that way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not sure I'm following. First, let me sum up:
- Introducing
_copier_conf.operation
means we have an attribute on the worker representing the current high-level (user-requested) operation. - You're proposing to use this reference for
_check_unsafe
instead of the parameter. - I noted that doing this will change how
_check_unsafe
behaves during the individual copy operations that run during an update, where the high-level operation isupdate
, but the low-level one iscopy
, advocating for keeping the parameter.
I'm already using replace
for overriding the operation during update
. Are you saying the high-level operation during the individual copy operations should be copy
? Because that would mean _copier_conf.operation
is always copy
during template rendering, i.e. defeat the purpose of this feature.
031e199
to
54fc0b7
Compare
4ba043b
to
e0cac30
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1733 +/- ##
==========================================
+ Coverage 97.61% 97.75% +0.13%
==========================================
Files 49 50 +1
Lines 5042 5084 +42
==========================================
+ Hits 4922 4970 +48
+ Misses 120 114 -6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@yajo Is there anything I still need to do here? Just making sure you noticed the changes. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for taking so long to review! I don't have a lot of time and this one required deep thinking.
@@ -68,6 +68,9 @@ | |||
_T = TypeVar("_T") | |||
|
|||
|
|||
Operation = Literal["copy", "update"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this to copier.types
module
@@ -234,7 +238,7 @@ def _cleanup(self) -> None: | |||
for method in self._cleanup_hooks: | |||
method() | |||
|
|||
def _check_unsafe(self, mode: Literal["copy", "update"]) -> None: | |||
def _check_unsafe(self, mode: Operation) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. However, that is correct. You will notice that there are calls to replace
. In those calls, you can replace some configuration for the sub-worker that is created. Could you please try doing it that way?
@@ -203,6 +206,7 @@ class Worker: | |||
unsafe: bool = False | |||
skip_answered: bool = False | |||
skip_tasks: bool = False | |||
operation: Operation = "copy" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: I find this a bit counter-intuitive. We have to pass an operation now to the worker. The operation could be something like "update" for example. But then we can run_copy()
or run_update()
.
It seems like duplicated features and inconsistent API behavior. If this is the case now, I think we should have a unified run_operation()
method that runs either one or the other behind the scenes.
When the workers are created, they should be with the correct operation, and from there onwards use that unless replace()
changes it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to pass an operation now to the worker.
We mustn't pass it to the worker though, it's an internal reference that has to be copy
by default, only overridden by calls to replace
in run_update
.
I wanted to declare this field as init=False
for that reason, but that does not work together with replace
by default (see the last paragraph on dataclasses.replace). This can be fixed by using a custom replace
function though.
git_save() | ||
getattr(copier, f"run_{operation}")(str(dst), overwrite=True) | ||
expected = "copy" if operation == "recopy" else operation | ||
assert ctx_file.read_text() == expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: So, let's say that we copy one template and render the value. The file will contain copy
.
Now we run it again. The file contains update
. The change is OK because the replay renders copy
and detects the change.
However, what would happen if we run it yet again? The replay will render copy
, but in reality it is update
. Now the next one is also update
.
Triple-thinking this... are we introducing here a way to make replays non-reproducible and thus maybe making the update algorithm unreliable? Because there's no way Copier could know if last play was a copy or an update.
CC @copier-org/maintainers. Should we just reject this feature to avoid shooting our own feet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregarding that _copier_conf.operation
is not intended to be used for template content changes, this test for checking the value of operation
only works because the file containing {{ _copier_conf.operation }}
- is deleted
- is marked as
skip_if_exists
, which means it will be regenerated duringupdate
if deleted.
What would happen without it being deleted is the following:
copier copy
renders the template, it containscopy
copier recopy
overwrites the template, it containscopy
copier update
updates the template, it still containscopy
. This is valid for each subsequent update.
Why (3):
During _apply_update
, old_copy
and new_copy
are both rendered with _copier_conf.operation == "update"
. The file contains copy
though, which is detected as a custom change and reapplied.
_copier_conf.operation
should be documented as a meta variable, intended for templating copier.yml
configuration. It cannot be used to influence template contents (reliably).
Adds
operation
to_copier_conf
, representing the current operation - eithercopy
,recopy
orupdate
. This was proposed here: #1718 (comment)I hope the way it's implemented and tested is as intended by @yajo.
The tests are quite synthetic,I would expect the usual application to be_copier_conf.operation (!/=)= "update"
.Fixes: #1725
Alternative to #1732