-
Notifications
You must be signed in to change notification settings - Fork 122
[Python] Fix issues with multiple mutators #2509
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import sys | ||
| from dataclasses import replace | ||
| from io import StringIO | ||
| from pathlib import Path | ||
|
|
||
| from databricks.bundles.build import ( | ||
| _append_resources, | ||
| _apply_mutators, | ||
| _Args, | ||
| _Conf, | ||
| _load_object, | ||
|
|
@@ -21,6 +23,7 @@ | |
| Location, | ||
| Resources, | ||
| Severity, | ||
| job_mutator, | ||
| ) | ||
| from databricks.bundles.jobs import Job | ||
|
|
||
|
|
@@ -299,6 +302,69 @@ def test_conf_from_dict(): | |
| ) | ||
|
|
||
|
|
||
| def test_mutators(): | ||
| bundle = Bundle(target="default") | ||
| resources = Resources() | ||
| resources.add_job("job_0", Job(tags={"tag": "value"})) | ||
|
|
||
| @job_mutator | ||
| def add_first_tag(bundle: Bundle, job: Job) -> Job: | ||
| tags = bundle.resolve_variable(job.tags) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why call
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because of types, job.tags can potentially refer to a variable, while it isn't possible in practice. |
||
|
|
||
| return replace(job, tags={"first": "tag", **tags}) | ||
|
|
||
| @job_mutator | ||
| def add_second_tag(bundle: Bundle, job: Job) -> Job: | ||
| tags = bundle.resolve_variable(job.tags) | ||
|
|
||
| return replace(job, tags={"second": "tag", **tags}) | ||
|
|
||
| new_resources, diagnostics = _apply_mutators( | ||
| bundle=bundle, | ||
| resources=resources, | ||
| mutator_functions=[add_first_tag, add_second_tag], | ||
| ) | ||
|
|
||
| # add_second_tag is the last mutator that has modified a job | ||
| expected_location = Location.from_callable(add_second_tag.function) | ||
|
|
||
| assert not diagnostics.has_error() | ||
| assert new_resources._locations[("resources", "jobs", "job_0")] == expected_location | ||
| assert new_resources.jobs["job_0"].tags == { | ||
| "first": "tag", | ||
| "second": "tag", | ||
| "tag": "value", | ||
| } | ||
|
|
||
|
|
||
| def test_mutators_unmodified(): | ||
| bundle = Bundle(target="default") | ||
|
|
||
| resources = Resources() | ||
| resources.add_job("job_0", Job(description="description")) | ||
|
|
||
| @job_mutator | ||
| def mutator_1(job: Job) -> Job: | ||
| return replace(job, description="updated description") | ||
|
|
||
| @job_mutator | ||
| def mutator_2(job: Job) -> Job: | ||
| return job | ||
|
|
||
| new_resources, diagnostics = _apply_mutators( | ||
| bundle=bundle, | ||
| resources=resources, | ||
| mutator_functions=[mutator_1, mutator_2], | ||
| ) | ||
|
|
||
| # despite mutator_2 being called last, it doesn't change the job, and we should use location of mutator_1 | ||
| expected_location = Location.from_callable(mutator_1.function) | ||
|
|
||
| assert not diagnostics.has_error() | ||
| assert new_resources._locations[("resources", "jobs", "job_0")] == expected_location | ||
| assert new_resources.jobs["job_0"].description == "updated description" | ||
|
|
||
|
|
||
| def test_load_resources(): | ||
| bundle = Bundle(target="default") | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Per the comment above, it is not possible to detect in-place mutations, but this doesn't do anything to prevent them. Is that correct?
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.
Right. I will add tests for it. If the object is mutated but not copied, a mutation will have an effect, but we aren't going to record LOC changes. We can potentially detect mutation by comparing it to deep copy or hashing objects, but it will introduce an additional overhead.