Skip to content

Commit

Permalink
change to white-list of overridable attributes in Namedlist._set_name (
Browse files Browse the repository at this point in the history
…#338)

* change to white-list of overridable attributes in Namedlist._set_name

* fmt

* remove lambda in Namedlist.__init__ to fix pickling issue

* reformat (after remove lambda in Namedlist.__init__ to fix pickling)

Co-authored-by: Johannes Köster <johannes.koester@uni-due.de>
  • Loading branch information
twlee79 and johanneskoester committed May 19, 2020
1 parent 9954cc9 commit 2cc2a9b
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions snakemake/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,12 @@ def __init__(
list.__init__(self)
self._names = dict()

# white-list of attribute names that can be overridden in _set_name
# default to throwing exception if called to prevent use as functions
self._allowed_overrides = ["index", "sort"]
for name in self._allowed_overrides:
setattr(self, name, functools.partial(self._used_attribute, _name=name))

if toclone:
if custom_map is not None:
self.extend(map(custom_map, toclone))
Expand All @@ -1205,6 +1211,20 @@ def __init__(
self.append(item)
self._add_name(key)

@staticmethod
def _used_attribute(*args, _name, **kwargs):
"""
Generic function that throws an `AttributeError`.
Used as replacement for functions such as `index()` and `sort()`,
which may be overridden by workflows, to signal to a user that
these functions should not be used.
"""
raise AttributeError(
f"{_name}() cannot be used; attribute name reserved"
f" for use in some existing workflows"
)

def _add_name(self, name):
"""
Add a name to the last item.
Expand All @@ -1222,10 +1242,10 @@ def _set_name(self, name, index, end=None):
name -- a name
index -- the item index
"""
if name == "items" or name == "keys" or name == "get":
if name not in self._allowed_overrides and hasattr(self.__class__, name):
raise AttributeError(
"invalid name for input, output, wildcard, "
"params or log: 'items', 'keys', and 'get' are reserved for internal use"
f"invalid name for input, output, wildcard, "
f"params or log: {name} is reserved for internal use"
)

self._names[name] = (index, end)
Expand Down

0 comments on commit 2cc2a9b

Please sign in to comment.