Browse Source

added an exception to `Regression.predict()` if both arguments 'x' and 'y' are specified.

main
Holger Frey 2 years ago
parent
commit
59a642d2bd
  1. 6
      linear_regression.py

6
linear_regression.py

@ -21,6 +21,9 @@ class Regression:
def predict(self, *, x: int | float = None, y: int | float = None) -> float: def predict(self, *, x: int | float = None, y: int | float = None) -> float:
"""predict a value if x or y is given""" """predict a value if x or y is given"""
if x is not None and y is not None:
msg = "predict() expects one keyword argument 'x' or 'y', got both"
raise TypeError(msg)
if x is not None: if x is not None:
return self.intercept + x * self.coefficient return self.intercept + x * self.coefficient
if y is not None: if y is not None:
@ -77,6 +80,9 @@ def test_regression_predict_exceptions(example_data):
with pytest.raises(TypeError, match="expects a keyword"): with pytest.raises(TypeError, match="expects a keyword"):
regression.predict() regression.predict()
with pytest.raises(TypeError, match="expects one keyword"):
regression.predict(x=1, y=2)
with pytest.raises(TypeError, match="takes 1 positional argument but"): with pytest.raises(TypeError, match="takes 1 positional argument but"):
regression.predict(1) regression.predict(1)

Loading…
Cancel
Save