Skip to content

Commit

Permalink
docs: better formatting for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed May 5, 2024
1 parent 6248183 commit 6088859
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 7 additions & 5 deletions pysr/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,15 +884,15 @@ def __init__(
updated_kwarg_name = DEPRECATED_KWARGS[k]
setattr(self, updated_kwarg_name, v)
warnings.warn(
f"{k} has been renamed to {updated_kwarg_name} in PySRRegressor. "
f"`{k}` has been renamed to `{updated_kwarg_name}` in PySRRegressor. "
"Please use that instead.",
FutureWarning,
)
# Handle kwargs that have been moved to the fit method
elif k in ["weights", "variable_names", "Xresampled"]:
warnings.warn(
f"{k} is a data dependant parameter so should be passed when fit is called. "
f"Ignoring parameter; please pass {k} during the call to fit instead.",
f"`{k}` is a data-dependent parameter and should be passed when fit is called. "
f"Ignoring parameter; please pass `{k}` during the call to fit instead.",
FutureWarning,
)
elif k == "julia_project":
Expand All @@ -910,9 +910,11 @@ def __init__(
)
else:
suggested_keywords = _suggest_keywords(PySRRegressor, k)
err_msg = f"{k} is not a valid keyword argument for PySRRegressor."
err_msg = (
f"`{k}` is not a valid keyword argument for PySRRegressor."
)
if len(suggested_keywords) > 0:
err_msg += f" Did you mean {' or '.join(suggested_keywords)}?"
err_msg += f" Did you mean {', '.join(map(lambda s: f'`{s}`', suggested_keywords))}?"
raise TypeError(err_msg)

@classmethod
Expand Down
10 changes: 6 additions & 4 deletions pysr/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,16 +819,18 @@ def test_suggest_keywords(self):
with self.assertRaises(TypeError) as cm:
model = PySRRegressor(ncyclesperiterationn=5)

self.assertIn("ncyclesperiterationn is not a valid keyword", str(cm.exception))
self.assertIn(
"`ncyclesperiterationn` is not a valid keyword", str(cm.exception)
)
self.assertIn("Did you mean", str(cm.exception))
self.assertIn("ncycles_per_iteration or", str(cm.exception))
self.assertIn("niteration", str(cm.exception))
self.assertIn("`ncycles_per_iteration`, ", str(cm.exception))
self.assertIn("`niterations`", str(cm.exception))

# Farther matches (this might need to be changed)
with self.assertRaises(TypeError) as cm:
model = PySRRegressor(operators=["+", "-"])

self.assertIn("unary_operators or binary_operators", str(cm.exception))
self.assertIn("`unary_operators`, `binary_operators`", str(cm.exception))


TRUE_PREAMBLE = "\n".join(
Expand Down

0 comments on commit 6088859

Please sign in to comment.