Repository: lbl-anp/becquerel

Issue:
iminuit parameter lookup does not error on bad parameter names
![image](https://user-images.githubusercontent.com/7683814/187555557-dfe49625-cb5f-4a06-b986-cd5338934ab9.png)

This feels like a bug in iminuit, not becquerel, but we could certainly check the parameter name first.


Discussion:

Patch:
diff --git a/becquerel/core/fitting.py b/becquerel/core/fitting.py
index c5c594f..2ffdaf4 100644
--- a/becquerel/core/fitting.py
+++ b/becquerel/core/fitting.py
@@ -734,6 +734,8 @@ class Fitter:
             p = comp.guess(self.y_roi, x=self.x_roi, dx=self.dx_roi)
             if isinstance(p, Parameters):
                 p = _parameters_to_bq_guess(p)
+            elif p is None:
+                raise TypeError()
             params += p
         return params
 
@@ -1053,12 +1055,14 @@ class Fitter:
         if "lmfit" in self.backend:
             if param in self.result.params:
                 return self.result.params[param].value
-            elif param in self.fit.best_values:
+            elif param in self.result.best_values:
                 return self.result.best_values[param]
             else:
                 raise FittingError(f"Unknown param: {param}")
         elif "minuit" in self.backend:
-            return self.result.params[param].value
+            if param in self.result.parameters:
+                return self.result.params[param].value
+            raise FittingError(f"Unknown param: {param}")
         raise FittingError(f"Unknown backend: {self.backend}")
 
     def param_unc(self, param):
@@ -1076,7 +1080,9 @@ class Fitter:
             else:
                 raise FittingError(f"Unknown param: {param}")
         elif "minuit" in self.backend:
-            return self.result.params[param].error  # TODO minos vs hesse?
+            if param in self.result.parameters:
+                return self.result.params[param].error  # TODO minos vs hesse?
+            raise FittingError(f"Unknown param: {param}")
         raise FittingError(f"Unknown backend: {self.backend}")
 
     def param_rel_unc(self, param):


Test patch:
diff --git a/tests/fitting_test.py b/tests/fitting_test.py
index f39cbd0..9cff1b7 100644
--- a/tests/fitting_test.py
+++ b/tests/fitting_test.py
@@ -291,6 +291,13 @@ class TestFittingHighStatSimData:
         assert bq.fitting._is_count_like(fitter.y_roi)
         assert not bq.fitting._is_count_like(fitter.y_roi * 0.5)
 
+        name0 = fitter.param_names[0]
+        fitter.param_val(name0)
+        fitter.param_unc(name0)
+        with pytest.raises(bq.core.fitting.FittingError):
+            fitter.param_val("bad_name")
+        with pytest.raises(bq.core.fitting.FittingError):
+            fitter.param_unc("bad_name")
         if "gauss_amp" in fitter.param_names:
             u = fitter.param_rel_unc("gauss_amp")
             assert u is None or u < 0.01