From 186824065b664ebae5b0b217a43b3e1b25afc472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8xbro=20Hansen?= Date: Fri, 22 Nov 2024 17:06:53 +0100 Subject: [PATCH] refactor: Remove `_dict_update` (#980) --- doc/upgrade_guide.md | 2 +- param/_utils.py | 11 +-------- param/parameterized.py | 3 +-- param/parameters.py | 39 ++++++++++++++++---------------- tests/testparameterizedobject.py | 3 +-- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/doc/upgrade_guide.md b/doc/upgrade_guide.md index d44cd989..27b2bb95 100644 --- a/doc/upgrade_guide.md +++ b/doc/upgrade_guide.md @@ -90,7 +90,7 @@ class CustomParameter(Parameter): __slots__ = ['some_attribute'] - _slot_defaults = _dict_update(Parameter._slot_defaults, + _slot_defaults = dict(Parameter._slot_defaults, default=None, some_attribute=10 ) diff --git a/param/_utils.py b/param/_utils.py index 650632ee..25f3a5fd 100644 --- a/param/_utils.py +++ b/param/_utils.py @@ -200,16 +200,7 @@ def _validate_error_prefix(parameter, attribute=None): def _is_mutable_container(value): """True for mutable containers, which typically need special handling when being copied""" - return issubclass(type(value), MUTABLE_TYPES) - - -def _dict_update(dictionary, **kwargs): - """ - Small utility to update a copy of a dict with the provided keyword args. - """ - d = dictionary.copy() - d.update(kwargs) - return d + return isinstance(value, MUTABLE_TYPES) def full_groupby(l, key=lambda x: x): diff --git a/param/parameterized.py b/param/parameterized.py index f5ce4f5c..56f27546 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -41,7 +41,6 @@ Skip, _deprecated, _deprecate_positional_args, - _dict_update, _in_ipython, _is_auto_name, _is_mutable_container, @@ -1601,7 +1600,7 @@ def __init__(self, default="0.0.0.0", allow_None=False, **kwargs): __slots__ = ['regex'] - _slot_defaults = _dict_update(Parameter._slot_defaults, default="", regex=None) + _slot_defaults = dict(Parameter._slot_defaults, default="", regex=None) @typing.overload def __init__( diff --git a/param/parameters.py b/param/parameters.py index 81a0a8c6..fe327279 100644 --- a/param/parameters.py +++ b/param/parameters.py @@ -42,7 +42,6 @@ ParamDeprecationWarning as _ParamDeprecationWarning, _deprecate_positional_args, _deprecated, - _dict_update, _validate_error_prefix, _deserialize_from_path, _named_objs, @@ -674,7 +673,7 @@ class Number(Dynamic): __slots__ = ['bounds', 'softbounds', 'inclusive_bounds', 'set_hook', 'step'] - _slot_defaults = _dict_update( + _slot_defaults = dict( Dynamic._slot_defaults, default=0.0, bounds=None, softbounds=None, inclusive_bounds=(True,True), step=None, set_hook=_compute_set_hook, ) @@ -844,7 +843,7 @@ def __setstate__(self,state): class Integer(Number): """Numeric Parameter required to be an Integer""" - _slot_defaults = _dict_update(Number._slot_defaults, default=0) + _slot_defaults = dict(Number._slot_defaults, default=0) def _validate_value(self, val, allow_None): if callable(val): @@ -870,7 +869,7 @@ def _validate_step(self, val, step): class Magnitude(Number): """Numeric Parameter required to be in the range [0.0-1.0].""" - _slot_defaults = _dict_update(Number._slot_defaults, default=1.0, bounds=(0.0,1.0)) + _slot_defaults = dict(Number._slot_defaults, default=1.0, bounds=(0.0,1.0)) @typing.overload def __init__( @@ -895,7 +894,7 @@ class Date(Number): Date parameter of datetime or date type. """ - _slot_defaults = _dict_update(Number._slot_defaults, default=None) + _slot_defaults = dict(Number._slot_defaults, default=None) @typing.overload def __init__( @@ -956,7 +955,7 @@ class CalendarDate(Number): Parameter specifically allowing dates (not datetimes). """ - _slot_defaults = _dict_update(Number._slot_defaults, default=None) + _slot_defaults = dict(Number._slot_defaults, default=None) @typing.overload def __init__( @@ -1010,7 +1009,7 @@ def deserialize(cls, value): class Boolean(Parameter): """Binary or tristate Boolean Parameter.""" - _slot_defaults = _dict_update(Parameter._slot_defaults, default=False) + _slot_defaults = dict(Parameter._slot_defaults, default=False) @typing.overload def __init__( @@ -1136,7 +1135,7 @@ class Tuple(Parameter): __slots__ = ['length'] - _slot_defaults = _dict_update(Parameter._slot_defaults, default=(0,0), length=_compute_length_of_default) + _slot_defaults = dict(Parameter._slot_defaults, default=(0,0), length=_compute_length_of_default) @typing.overload def __init__( @@ -1224,7 +1223,7 @@ def _validate_value(self, val, allow_None): class XYCoordinates(NumericTuple): """A NumericTuple for an X,Y coordinate.""" - _slot_defaults = _dict_update(NumericTuple._slot_defaults, default=(0.0, 0.0)) + _slot_defaults = dict(NumericTuple._slot_defaults, default=(0.0, 0.0)) @typing.overload def __init__( @@ -1247,7 +1246,7 @@ class Range(NumericTuple): __slots__ = ['bounds', 'inclusive_bounds', 'softbounds', 'step'] - _slot_defaults = _dict_update( + _slot_defaults = dict( NumericTuple._slot_defaults, default=None, bounds=None, inclusive_bounds=(True,True), softbounds=None, step=None ) @@ -1809,7 +1808,7 @@ def sig(self): class _SignatureSelector(Parameter): # Needs docstring; why is this a separate mixin? - _slot_defaults = _dict_update( + _slot_defaults = dict( SelectorBase._slot_defaults, _objects=_compute_selector_default, compute_default_fn=None, check_on_set=_compute_selector_checking_default, allow_None=None, instantiate=False, default=None, @@ -1998,7 +1997,7 @@ class FileSelector(Selector): """ __slots__ = ['path'] - _slot_defaults = _dict_update( + _slot_defaults = dict( Selector._slot_defaults, path="", ) @@ -2108,7 +2107,7 @@ class MultiFileSelector(ListSelector): """ __slots__ = ['path'] - _slot_defaults = _dict_update( + _slot_defaults = dict( Selector._slot_defaults, path="", ) @@ -2159,7 +2158,7 @@ class ClassSelector(SelectorBase): __slots__ = ['class_', 'is_instance'] - _slot_defaults = _dict_update(SelectorBase._slot_defaults, instantiate=True, is_instance=True) + _slot_defaults = dict(SelectorBase._slot_defaults, instantiate=True, is_instance=True) @typing.overload def __init__( @@ -2297,7 +2296,7 @@ class DataFrame(ClassSelector): __slots__ = ['rows', 'columns', 'ordered'] - _slot_defaults = _dict_update( + _slot_defaults = dict( ClassSelector._slot_defaults, rows=None, columns=None, ordered=None ) @@ -2413,7 +2412,7 @@ class Series(ClassSelector): __slots__ = ['rows'] - _slot_defaults = _dict_update( + _slot_defaults = dict( ClassSelector._slot_defaults, rows=None, allow_None=False ) @@ -2476,7 +2475,7 @@ class List(Parameter): __slots__ = ['bounds', 'item_type', 'class_'] - _slot_defaults = _dict_update( + _slot_defaults = dict( Parameter._slot_defaults, class_=None, item_type=None, bounds=(0, None), instantiate=True, default=[], ) @@ -2715,7 +2714,7 @@ class Path(Parameter): __slots__ = ['search_paths', 'check_exists'] - _slot_defaults = _dict_update( + _slot_defaults = dict( Parameter._slot_defaults, check_exists=True, ) @@ -2877,7 +2876,7 @@ class Color(Parameter): __slots__ = ['allow_named'] - _slot_defaults = _dict_update(Parameter._slot_defaults, allow_named=True) + _slot_defaults = dict(Parameter._slot_defaults, allow_named=True) @typing.overload def __init__( @@ -2939,7 +2938,7 @@ class Bytes(Parameter): __slots__ = ['regex'] - _slot_defaults = _dict_update( + _slot_defaults = dict( Parameter._slot_defaults, default=b"", regex=None, allow_None=False, ) diff --git a/tests/testparameterizedobject.py b/tests/testparameterizedobject.py index f9b5ea76..e7127156 100644 --- a/tests/testparameterizedobject.py +++ b/tests/testparameterizedobject.py @@ -17,7 +17,6 @@ import random from param import parameterized, Parameter -from param._utils import _dict_update from param.parameterized import ( ParamOverrides, Undefined, @@ -1357,7 +1356,7 @@ class CustomParameter(Parameter): __slots__ = ['container'] - _slot_defaults = _dict_update(Parameter._slot_defaults, container=None) + _slot_defaults = dict(Parameter._slot_defaults, container=None) def __init__(self, default=Undefined, *, container=Undefined, **kwargs): super().__init__(default=default, **kwargs)