# Buggy code
def copy(self):
    new_ta = type(self)()
    new_ta.tag_id = self.tag_id
    new_ta.user_tname = self.user_tname
    new_ta.value = self.value
    new_ta.user_value = self.user_value
    return new_ta


# Traceback (KeyError)
galaxy.tools.actions INFO 2017-11-10 17:17:20,507 Verified access to datasets for Job[unflushed,tool_id=toolshed.g2.bx.psu.edu/repos/lparsons/htseq_count/htseq_count/0.6.1galaxy3] (39.007 ms)
galaxy.tools.actions INFO 2017-11-10 17:17:20,511 Setup for job Job[unflushed,tool_id=toolshed.g2.bx.psu.edu/repos/lparsons/htseq_count/htseq_count/0.6.1galaxy3] complete, ready to flush (67.715 ms)
galaxy.tools.actions INFO 2017-11-10 17:17:20,608 Flushed transaction for job Job[id=2195981,tool_id=toolshed.g2.bx.psu.edu/repos/lparsons/htseq_count/htseq_count/0.6.1galaxy3] (96.495 ms)
galaxy.tools.execute DEBUG 2017-11-10 17:17:20,608 Tool [toolshed.g2.bx.psu.edu/repos/lparsons/htseq_count/htseq_count/0.6.1galaxy3] created job [2195981] (631.589 ms)
galaxy.tools.execute DEBUG 2017-11-10 17:17:20,629 Executed 2 job(s) for tool toolshed.g2.bx.psu.edu/repos/lparsons/htseq_count/htseq_count/0.6.1galaxy3 request: (1092.059 ms)
galaxy.managers.collections DEBUG 2017-11-10 17:17:20,662 Created collection with 2 elements
galaxy.managers.collections DEBUG 2017-11-10 17:17:20,812 Created collection with 2 elements
galaxy.workflow.run DEBUG 2017-11-10 17:17:21,068 Workflow step 137193 of invocation 29732 invoked (1794.676 ms)
galaxy.managers.collections DEBUG 2017-11-10 17:17:21,479 Created collection with 0 elements
galaxy.tools ERROR 2017-11-10 17:17:21,510 Exception caught while attempting tool execution:
Traceback (most recent call last):
File "/usr/local/galaxy/galaxy-dist/lib/galaxy/tools/__init__.py", line 1311, in handle_single_execution
job, out_data = self.execute(trans, incoming=params, history=history, rerun_remap_job_id=rerun_remap_job_id, mapping_over_collection=mapping_over_collection, execution_cache=execution_cache)
File "/usr/local/galaxy/galaxy-dist/lib/galaxy/tools/__init__.py", line 1391, in execute
return self.tool_action.execute(self, trans, incoming=incoming, set_output_hid=set_output_hid, history=history, **kwargs)
File "/usr/local/galaxy/galaxy-dist/lib/galaxy/tools/actions/__init__.py", line 410, in execute
**element_kwds
File "/usr/local/galaxy/galaxy-dist/lib/galaxy/tools/actions/__init__.py", line 735, in create_collection
**element_kwds
File "/usr/local/galaxy/galaxy-dist/lib/galaxy/managers/collections.py", line 107, in create
dataset_collection_instance.tags.append(tag.copy())
File "/usr/local/galaxy/galaxy-dist/.venv/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1073, in append
item = __set(self, item, _sa_initiator)
File "/usr/local/galaxy/galaxy-dist/.venv/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1045, in __set
item = executor.fire_append_event(item, _sa_initiator)
File "/usr/local/galaxy/galaxy-dist/.venv/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 717, in fire_append_event
item, initiator)
File "/usr/local/galaxy/galaxy-dist/.venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 941, in fire_append_event
initiator or self._append_token or self._init_append_token())
File "/usr/local/galaxy/galaxy-dist/.venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 1178, in emit_backref_from_collection_append_event
child_impl = child_state.manager[key].impl
KeyError: 'dataset_collections'

# Fixed code
def copy(self, cls=None):
    if cls:
        new_ta = cls()
    else:
        new_ta = type(self)()
    new_ta.tag_id = self.tag_id
    new_ta.user_tname = self.user_tname
    new_ta.value = self.value
    new_ta.user_value = self.user_value
    return new_ta