From 2c99c489a9fa0060cd362299c170a30c82196c9d Mon Sep 17 00:00:00 2001 From: beyarkay Date: Tue, 11 Jul 2023 22:37:07 +0200 Subject: [PATCH 1/2] Replace confusing brackets with backticks There are some errors which read like: > only list-like objects are allowed to be passed to isin(), you > passed a [str] (note how the type `str` is enclosed in square brackets) This is potentially confusing, since some languages use notation like `[TYPE]` to denote a list of TYPE objects. So the user could read the error message saying they can't use `[str]`, but intepret `[str]` to mean "a list of str objects". This commit removes the possibility of confusion by using backticks instead of square brackets. --- pandas/core/algorithms.py | 4 ++-- pandas/core/arrays/categorical.py | 2 +- pandas/tests/series/methods/test_isin.py | 2 +- pandas/tests/test_algos.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 5783842e9ddef..e0340f99b92e1 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -470,12 +470,12 @@ def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]: if not is_list_like(comps): raise TypeError( "only list-like objects are allowed to be passed " - f"to isin(), you passed a [{type(comps).__name__}]" + f"to isin(), you passed a `{type(comps).__name__}`" ) if not is_list_like(values): raise TypeError( "only list-like objects are allowed to be passed " - f"to isin(), you passed a [{type(values).__name__}]" + f"to isin(), you passed a `{type(values).__name__}`" ) if not isinstance(values, (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray)): diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 8898379689cfd..46e2b64cb60c6 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2581,7 +2581,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]: values_type = type(values).__name__ raise TypeError( "only list-like objects are allowed to be passed " - f"to isin(), you passed a [{values_type}]" + f"to isin(), you passed a `{values_type}`" ) values = sanitize_array(values, None, None) null_mask = np.asarray(isna(values)) diff --git a/pandas/tests/series/methods/test_isin.py b/pandas/tests/series/methods/test_isin.py index 3e4857b7abf38..4ea2fbf51afc9 100644 --- a/pandas/tests/series/methods/test_isin.py +++ b/pandas/tests/series/methods/test_isin.py @@ -34,7 +34,7 @@ def test_isin_with_string_scalar(self): s = Series(["A", "B", "C", "a", "B", "B", "A", "C"]) msg = ( r"only list-like objects are allowed to be passed to isin\(\), " - r"you passed a \[str\]" + r"you passed a `str`" ) with pytest.raises(TypeError, match=msg): s.isin("a") diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 8c26bbd209a6a..34a0bee877664 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -888,7 +888,7 @@ class TestIsin: def test_invalid(self): msg = ( r"only list-like objects are allowed to be passed to isin\(\), " - r"you passed a \[int\]" + r"you passed a `int`" ) with pytest.raises(TypeError, match=msg): algos.isin(1, 1) From 696dac3ff033272e61e18010f2bb30239c7fabf6 Mon Sep 17 00:00:00 2001 From: beyarkay Date: Wed, 12 Jul 2023 10:45:20 +0200 Subject: [PATCH 2/2] Fix: also update some missed tests --- pandas/tests/computation/test_eval.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index 35960c707d3bd..e6e1363603e09 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -158,7 +158,7 @@ def test_simple_cmp_ops(self, cmp_op, lhs, rhs, engine, parser): [ r"only list-like( or dict-like)? objects are allowed to be " r"passed to (DataFrame\.)?isin\(\), you passed a " - r"(\[|')bool(\]|')", + r"(`|')bool(`|')", "argument of type 'bool' is not iterable", ] ) @@ -203,7 +203,7 @@ def test_compound_invert_op(self, op, lhs, rhs, request, engine, parser): [ r"only list-like( or dict-like)? objects are allowed to be " r"passed to (DataFrame\.)?isin\(\), you passed a " - r"(\[|')float(\]|')", + r"(`|')float(`|')", "argument of type 'float' is not iterable", ] )