Skip to content

Commit

Permalink
Refactor use of deepcopy by docval (#238)
Browse files Browse the repository at this point in the history
* Remove use of deepcopy by docval

* Use deepcopy on default value in case a complex obj is used

* Remove unused helper function

* Fix flake8 unused import

* Remove other unnecessary uses of deepcopy
  • Loading branch information
rly authored Jan 7, 2020
1 parent c4a53f9 commit db1b1ad
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/hdmf/build/map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from collections import OrderedDict
from copy import copy, deepcopy
from copy import copy
from datetime import datetime

from ..utils import docval, getargs, ExtenderMeta, get_docval, call_docval_func, fmt_docval_args
Expand Down Expand Up @@ -449,7 +449,7 @@ def __get_cls_dict(self, base, addl_fields, name=None, default_name=None):
fields = list()

# copy docval args from superclass
for arg in deepcopy(get_docval(base.__init__)):
for arg in get_docval(base.__init__):
existing_args.add(arg['name'])
if arg['name'] in addl_fields:
continue
Expand Down
4 changes: 2 additions & 2 deletions src/hdmf/spec/namespace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import OrderedDict
from datetime import datetime
from copy import deepcopy, copy
from copy import copy
import ruamel.yaml as yaml
import os.path
import string
Expand Down Expand Up @@ -36,7 +36,7 @@ class SpecNamespace(dict):

__types_key = 'data_types'

@docval(*deepcopy(_namespace_args))
@docval(*_namespace_args)
def __init__(self, **kwargs):
doc, full_name, name, version, date, author, contact, schema, catalog = \
popargs('doc', 'full_name', 'name', 'version', 'date', 'author', 'contact', 'schema', 'catalog', kwargs)
Expand Down
12 changes: 6 additions & 6 deletions src/hdmf/spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class BaseStorageSpec(Spec):
__type_key = 'data_type'
__id_key = 'object_id'

@docval(*deepcopy(_attrbl_args))
@docval(*_attrbl_args)
def __init__(self, **kwargs):
name, doc, parent, quantity, attributes, linkable, data_type_def, data_type_inc =\
getargs('name', 'doc', 'parent', 'quantity', 'attributes',
Expand Down Expand Up @@ -477,7 +477,7 @@ def quantity(self):
''' The number of times the object being specified should be present '''
return self.get('quantity', DEF_QUANTITY)

@docval(*deepcopy(_attr_args))
@docval(*_attr_args)
def add_attribute(self, **kwargs):
''' Add an attribute to this specification '''
pargs, pkwargs = fmt_docval_args(AttributeSpec.__init__, kwargs)
Expand Down Expand Up @@ -618,7 +618,7 @@ class DatasetSpec(BaseStorageSpec):
To specify a table-like dataset i.e. a compound data type.
'''

@docval(*deepcopy(_dataset_args))
@docval(*_dataset_args)
def __init__(self, **kwargs):
doc, shape, dims, dtype, default_value = popargs('doc', 'shape', 'dims', 'dtype', 'default_value', kwargs)
if shape is not None:
Expand Down Expand Up @@ -811,7 +811,7 @@ class GroupSpec(BaseStorageSpec):
''' Specification for groups
'''

@docval(*deepcopy(_group_args))
@docval(*_group_args)
def __init__(self, **kwargs):
doc, groups, datasets, links = popargs('doc', 'groups', 'datasets', 'links', kwargs)
self.__data_types = dict()
Expand Down Expand Up @@ -1095,7 +1095,7 @@ def links(self):
''' The links specificed in this GroupSpec '''
return tuple(self.get('links', tuple()))

@docval(*deepcopy(_group_args))
@docval(*_group_args)
def add_group(self, **kwargs):
''' Add a new specification for a subgroup to this group specification '''
doc = kwargs.pop('doc')
Expand Down Expand Up @@ -1127,7 +1127,7 @@ def get_group(self, **kwargs):
name = getargs('name', kwargs)
return self.__groups.get(name, self.__links.get(name))

@docval(*deepcopy(_dataset_args))
@docval(*_dataset_args)
def add_dataset(self, **kwargs):
''' Add a new specification for a dataset to this group specification '''
doc = kwargs.pop('doc')
Expand Down
21 changes: 4 additions & 17 deletions src/hdmf/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import copy as _copy
import itertools as _itertools
from abc import ABCMeta
import collections
import h5py
Expand Down Expand Up @@ -225,7 +224,7 @@ def __parse_args(validator, args, kwargs, enforce_type=True, enforce_shape=True,
ret[argname] = args[argsi]
argsi += 1
else:
ret[argname] = arg['default']
ret[argname] = _copy.deepcopy(arg['default'])
argval = ret[argname]
if enforce_type:
if not __type_okay(argval, arg['type'], arg['default'] is None):
Expand Down Expand Up @@ -265,17 +264,6 @@ def __parse_args(validator, args, kwargs, enforce_type=True, enforce_shape=True,
return {'args': ret, 'type_errors': type_errors, 'value_errors': value_errors}


def __sort_args(validator):
pos = list()
kw = list()
for arg in validator:
if "default" in arg:
kw.append(arg)
else:
pos.append(arg)
return list(_itertools.chain(pos, kw))


docval_idx_name = '__dv_idx__'
docval_attr_name = '__docval__'
__docval_args_loc = 'args'
Expand Down Expand Up @@ -411,7 +399,6 @@ def foo(self, **kwargs):
rtype = options.pop('rtype', None)
is_method = options.pop('is_method', True)
allow_extra = options.pop('allow_extra', False)
val_copy = __sort_args(_copy.deepcopy(validator))

def dec(func):
_docval = _copy.copy(options)
Expand All @@ -420,7 +407,7 @@ def dec(func):
func.__doc__ = _docval.get('doc', func.__doc__)
pos = list()
kw = list()
for a in val_copy:
for a in validator:
try:
a['type'] = __resolve_type(a['type'])
except Exception as e:
Expand All @@ -436,7 +423,7 @@ def dec(func):
def func_call(*args, **kwargs):
self = args[0]
parsed = __parse_args(
_copy.deepcopy(loc_val),
loc_val,
args[1:],
kwargs,
enforce_type=enforce_type,
Expand All @@ -453,7 +440,7 @@ def func_call(*args, **kwargs):
return func(self, **parsed['args'])
else:
def func_call(*args, **kwargs):
parsed = __parse_args(_copy.deepcopy(loc_val),
parsed = __parse_args(loc_val,
args,
kwargs,
enforce_type=enforce_type,
Expand Down

0 comments on commit db1b1ad

Please sign in to comment.