def elements_and_dtype(elements, dtype, source=None): if source is None: prefix = "" else: prefix = f"{source}." if elements is not None: check_strategy(elements, f"{prefix}elements") else: with check("dtype is not None"): if dtype is None: raise InvalidArgument( f"At least one of {prefix}elements or {prefix}dtype must be provided." ) with check("is_categorical_dtype"): if is_categorical_dtype(dtype): raise InvalidArgument( f"{prefix}dtype is categorical, which is currently unsupported" ) if isinstance( dtype, type) and np.dtype(dtype).kind == "O" and dtype is not object: note_deprecation( f"Passed dtype={dtype!r} is not a valid Pandas dtype. We'll treat it as " "dtype=object for now, but this will be an error in a future version.", since="2021-12-31", has_codemod=False, ) dtype = try_convert(np.dtype, dtype, "dtype") if elements is None: elements = npst.from_dtype(dtype) elif dtype is not None: def convert_element(value): name = f"draw({prefix}elements)" try: return np.array([value], dtype=dtype)[0] except TypeError: raise InvalidArgument( "Cannot convert %s=%r of type %s to dtype %s" % (name, value, type(value).__name__, dtype.str)) from None except ValueError: raise InvalidArgument( f"Cannot convert {name}={value!r} to type {dtype.str}" ) from None elements = elements.map(convert_element) assert elements is not None return elements, dtype
def elements_and_dtype(elements, dtype, source=None): if source is None: prefix = "" else: prefix = "%s." % (source,) if elements is not None: st.check_strategy(elements, "%selements" % (prefix,)) else: with check("dtype is not None"): if dtype is None: raise InvalidArgument( ( "At least one of %(prefix)selements or %(prefix)sdtype " "must be provided." ) % {"prefix": prefix} ) with check("is_categorical_dtype"): if is_categorical_dtype(dtype): raise InvalidArgument( "%sdtype is categorical, which is currently unsupported" % (prefix,) ) dtype = try_convert(np.dtype, dtype, "dtype") if elements is None: elements = npst.from_dtype(dtype) elif dtype is not None: def convert_element(value): name = "draw(%selements)" % (prefix,) try: return np.array([value], dtype=dtype)[0] except TypeError: raise InvalidArgument( "Cannot convert %s=%r of type %s to dtype %s" % (name, value, type(value).__name__, dtype.str) ) except ValueError: raise InvalidArgument( "Cannot convert %s=%r to type %s" % (name, value, dtype.str) ) elements = elements.map(convert_element) assert elements is not None return elements, dtype
def elements_and_dtype(elements, dtype, source=None): if source is None: prefix = "" else: prefix = "%s." % (source,) if elements is not None: st.check_strategy(elements, "%selements" % (prefix,)) else: with check("dtype is not None"): if dtype is None: raise InvalidArgument( ( "At least one of %(prefix)selements or %(prefix)sdtype " "must be provided." ) % {"prefix": prefix} ) with check("is_categorical_dtype"): if is_categorical_dtype(dtype): raise InvalidArgument( "%sdtype is categorical, which is currently unsupported" % (prefix,) ) dtype = try_convert(np.dtype, dtype, "dtype") if elements is None: elements = npst.from_dtype(dtype) elif dtype is not None: def convert_element(value): name = "draw(%selements)" % (prefix,) try: return np.array([value], dtype=dtype)[0] except TypeError: raise InvalidArgument( "Cannot convert %s=%r of type %s to dtype %s" % (name, value, type(value).__name__, dtype.str) ) except ValueError: raise InvalidArgument( "Cannot convert %s=%r to type %s" % (name, value, dtype.str) ) elements = elements.map(convert_element) assert elements is not None return elements, dtype
def elements_and_dtype(elements, dtype, source=None): if source is None: prefix = '' else: prefix = '%s.' % (source,) if elements is not None: st.check_strategy(elements, '%selements' % (prefix,)) else: with check('dtype is not None'): if dtype is None: raise InvalidArgument(( 'At least one of %(prefix)selements or %(prefix)sdtype ' 'must be provided.') % {'prefix': prefix}) with check('is_categorical_dtype'): if is_categorical_dtype(dtype): raise InvalidArgument( '%sdtype is categorical, which is currently unsupported' % ( prefix, )) dtype = st.try_convert(np.dtype, dtype, 'dtype') if elements is None: elements = npst.from_dtype(dtype) elif dtype is not None: def convert_element(value): name = 'draw(%selements)' % (prefix,) try: return np.array([value], dtype=dtype)[0] except TypeError: raise InvalidArgument( 'Cannot convert %s=%r of type %s to dtype %s' % ( name, value, type(value).__name__, dtype.str ) ) except ValueError: raise InvalidArgument( 'Cannot convert %s=%r to type %s' % ( name, value, dtype.str, ) ) elements = elements.map(convert_element) assert elements is not None return elements, dtype
def elements_and_dtype(elements, dtype, source=None): if source is None: prefix = '' else: prefix = '%s.' % (source,) if elements is not None: st.check_strategy(elements, '%selements' % (prefix,)) else: with check('dtype is not None'): if dtype is None: raise InvalidArgument(( 'At least one of %(prefix)selements or %(prefix)sdtype ' 'must be provided.') % {'prefix': prefix}) with check('is_categorical_dtype'): if is_categorical_dtype(dtype): raise InvalidArgument( '%sdtype is categorical, which is currently unsupported' % ( prefix, )) dtype = st.try_convert(np.dtype, dtype, 'dtype') if elements is None: elements = npst.from_dtype(dtype) elif dtype is not None: def convert_element(value): name = 'draw(%selements)' % (prefix,) try: return np.array([value], dtype=dtype)[0] except TypeError: raise InvalidArgument( 'Cannot convert %s=%r of type %s to dtype %s' % ( name, value, type(value).__name__, dtype.str ) ) except ValueError: raise InvalidArgument( 'Cannot convert %s=%r to type %s' % ( name, value, dtype.str, ) ) elements = elements.map(convert_element) assert elements is not None return elements, dtype
def elements_and_dtype(elements, dtype, source=None): if source is None: prefix = "" else: prefix = f"{source}." if elements is not None: check_strategy(elements, f"{prefix}elements") else: with check("dtype is not None"): if dtype is None: raise InvalidArgument( f"At least one of {prefix}elements or {prefix}dtype must be provided." ) with check("is_categorical_dtype"): if is_categorical_dtype(dtype): raise InvalidArgument( f"{prefix}dtype is categorical, which is currently unsupported" ) dtype = try_convert(np.dtype, dtype, "dtype") if elements is None: elements = npst.from_dtype(dtype) elif dtype is not None: def convert_element(value): name = f"draw({prefix}elements)" try: return np.array([value], dtype=dtype)[0] except TypeError: raise InvalidArgument( "Cannot convert %s=%r of type %s to dtype %s" % (name, value, type(value).__name__, dtype.str)) from None except ValueError: raise InvalidArgument( f"Cannot convert {name}={value!r} to type {dtype.str}" ) from None elements = elements.map(convert_element) assert elements is not None return elements, dtype