Skip to content

Commit f798e73

Browse files
authored
refactor: prefer super().__setattr__ over __dict__ manipulation (#174)
Using super().__setattr__ is a preferred idiom over directly manipulating an object's __dict__. This allows for a more seamless transition to a named tuple subclass, or __slots__.
1 parent 5d92c26 commit f798e73

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

packages/proto-plus/proto/message.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def wrap(cls, pb):
300300
"""
301301
# Optimized fast path.
302302
instance = cls.__new__(cls)
303-
instance.__dict__["_pb"] = pb
303+
super(cls, instance).__setattr__("_pb", pb)
304304
return instance
305305

306306
def serialize(cls, instance) -> bytes:
@@ -414,7 +414,7 @@ def __init__(self, mapping=None, *, ignore_unknown_fields=False, **kwargs):
414414
if mapping is None:
415415
if not kwargs:
416416
# Special fast path for empty construction.
417-
self.__dict__["_pb"] = self._meta.pb()
417+
super().__setattr__("_pb", self._meta.pb())
418418
return
419419

420420
mapping = kwargs
@@ -430,7 +430,7 @@ def __init__(self, mapping=None, *, ignore_unknown_fields=False, **kwargs):
430430
if kwargs:
431431
mapping.MergeFrom(self._meta.pb(**kwargs))
432432

433-
self.__dict__["_pb"] = mapping
433+
super().__setattr__("_pb", mapping)
434434
return
435435
elif isinstance(mapping, type(self)):
436436
# Just use the above logic on mapping's underlying pb.
@@ -468,7 +468,7 @@ def __init__(self, mapping=None, *, ignore_unknown_fields=False, **kwargs):
468468
params[key] = pb_value
469469

470470
# Create the internal protocol buffer.
471-
self.__dict__["_pb"] = self._meta.pb(**params)
471+
super().__setattr__("_pb", self._meta.pb(**params))
472472

473473
def __bool__(self):
474474
"""Return True if any field is truthy, False otherwise."""

0 commit comments

Comments
 (0)