Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support accessing reserved word resource properties via attribute #1178

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stripe/_stripe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def __getattr__(self, k):
raise AttributeError(k)

try:
if k in self._field_remappings:
k = self._field_remappings[k]
return self[k]
except KeyError as err:
raise AttributeError(*err.args)
Expand Down Expand Up @@ -530,6 +532,8 @@ def __deepcopy__(self, memo: Dict[int, Any]) -> "StripeObject":

return copied

_field_remappings: ClassVar[Dict[str, str]] = {}

_inner_class_types: ClassVar[Dict[str, Type["StripeObject"]]] = {}
_inner_class_dicts: ClassVar[List[str]] = []

Expand Down
7 changes: 7 additions & 0 deletions tests/test_stripe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,10 @@ def serialize(self, previous):
)

assert obj.serialize(None) == {"nested": ""}

def test_field_name_remapping(self):
class Foo(stripe.stripe_object.StripeObject):
_field_remappings = {"getter_name": "data_name"}

obj = Foo.construct_from({"data_name": "foo"}, "mykey")
assert obj.getter_name == "foo"