Skip to content

Commit

Permalink
Fix ModelOutput instantiation when there is only one tuple (#20416)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgugger authored Nov 23, 2022
1 parent 993a187 commit afce73b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/transformers/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,20 @@ def __post_init__(self):
# if we provided an iterator as first field and the iterator is a (key, value) iterator
# set the associated fields
if first_field_iterator:
for element in iterator:
for idx, element in enumerate(iterator):
if (
not isinstance(element, (list, tuple))
or not len(element) == 2
or not isinstance(element[0], str)
):
if idx == 0:
# If we do not have an iterator of key/values, set it as attribute
self[class_fields[0].name] = first_field
else:
# If we have a mixed iterator, raise an error
raise ValueError(
f"Cannot set key/value for {element}. It needs to be a tuple (key, value)."
)
break
setattr(self, element[0], element[1])
if element[1] is not None:
Expand Down
13 changes: 13 additions & 0 deletions tests/utils/test_model_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,16 @@ def test_instantiate_from_dict(self):
self.assertEqual(list(x.keys()), ["a", "b"])
self.assertEqual(x.a, 30)
self.assertEqual(x.b, 10)

def test_instantiate_from_iterator(self):
x = ModelOutputTest([("a", 30), ("b", 10)])
self.assertEqual(list(x.keys()), ["a", "b"])
self.assertEqual(x.a, 30)
self.assertEqual(x.b, 10)

with self.assertRaises(ValueError):
_ = ModelOutputTest([("a", 30), (10, 10)])

x = ModelOutputTest(a=(30, 30))
self.assertEqual(list(x.keys()), ["a"])
self.assertEqual(x.a, (30, 30))

0 comments on commit afce73b

Please sign in to comment.