Skip to content

Commit

Permalink
fix bug on unknown field
Browse files Browse the repository at this point in the history
Summary:
When a user gives a bad field name, it exposes a bug in `Struct.__init__`.
```
File "thrift.python/types.pyx", line 1118, in thrift.python.types.Struct.__call__
TypeError: 'dict_keys' object is not subscriptable
```

Instead of subscripting keys, just format all the bad keys as a list.

Reviewed By: yoney

Differential Revision: D67347012

fbshipit-source-id: 1bddf24505d0602a824535785c46e9f143defac3
  • Loading branch information
ahilger authored and facebook-github-bot committed Dec 18, 2024
1 parent 3f284b6 commit 0a81d40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion third-party/thrift/src/thrift/lib/python/test/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
to_thrift_set,
)

from thrift.python.serializer import deserialize, serialize_iobuf
from thrift.python.types import isset, update_nested_field

ListT = TypeVar("ListT")
Expand Down Expand Up @@ -304,6 +303,14 @@ def test_call_replace(self) -> None:
self.assertIsNone(x.name)
self.assertIsNotNone(x.an_int)

def test_call_bad_key(self) -> None:
x = self.easy()

err_type = AttributeError if self.is_mutable_run else TypeError
with self.assertRaisesRegex(err_type, "'easy' object .* attribute.* 'bad_key'"):
# pyre-ignore[28]: bad key for test
x = x(bad_key="foo")

def test_reserved(self) -> None:
x = self.Reserved(
from_="hello",
Expand Down
4 changes: 2 additions & 2 deletions third-party/thrift/src/thrift/lib/python/types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1114,8 +1114,8 @@ cdef class Struct(StructOrUnion):
set_struct_field(new_inst._fbthrift_data, field_index, value_to_copy)
if kwargs:
raise TypeError(
f"{type(self)}: error updating copy with unknown field: "
f"'{kwargs.keys()[0]}'"
f"'{type(self).__name__}' object does not have attribute(s): "
f"'{', '.join(kwargs.keys())}'"
)
new_inst._fbthrift_populate_primitive_fields()
return new_inst
Expand Down

0 comments on commit 0a81d40

Please sign in to comment.