Unverified Commit 0454e4bd authored by Sylvain Gugger's avatar Sylvain Gugger Committed by GitHub
Browse files

Fix ModelOutput instantiation form dictionaries (#13067)

* Fix ModelOutput instantiation form dictionaries

* Style
parent 3157fa3c
......@@ -1850,11 +1850,15 @@ class ModelOutput(OrderedDict):
other_fields_are_none = all(getattr(self, field.name) is None for field in class_fields[1:])
if other_fields_are_none and not is_tensor(first_field):
try:
iterator = iter(first_field)
if isinstance(first_field, dict):
iterator = first_field.items()
first_field_iterator = True
except TypeError:
first_field_iterator = False
else:
try:
iterator = iter(first_field)
first_field_iterator = True
except TypeError:
first_field_iterator = False
# if we provided an iterator as first field and the iterator is a (key, value) iterator
# set the associated fields
......
......@@ -101,3 +101,9 @@ class ModelOutputTester(unittest.TestCase):
x["a"] = 10
self.assertEqual(x.a, 10)
self.assertEqual(x["a"], 10)
def test_instantiate_from_dict(self):
x = ModelOutputTest({"a": 30, "b": 10})
self.assertEqual(list(x.keys()), ["a", "b"])
self.assertEqual(x.a, 30)
self.assertEqual(x.b, 10)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment