diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/_ufunc_config.py | 4 | ||||
-rw-r--r-- | numpy/core/arrayprint.py | 59 | ||||
-rw-r--r-- | numpy/core/code_generators/generate_umath.py | 6 | ||||
-rw-r--r-- | numpy/core/einsumfunc.py | 62 | ||||
-rw-r--r-- | numpy/core/getlimits.py | 12 | ||||
-rw-r--r-- | numpy/core/numeric.py | 3 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 4 | ||||
-rw-r--r-- | numpy/doc/subclassing.py | 4 | ||||
-rw-r--r-- | numpy/lib/function_base.py | 27 | ||||
-rw-r--r-- | numpy/lib/stride_tricks.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test_stride_tricks.py | 9 | ||||
-rw-r--r-- | numpy/testing/_private/utils.py | 3 |
12 files changed, 76 insertions, 125 deletions
diff --git a/numpy/core/_ufunc_config.py b/numpy/core/_ufunc_config.py index cc8d2bcb0..4872a5385 100644 --- a/numpy/core/_ufunc_config.py +++ b/numpy/core/_ufunc_config.py @@ -430,8 +430,8 @@ class errstate(contextlib.ContextDecorator): """ - def __init__(self, **kwargs): - self.call = kwargs.pop('call', _Unspecified) + def __init__(self, *, call=_Unspecified, **kwargs): + self.call = call self.kwargs = kwargs def __enter__(self): diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 136b9ecff..918da4a72 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -98,7 +98,7 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None, @set_module('numpy') def set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, - formatter=None, sign=None, floatmode=None, **kwarg): + formatter=None, sign=None, floatmode=None, *, legacy=None): """ Set printing options. @@ -247,11 +247,6 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, array([ 0. , 1.11, 2.22, ..., 7.78, 8.89, 10. ]) """ - legacy = kwarg.pop('legacy', None) - if kwarg: - msg = "set_printoptions() got unexpected keyword argument '{}'" - raise TypeError(msg.format(kwarg.popitem()[0])) - opt = _make_options_dict(precision, threshold, edgeitems, linewidth, suppress, nanstr, infstr, sign, formatter, floatmode, legacy) @@ -367,23 +362,22 @@ def repr_format(x): def str_format(x): return str(x) -def _get_formatdict(data, **opt): - prec, fmode = opt['precision'], opt['floatmode'] - supp, sign = opt['suppress'], opt['sign'] - legacy = opt['legacy'] +def _get_formatdict(data, *, precision, floatmode, suppress, sign, legacy, + formatter, **kwargs): + # note: extra arguments in kwargs are ignored # wrapped in lambdas to avoid taking a code path with the wrong type of data formatdict = { 'bool': lambda: BoolFormat(data), 'int': lambda: IntegerFormat(data), - 'float': lambda: - FloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), - 'longfloat': lambda: - FloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), - 'complexfloat': lambda: - ComplexFloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), - 'longcomplexfloat': lambda: - ComplexFloatingFormat(data, prec, fmode, supp, sign, legacy=legacy), + 'float': lambda: FloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), + 'longfloat': lambda: FloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), + 'complexfloat': lambda: ComplexFloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), + 'longcomplexfloat': lambda: ComplexFloatingFormat( + data, precision, floatmode, suppress, sign, legacy=legacy), 'datetime': lambda: DatetimeFormat(data, legacy=legacy), 'timedelta': lambda: TimedeltaFormat(data), 'object': lambda: _object_format, @@ -396,7 +390,6 @@ def _get_formatdict(data, **opt): def indirect(x): return lambda: x - formatter = opt['formatter'] if formatter is not None: fkeys = [k for k in formatter.keys() if formatter[k] is not None] if 'all' in fkeys: @@ -523,7 +516,7 @@ def _array2string_dispatcher( suppress_small=None, separator=None, prefix=None, style=None, formatter=None, threshold=None, edgeitems=None, sign=None, floatmode=None, suffix=None, - **kwarg): + *, legacy=None): return (a,) @@ -532,7 +525,7 @@ def array2string(a, max_line_width=None, precision=None, suppress_small=None, separator=' ', prefix="", style=np._NoValue, formatter=None, threshold=None, edgeitems=None, sign=None, floatmode=None, suffix="", - **kwarg): + *, legacy=None): """ Return a string representation of an array. @@ -677,10 +670,6 @@ def array2string(a, max_line_width=None, precision=None, '[0x0 0x1 0x2]' """ - legacy = kwarg.pop('legacy', None) - if kwarg: - msg = "array2string() got unexpected keyword argument '{}'" - raise TypeError(msg.format(kwarg.popitem()[0])) overrides = _make_options_dict(precision, threshold, edgeitems, max_line_width, suppress_small, None, None, @@ -852,12 +841,12 @@ def _none_or_positive_arg(x, name): class FloatingFormat: """ Formatter for subtypes of np.floating """ def __init__(self, data, precision, floatmode, suppress_small, sign=False, - **kwarg): + *, legacy=None): # for backcompatibility, accept bools if isinstance(sign, bool): sign = '+' if sign else '-' - self._legacy = kwarg.get('legacy', False) + self._legacy = legacy if self._legacy == '1.13': # when not 0d, legacy does not support '-' if data.shape != () and sign == '-': @@ -1164,20 +1153,24 @@ class BoolFormat: class ComplexFloatingFormat: """ Formatter for subtypes of np.complexfloating """ def __init__(self, x, precision, floatmode, suppress_small, - sign=False, **kwarg): + sign=False, *, legacy=None): # for backcompatibility, accept bools if isinstance(sign, bool): sign = '+' if sign else '-' floatmode_real = floatmode_imag = floatmode - if kwarg.get('legacy', False) == '1.13': + if legacy == '1.13': floatmode_real = 'maxprec_equal' floatmode_imag = 'maxprec' - self.real_format = FloatingFormat(x.real, precision, floatmode_real, - suppress_small, sign=sign, **kwarg) - self.imag_format = FloatingFormat(x.imag, precision, floatmode_imag, - suppress_small, sign='+', **kwarg) + self.real_format = FloatingFormat( + x.real, precision, floatmode_real, suppress_small, + sign=sign, legacy=legacy + ) + self.imag_format = FloatingFormat( + x.imag, precision, floatmode_imag, suppress_small, + sign='+', legacy=legacy + ) def __call__(self, x): r = self.real_format(x.real) diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index c517248de..6f18d5aa9 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -130,7 +130,7 @@ class Ufunc: type_descriptions : list of TypeDescription objects """ def __init__(self, nin, nout, identity, docstring, typereso, - *type_descriptions, **kwargs): + *type_descriptions, signature=None): self.nin = nin self.nout = nout if identity is None: @@ -139,13 +139,11 @@ class Ufunc: self.docstring = docstring self.typereso = typereso self.type_descriptions = [] - self.signature = kwargs.pop('signature', None) + self.signature = signature for td in type_descriptions: self.type_descriptions.extend(td) for td in self.type_descriptions: td.finish_signature(self.nin, self.nout) - if kwargs: - raise ValueError('unknown kwargs %r' % str(kwargs)) # String-handling utilities to avoid locale-dependence. diff --git a/numpy/core/einsumfunc.py b/numpy/core/einsumfunc.py index 8ae14ce30..ec3eb19d2 100644 --- a/numpy/core/einsumfunc.py +++ b/numpy/core/einsumfunc.py @@ -689,7 +689,7 @@ def _parse_einsum_input(operands): return (input_subscripts, output_subscript, operands) -def _einsum_path_dispatcher(*operands, **kwargs): +def _einsum_path_dispatcher(*operands, optimize=None, einsum_call=None): # NOTE: technically, we should only dispatch on array-like arguments, not # subscripts (given as strings). But separating operands into # arrays/subscripts is a little tricky/slow (given einsum's two supported @@ -700,7 +700,7 @@ def _einsum_path_dispatcher(*operands, **kwargs): @array_function_dispatch(_einsum_path_dispatcher, module='numpy') -def einsum_path(*operands, **kwargs): +def einsum_path(*operands, optimize='greedy', einsum_call=False): """ einsum_path(subscripts, *operands, optimize='greedy') @@ -810,16 +810,8 @@ def einsum_path(*operands, **kwargs): 5 defg,hd->efgh efgh->efgh """ - # Make sure all keywords are valid - valid_contract_kwargs = ['optimize', 'einsum_call'] - unknown_kwargs = [k for (k, v) in kwargs.items() if k - not in valid_contract_kwargs] - if len(unknown_kwargs): - raise TypeError("Did not understand the following kwargs:" - " %s" % unknown_kwargs) - # Figure out what the path really is - path_type = kwargs.pop('optimize', True) + path_type = optimize if path_type is True: path_type = 'greedy' if path_type is None: @@ -845,7 +837,7 @@ def einsum_path(*operands, **kwargs): raise TypeError("Did not understand the path: %s" % str(path_type)) # Hidden option, only einsum should call this - einsum_call_arg = kwargs.pop("einsum_call", False) + einsum_call_arg = einsum_call # Python side parsing input_subscripts, output_subscript, operands = _parse_einsum_input(operands) @@ -990,17 +982,17 @@ def einsum_path(*operands, **kwargs): return (path, path_print) -def _einsum_dispatcher(*operands, **kwargs): +def _einsum_dispatcher(*operands, out=None, optimize=None, **kwargs): # Arguably we dispatch on more arguments that we really should; see note in # _einsum_path_dispatcher for why. for op in operands: yield op - yield kwargs.get('out') + yield out # Rewrite einsum to handle different cases @array_function_dispatch(_einsum_dispatcher, module='numpy') -def einsum(*operands, **kwargs): +def einsum(*operands, out=None, optimize=False, **kwargs): """ einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False) @@ -1345,39 +1337,29 @@ def einsum(*operands, **kwargs): ... _ = np.einsum('ijk,ilm,njm,nlk,abc->',a,a,a,a,a, optimize=path) """ - - # Grab non-einsum kwargs; do not optimize by default. - optimize_arg = kwargs.pop('optimize', False) + # Special handling if out is specified + specified_out = out is not None # If no optimization, run pure einsum - if optimize_arg is False: + if optimize is False: + if specified_out: + kwargs['out'] = out return c_einsum(*operands, **kwargs) - valid_einsum_kwargs = ['out', 'dtype', 'order', 'casting'] - einsum_kwargs = {k: v for (k, v) in kwargs.items() if - k in valid_einsum_kwargs} - - # Make sure all keywords are valid - valid_contract_kwargs = ['optimize'] + valid_einsum_kwargs + # Check the kwargs to avoid a more cryptic error later, without having to + # repeat default values here + valid_einsum_kwargs = ['dtype', 'order', 'casting'] unknown_kwargs = [k for (k, v) in kwargs.items() if - k not in valid_contract_kwargs] - + k not in valid_einsum_kwargs] if len(unknown_kwargs): raise TypeError("Did not understand the following kwargs: %s" % unknown_kwargs) - # Special handeling if out is specified - specified_out = False - out_array = einsum_kwargs.pop('out', None) - if out_array is not None: - specified_out = True # Build the contraction list and operand - operands, contraction_list = einsum_path(*operands, optimize=optimize_arg, + operands, contraction_list = einsum_path(*operands, optimize=optimize, einsum_call=True) - handle_out = False - # Start contraction loop for num, contraction in enumerate(contraction_list): inds, idx_rm, einsum_str, remaining, blas = contraction @@ -1408,23 +1390,23 @@ def einsum(*operands, **kwargs): # Build a new view if needed if (tensor_result != results_index) or handle_out: if handle_out: - einsum_kwargs["out"] = out_array - new_view = c_einsum(tensor_result + '->' + results_index, new_view, **einsum_kwargs) + kwargs["out"] = out + new_view = c_einsum(tensor_result + '->' + results_index, new_view, **kwargs) # Call einsum else: # If out was specified if handle_out: - einsum_kwargs["out"] = out_array + kwargs["out"] = out # Do the contraction - new_view = c_einsum(einsum_str, *tmp_operands, **einsum_kwargs) + new_view = c_einsum(einsum_str, *tmp_operands, **kwargs) # Append new items and dereference what we can operands.append(new_view) del tmp_operands, new_view if specified_out: - return out_array + return out else: return operands[0] diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index a80db1c81..b00ef64bd 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -34,7 +34,7 @@ class MachArLike: def __init__(self, ftype, - **kwargs): + *, eps, epsneg, huge, tiny, ibeta, **kwargs): params = _MACHAR_PARAMS[ftype] float_conv = lambda v: array([v], ftype) float_to_float = lambda v : _fr1(float_conv(v)) @@ -42,11 +42,11 @@ class MachArLike: self.title = params['title'] # Parameter types same as for discovered MachAr object. - self.epsilon = self.eps = float_to_float(kwargs.pop('eps')) - self.epsneg = float_to_float(kwargs.pop('epsneg')) - self.xmax = self.huge = float_to_float(kwargs.pop('huge')) - self.xmin = self.tiny = float_to_float(kwargs.pop('tiny')) - self.ibeta = params['itype'](kwargs.pop('ibeta')) + self.epsilon = self.eps = float_to_float(eps) + self.epsneg = float_to_float(epsneg) + self.xmax = self.huge = float_to_float(huge) + self.xmin = self.tiny = float_to_float(tiny) + self.ibeta = params['itype'](ibeta) self.__dict__.update(kwargs) self.precision = int(-log10(self.eps)) self.resolution = float_to_float(float_conv(10) ** (-self.precision)) diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 505218a2e..f18ab6336 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1716,7 +1716,7 @@ def indices(dimensions, dtype=int, sparse=False): @set_module('numpy') -def fromfunction(function, shape, **kwargs): +def fromfunction(function, shape, *, dtype=float, **kwargs): """ Construct an array by executing a function over each coordinate. @@ -1767,7 +1767,6 @@ def fromfunction(function, shape, **kwargs): [2, 3, 4]]) """ - dtype = kwargs.pop('dtype', float) args = indices(shape, dtype=dtype) return function(*args, **kwargs) diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 2913b1c4d..e966eebf0 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -2353,7 +2353,7 @@ class TestSpecialMethods: # NOTE: this class is given as an example in doc/subclassing.py; # if you make any changes here, do update it there too. class A(np.ndarray): - def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + def __array_ufunc__(self, ufunc, method, *inputs, out=None, **kwargs): args = [] in_no = [] for i, input_ in enumerate(inputs): @@ -2363,7 +2363,7 @@ class TestSpecialMethods: else: args.append(input_) - outputs = kwargs.pop('out', None) + outputs = out out_no = [] if outputs: out_args = [] diff --git a/numpy/doc/subclassing.py b/numpy/doc/subclassing.py index bfc01bdc6..5a54ddd90 100644 --- a/numpy/doc/subclassing.py +++ b/numpy/doc/subclassing.py @@ -454,7 +454,7 @@ following. input numpy as np class A(np.ndarray): - def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + def __array_ufunc__(self, ufunc, method, *inputs, out=None, **kwargs): args = [] in_no = [] for i, input_ in enumerate(inputs): @@ -464,7 +464,7 @@ following. else: args.append(input_) - outputs = kwargs.pop('out', None) + outputs = out out_no = [] if outputs: out_args = [] diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 6c7dfbfd3..ef8a26fe3 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -770,14 +770,14 @@ def copy(a, order='K'): # Basic operations -def _gradient_dispatcher(f, *varargs, **kwargs): +def _gradient_dispatcher(f, *varargs, axis=None, edge_order=None): yield f for v in varargs: yield v @array_function_dispatch(_gradient_dispatcher) -def gradient(f, *varargs, **kwargs): +def gradient(f, *varargs, axis=None, edge_order=1): """ Return the gradient of an N-dimensional array. @@ -954,11 +954,10 @@ def gradient(f, *varargs, **kwargs): f = np.asanyarray(f) N = f.ndim # number of dimensions - axes = kwargs.pop('axis', None) - if axes is None: + if axis is None: axes = tuple(range(N)) else: - axes = _nx.normalize_axis_tuple(axes, N) + axes = _nx.normalize_axis_tuple(axis, N) len_axes = len(axes) n = len(varargs) @@ -993,10 +992,6 @@ def gradient(f, *varargs, **kwargs): else: raise TypeError("invalid number of arguments") - edge_order = kwargs.pop('edge_order', 1) - if kwargs: - raise TypeError('"{}" are not valid keyword arguments.'.format( - '", "'.join(kwargs.keys()))) if edge_order > 2: raise ValueError("'edge_order' greater than 2 not supported") @@ -4063,13 +4058,13 @@ def trapz(y, x=None, dx=1.0, axis=-1): return ret -def _meshgrid_dispatcher(*xi, **kwargs): +def _meshgrid_dispatcher(*xi, copy=None, sparse=None, indexing=None): return xi # Based on scitools meshgrid @array_function_dispatch(_meshgrid_dispatcher) -def meshgrid(*xi, **kwargs): +def meshgrid(*xi, copy=True, sparse=False, indexing='xy'): """ Return coordinate matrices from coordinate vectors. @@ -4175,14 +4170,6 @@ def meshgrid(*xi, **kwargs): """ ndim = len(xi) - copy_ = kwargs.pop('copy', True) - sparse = kwargs.pop('sparse', False) - indexing = kwargs.pop('indexing', 'xy') - - if kwargs: - raise TypeError("meshgrid() got an unexpected keyword argument '%s'" - % (list(kwargs)[0],)) - if indexing not in ['xy', 'ij']: raise ValueError( "Valid values for `indexing` are 'xy' and 'ij'.") @@ -4200,7 +4187,7 @@ def meshgrid(*xi, **kwargs): # Return the full N-D matrix (not only the 1-D vector) output = np.broadcast_arrays(*output, subok=True) - if copy_: + if copy: output = [x.copy() for x in output] return output diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py index 9c9da2974..502235bdf 100644 --- a/numpy/lib/stride_tricks.py +++ b/numpy/lib/stride_tricks.py @@ -197,12 +197,12 @@ def _broadcast_shape(*args): return b.shape -def _broadcast_arrays_dispatcher(*args, **kwargs): +def _broadcast_arrays_dispatcher(*args, subok=None): return args @array_function_dispatch(_broadcast_arrays_dispatcher, module='numpy') -def broadcast_arrays(*args, **kwargs): +def broadcast_arrays(*args, subok=False): """ Broadcast any number of arrays against each other. @@ -253,10 +253,6 @@ def broadcast_arrays(*args, **kwargs): # return np.nditer(args, flags=['multi_index', 'zerosize_ok'], # order='C').itviews - subok = kwargs.pop('subok', False) - if kwargs: - raise TypeError('broadcast_arrays() got an unexpected keyword ' - 'argument {!r}'.format(list(kwargs.keys())[0])) args = [np.array(_m, copy=False, subok=subok) for _m in args] shape = _broadcast_shape(*args) diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index 6131ba5e1..9d95eb9d0 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -63,8 +63,7 @@ def test_broadcast_kwargs(): x = np.arange(10) y = np.arange(10) - with assert_raises_regex(TypeError, - r'broadcast_arrays\(\) got an unexpected keyword*'): + with assert_raises_regex(TypeError, 'got an unexpected keyword'): broadcast_arrays(x, y, dtype='float64') @@ -354,14 +353,12 @@ def as_strided_writeable(): class VerySimpleSubClass(np.ndarray): def __new__(cls, *args, **kwargs): - kwargs['subok'] = True - return np.array(*args, **kwargs).view(cls) + return np.array(*args, subok=True, **kwargs).view(cls) class SimpleSubClass(VerySimpleSubClass): def __new__(cls, *args, **kwargs): - kwargs['subok'] = True - self = np.array(*args, **kwargs).view(cls) + self = np.array(*args, subok=True, **kwargs).view(cls) self.info = 'simple' return self diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py index 9e5a2eb2a..914491b71 100644 --- a/numpy/testing/_private/utils.py +++ b/numpy/testing/_private/utils.py @@ -2216,8 +2216,7 @@ class suppress_warnings: del self._filters def _showwarning(self, message, category, filename, lineno, - *args, **kwargs): - use_warnmsg = kwargs.pop("use_warnmsg", None) + *args, use_warnmsg=None, **kwargs): for cat, _, pattern, mod, rec in ( self._suppressions + self._tmp_suppressions)[::-1]: if (issubclass(category, cat) and |