Skip to content

Commit

Permalink
refactor: Remove _dict_update (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro authored Nov 22, 2024
1 parent 6c28e65 commit 1868240
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion doc/upgrade_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
11 changes: 1 addition & 10 deletions param/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
Skip,
_deprecated,
_deprecate_positional_args,
_dict_update,
_in_ipython,
_is_auto_name,
_is_mutable_container,
Expand Down Expand Up @@ -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__(
Expand Down
39 changes: 19 additions & 20 deletions param/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
ParamDeprecationWarning as _ParamDeprecationWarning,
_deprecate_positional_args,
_deprecated,
_dict_update,
_validate_error_prefix,
_deserialize_from_path,
_named_objs,
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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):
Expand All @@ -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__(
Expand All @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand All @@ -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
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1998,7 +1997,7 @@ class FileSelector(Selector):
"""
__slots__ = ['path']

_slot_defaults = _dict_update(
_slot_defaults = dict(
Selector._slot_defaults, path="",
)

Expand Down Expand Up @@ -2108,7 +2107,7 @@ class MultiFileSelector(ListSelector):
"""
__slots__ = ['path']

_slot_defaults = _dict_update(
_slot_defaults = dict(
Selector._slot_defaults, path="",
)

Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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
)

Expand Down Expand Up @@ -2413,7 +2412,7 @@ class Series(ClassSelector):

__slots__ = ['rows']

_slot_defaults = _dict_update(
_slot_defaults = dict(
ClassSelector._slot_defaults, rows=None, allow_None=False
)

Expand Down Expand Up @@ -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=[],
)
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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,
)

Expand Down
3 changes: 1 addition & 2 deletions tests/testparameterizedobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import random

from param import parameterized, Parameter
from param._utils import _dict_update
from param.parameterized import (
ParamOverrides,
Undefined,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1868240

Please sign in to comment.