diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2dbd592fc6787..f3ee6450cc3bc 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -452,7 +452,7 @@ def __new__( elif hasattr(data, "__array__"): return Index(np.asarray(data), dtype=dtype, copy=copy, name=name, **kwargs) elif data is None or is_scalar(data): - cls._scalar_data_error(data) + raise cls._scalar_data_error(data) else: if tupleize_cols and is_list_like(data): # GH21470: convert iterable to list before determining if empty @@ -4020,7 +4020,9 @@ def _try_convert_to_int_index(cls, data, copy, name, dtype): @classmethod def _scalar_data_error(cls, data): - raise TypeError( + # We return the TypeError so that we can raise it from the constructor + # in order to keep mypy happy + return TypeError( "{0}(...) must be called with a collection of some " "kind, {1} was passed".format(cls.__name__, repr(data)) ) @@ -4048,7 +4050,7 @@ def _coerce_to_ndarray(cls, data): if not isinstance(data, (np.ndarray, Index)): if data is None or is_scalar(data): - cls._scalar_data_error(data) + raise cls._scalar_data_error(data) # other iterable of some kind if not isinstance(data, (ABCSeries, list, tuple)): diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 82806c7351db6..c4321c993e638 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -194,7 +194,7 @@ def __new__( # if data is None, then categories must be provided if is_scalar(data): if data is not None or categories is None: - cls._scalar_data_error(data) + raise cls._scalar_data_error(data) data = [] data = cls._create_categorical(data, dtype=dtype)