Skip to content

Commit

Permalink
Add module name to expected meta error message (#4499)
Browse files Browse the repository at this point in the history
* Add module name to expected meta error message

* handle builtins and None in typename
  • Loading branch information
mrocklin authored and jcrist committed Feb 19, 2019
1 parent 6d27415 commit f6b61fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dask/dataframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ..context import globalmethod
from ..utils import (random_state_data, pseudorandom, derived_from, funcname,
memory_repr, put_lines, M, key_split, OperatorMethodMixin,
is_arraylike)
is_arraylike, typename)
from ..array.core import Array, normalize_arg
from ..blockwise import blockwise, Blockwise
from ..base import DaskMethodsMixin, tokenize, dont_optimize, is_dask_collection
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(self, dsk, name, meta, divisions=None):
meta = make_meta(meta)
if is_dataframe_like(meta) or is_series_like(meta) or is_index_like(meta):
raise TypeError("Expected meta to specify scalar, got "
"{0}".format(type(meta).__name__))
"{0}".format(typename(type(meta))))
self._meta = meta

def __dask_graph__(self):
Expand Down Expand Up @@ -253,8 +253,8 @@ def __init__(self, dsk, name, meta, divisions):
meta = make_meta(meta)
if not isinstance(meta, self._partition_type):
raise TypeError("Expected meta to specify type {0}, got type "
"{1}".format(self._partition_type.__name__,
type(meta).__name__))
"{1}".format(typename(self._partition_type),
typename(type(meta))))
self._meta = meta
self.divisions = tuple(divisions)

Expand Down
9 changes: 9 additions & 0 deletions dask/dataframe/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3380,3 +3380,12 @@ def test_has_parallel_type():
assert has_parallel_type(pd.DataFrame())
assert has_parallel_type(pd.Series())
assert not has_parallel_type(123)


def test_meta_error_message():
with pytest.raises(TypeError) as info:
dd.DataFrame({('x', 1): 123}, 'x', pd.Series(), [None, None])

assert 'Series' in str(info.value)
assert 'DataFrame' in str(info.value)
assert 'pandas' in str(info.value)
19 changes: 19 additions & 0 deletions dask/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,25 @@ def funcname(func):
return str(func)


def typename(typ):
"""
Return the name of a type
Examples
--------
>>> typename(int)
'int'
>>> from dask.core import literal
>>> typename(literal)
'dask.core.literal'
"""
if not typ.__module__ or typ.__module__ == 'builtins':
return typ.__name__
else:
return typ.__module__ + '.' + typ.__name__


def ensure_bytes(s):
""" Turn string or bytes to bytes
Expand Down

0 comments on commit f6b61fc

Please sign in to comment.