pytorch/caffe2/python/task_test.py
Kunal Bhalla 4564444c91 [RFC][caffe2] TaskGroup.__repr__ shouldn't have side effects
Summary: `__repr__` calling self.tasks() ends up marking the instance as "used", which doesn't seem appropriate. I was debugging a value being passed around and then ran into `Cannot add Task to an already used TaskGroup.` because the value had been logged once.

Test Plan:
Added a unit test -- didn't see a clean public method to test it, but I'm happy to add one if that makes sense.

Will wait for sandcastle to trigger everything else; I'm not at all familiar with this code so any other recommendations would be great!

Reviewed By: cryptopic

Differential Revision: D23541198

fbshipit-source-id: 5d1ec674a1ddaedf113140133b90e0da6afa7270
2020-10-01 14:21:03 -07:00

25 lines
870 B
Python

import unittest
from caffe2.python import task
class TestTask(unittest.TestCase):
def testRepr(self):
cases = [
(task.Cluster(), "Cluster(nodes=[], node_kwargs={})"),
(task.Node(), "Node(name=local, kwargs={})"),
(
task.TaskGroup(),
"TaskGroup(tasks=[], workspace_type=None, remote_nets=[])",
),
(task.TaskOutput([]), "TaskOutput(names=[], values=None)"),
(task.Task(), "Task(name=local/task, node=local, outputs=[])"),
(task.SetupNets(), "SetupNets(init_nets=None, exit_nets=None)"),
]
for obj, want in cases:
self.assertEqual(obj.__repr__(), want)
def testEffectlessRepr(self):
task_group = task.TaskGroup()
_repr = task_group.__repr__()
self.assertFalse(task_group._already_used)