Repository: PennyLaneAI/pennylane
Pull request #4798 (2023-11-07 18:41:51)

add a setter to avoid breaking Observable.return_type
**Context:**
Some plugin tests failed because they were setting this property, which I did not expect.

**Description of the Change:**
Add a setter to the `Observable.return_type` property to avoid breaking things in this release. Also warn if someone uses it.

**Benefits:**
We don't break `Observable.return_type` in this release.

**Possible Drawbacks:**
People can still set it? idk seems like a safe call for now. I'm not validating the type provided

**Related GitHub Issues:**
missed in #4762 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4798?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`de107aa`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/de107aacd31432641f987c59da9dd4038c685881?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.64% compared to head [(`4365f1c`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4798?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.63%.
> Report is 1 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #4798      +/-   ##
==========================================
- Coverage   99.64%   99.63%   -0.01%     
==========================================
  Files         381      381              
  Lines       34320    34065     -255     
==========================================
- Hits        34198    33942     -256     
- Misses        122      123       +1     
```


| [Files](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4798?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Coverage Δ | |
|---|---|---|
| [pennylane/operation.py](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4798?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL29wZXJhdGlvbi5weQ==) | `97.26% <100.00%> (ø)` | |

... and [41 files with indirect coverage changes](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4798/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4798?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 200c17cbb..8b2410381 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -56,6 +56,7 @@
 * `Observable.return_type` is deprecated. Instead, you should inspect the type
   of the surrounding measurement process.
   [(#4762)](https://github.com/PennyLaneAI/pennylane/pull/4762)
+  [(#4798)](https://github.com/PennyLaneAI/pennylane/pull/4798)
 
 <h3>Documentation 📝</h3>
 
diff --git a/pennylane/operation.py b/pennylane/operation.py
index 6cc951f5b..846f15310 100644
--- a/pennylane/operation.py
+++ b/pennylane/operation.py
@@ -1887,6 +1887,16 @@ class Observable(Operator):
         )
         return self._return_type
 
+    @return_type.setter
+    def return_type(self, value):
+        """Change the return type of an Observable. Note that this property is deprecated."""
+        warnings.warn(
+            "`Observable.return_type` is deprecated. Instead, you should "
+            "create a measurement process containing this Observable.",
+            UserWarning,
+        )
+        self._return_type = value
+
     def __matmul__(self, other):
         if active_new_opmath():
             return super().__matmul__(other=other)


Test patch:
diff --git a/tests/test_operation.py b/tests/test_operation.py
index bc352406c..383e9c47a 100644
--- a/tests/test_operation.py
+++ b/tests/test_operation.py
@@ -886,6 +886,11 @@ class TestObservableConstruction:
         with pytest.warns(UserWarning, match="`Observable.return_type` is deprecated. Instead"):
             assert DummyObserv(0, wires=[1]).return_type is None
 
+        obs = DummyObserv(0, wires=[1])
+        with pytest.warns(UserWarning, match="`Observable.return_type` is deprecated. Instead"):
+            # pylint:disable=attribute-defined-outside-init
+            obs.return_type = qml.measurements.Sample
+
     def test_construction_with_wires_pos_arg(self):
         """Test that the wires can be given as a positional argument"""