Skip to content

Commit 9b574d8

Browse files
committed
Caching doesn't work that way. Refactor with copy arg in get_type_map
1 parent 020d2f4 commit 9b574d8

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/pynwb/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,28 @@ def __get_resources() -> dict:
101101
@docval({'name': 'extensions', 'type': (str, TypeMap, list),
102102
'doc': 'a path to a namespace, a TypeMap, or a list consisting of paths to namespaces and TypeMaps',
103103
'default': None},
104+
{
105+
'name': 'copy', 'type': bool,
106+
'doc': 'Whether to return a deepcopy of the TypeMap. '
107+
'If False, a direct reference may be returned (use with caution).',
108+
'default': True
109+
},
104110
returns="TypeMap loaded for the given extension or NWB core namespace", rtype=tuple,
105111
is_method=False)
106112
def get_type_map(**kwargs):
107113
'''
108114
Get the TypeMap for the given extensions. If no extensions are provided,
109115
return the TypeMap for the core namespace
110116
'''
111-
extensions = getargs('extensions', kwargs)
117+
extensions, copy_map = getargs('extensions', 'copy', kwargs)
112118
type_map = None
113119
if extensions is None:
114-
type_map = deepcopy(__TYPE_MAP)
120+
if copy_map:
121+
type_map = deepcopy(__TYPE_MAP)
122+
else:
123+
type_map = __TYPE_MAP
115124
else:
116-
warnings.warn("The 'extensions' argument is deprecated and will be removed in HDMF 5.0", DeprecationWarning)
125+
warn("The 'extensions' argument is deprecated and will be removed in HDMF 5.0", DeprecationWarning)
117126
if isinstance(extensions, TypeMap):
118127
type_map = extensions
119128
else:

src/pynwb/core.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class NWBMixin(AbstractContainer):
3232

3333
__nwbfields__ = tuple()
3434

35-
__type_map = None
36-
3735
@docval({'name': 'neurodata_type', 'type': str, 'doc': 'the data_type to search for', 'default': None})
3836
def get_ancestor(self, **kwargs):
3937
"""
@@ -63,11 +61,7 @@ def _error_on_new_pass_on_construct(self, error_msg: str):
6361
raise ValueError(error_msg)
6462

6563
def _get_type_map(self):
66-
# Cache the type map to avoid repeated calls to get_type_map()
67-
if self.__type_map is None:
68-
self.__type_map = get_type_map()
69-
70-
return self.__type_map
64+
return get_type_map(copy=False)
7165

7266
@property
7367
def data_type(self):

0 commit comments

Comments
 (0)