From 7544cf46acee502e924d25ba0d27ec5b1346bf7e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 10 Sep 2019 18:30:56 -0700 Subject: [PATCH 1/3] CLN: Exception in core.dtypes --- pandas/core/dtypes/common.py | 6 ++---- pandas/io/pytables.py | 2 +- pandas/tests/dtypes/test_common.py | 2 +- pandas/tests/indexes/interval/test_astype.py | 2 +- pandas/tests/indexes/interval/test_construction.py | 2 +- pandas/tests/io/parser/test_dtypes.py | 2 +- 6 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index f75493be2dab1..cf1a602e9e9f2 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -2049,10 +2049,8 @@ def pandas_dtype(dtype): # raise a consistent TypeError if failed try: npdtype = np.dtype(dtype) - except Exception: - # we don't want to force a repr of the non-string - if not isinstance(dtype, str): - raise TypeError("data type not understood") + except SyntaxError: + # np.dtype uses `eval` which can raise SyntaxError raise TypeError("data type '{}' not understood".format(dtype)) # Any invalid dtype (such as pd.Timestamp) should raise an error. diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 1ff3400323e54..4f3f639de5cb1 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1791,7 +1791,7 @@ def convert(self, values, nan_rep, encoding, errors, start=None, stop=None): # making an Index instance could throw a number of different errors try: self.values = Index(values, **kwargs) - except Exception: # noqa: E722 + except Exception: # if the output freq is different that what we recorded, # it should be None (see also 'doc example part 2') diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 36548f3515a48..56e556a11d769 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -678,7 +678,7 @@ def test__get_dtype_sparse(): (None, "Cannot deduce dtype from null object"), (1, "data type not understood"), (1.2, "data type not understood"), - ("random string", "data type 'random string' not understood"), + ("random string", "data type \"random string\" not understood"), (pd.DataFrame([1, 2]), "data type not understood"), ], ) diff --git a/pandas/tests/indexes/interval/test_astype.py b/pandas/tests/indexes/interval/test_astype.py index 91022fef16521..a7396ffa783ec 100644 --- a/pandas/tests/indexes/interval/test_astype.py +++ b/pandas/tests/indexes/interval/test_astype.py @@ -67,7 +67,7 @@ def test_astype_cannot_cast(self, index, dtype): index.astype(dtype) def test_astype_invalid_dtype(self, index): - msg = "data type 'fake_dtype' not understood" + msg = "data type \"fake_dtype\" not understood" with pytest.raises(TypeError, match=msg): index.astype("fake_dtype") diff --git a/pandas/tests/indexes/interval/test_construction.py b/pandas/tests/indexes/interval/test_construction.py index 82a10d24dad30..fb92c64ef3408 100644 --- a/pandas/tests/indexes/interval/test_construction.py +++ b/pandas/tests/indexes/interval/test_construction.py @@ -164,7 +164,7 @@ def test_generic_errors(self, constructor): constructor(dtype="int64", **filler) # invalid dtype - msg = "data type 'invalid' not understood" + msg = "data type \"invalid\" not understood" with pytest.raises(TypeError, match=msg): constructor(dtype="invalid", **filler) diff --git a/pandas/tests/io/parser/test_dtypes.py b/pandas/tests/io/parser/test_dtypes.py index 92c91565e1c23..8e6da7e38bc50 100644 --- a/pandas/tests/io/parser/test_dtypes.py +++ b/pandas/tests/io/parser/test_dtypes.py @@ -79,7 +79,7 @@ def test_invalid_dtype_per_column(all_parsers): 3,4.5 4,5.5""" - with pytest.raises(TypeError, match="data type 'foo' not understood"): + with pytest.raises(TypeError, match="data type \"foo\" not understood"): parser.read_csv(StringIO(data), dtype={"one": "foo", 1: "int"}) From 1dca60656566ce98db6163285cd514c7bc3353ea Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 10 Sep 2019 18:33:27 -0700 Subject: [PATCH 2/3] CLN: Exception in arrays.sparse --- pandas/core/arrays/sparse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 201174b6b1995..24dafd11ae2b7 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -245,10 +245,10 @@ def construct_from_string(cls, string): if string.startswith("Sparse"): try: sub_type, has_fill_value = cls._parse_subtype(string) - result = SparseDtype(sub_type) - except Exception: + except ValueError: raise TypeError(msg) else: + result = SparseDtype(sub_type) msg = ( "Could not construct SparseDtype from '{}'.\n\nIt " "looks like the fill_value in the string is not " From c41782543859fe3a654f6dba3adf21ca15d71f39 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 11 Sep 2019 13:21:54 -0700 Subject: [PATCH 3/3] black fixup --- pandas/tests/dtypes/test_common.py | 2 +- pandas/tests/indexes/interval/test_astype.py | 2 +- pandas/tests/indexes/interval/test_construction.py | 2 +- pandas/tests/io/parser/test_dtypes.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 56e556a11d769..db9f647e0f0c7 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -678,7 +678,7 @@ def test__get_dtype_sparse(): (None, "Cannot deduce dtype from null object"), (1, "data type not understood"), (1.2, "data type not understood"), - ("random string", "data type \"random string\" not understood"), + ("random string", 'data type "random string" not understood'), (pd.DataFrame([1, 2]), "data type not understood"), ], ) diff --git a/pandas/tests/indexes/interval/test_astype.py b/pandas/tests/indexes/interval/test_astype.py index a7396ffa783ec..863b8c9082f07 100644 --- a/pandas/tests/indexes/interval/test_astype.py +++ b/pandas/tests/indexes/interval/test_astype.py @@ -67,7 +67,7 @@ def test_astype_cannot_cast(self, index, dtype): index.astype(dtype) def test_astype_invalid_dtype(self, index): - msg = "data type \"fake_dtype\" not understood" + msg = 'data type "fake_dtype" not understood' with pytest.raises(TypeError, match=msg): index.astype("fake_dtype") diff --git a/pandas/tests/indexes/interval/test_construction.py b/pandas/tests/indexes/interval/test_construction.py index fb92c64ef3408..98c1f7c6c2a8a 100644 --- a/pandas/tests/indexes/interval/test_construction.py +++ b/pandas/tests/indexes/interval/test_construction.py @@ -164,7 +164,7 @@ def test_generic_errors(self, constructor): constructor(dtype="int64", **filler) # invalid dtype - msg = "data type \"invalid\" not understood" + msg = 'data type "invalid" not understood' with pytest.raises(TypeError, match=msg): constructor(dtype="invalid", **filler) diff --git a/pandas/tests/io/parser/test_dtypes.py b/pandas/tests/io/parser/test_dtypes.py index 8e6da7e38bc50..a68d46e8a6c15 100644 --- a/pandas/tests/io/parser/test_dtypes.py +++ b/pandas/tests/io/parser/test_dtypes.py @@ -79,7 +79,7 @@ def test_invalid_dtype_per_column(all_parsers): 3,4.5 4,5.5""" - with pytest.raises(TypeError, match="data type \"foo\" not understood"): + with pytest.raises(TypeError, match='data type "foo" not understood'): parser.read_csv(StringIO(data), dtype={"one": "foo", 1: "int"})