Skip to content

Commit

Permalink
Support more kinds calls to instance_proxy.update
Browse files Browse the repository at this point in the history
This signature should be more like `dict.update`. This is also needed
for setuptools.
  • Loading branch information
tekknolagi committed Feb 21, 2024
1 parent c2979d3 commit b02630f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions library/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4343,10 +4343,13 @@ def copy(self):
_instance_guard(instance)
return {key: _instance_getattr(instance, key) for key in _object_keys(instance)}

def update(self, d):
def update(self, d=_Unbound, /, **kwargs):
instance = self._instance
_instance_guard(instance)
for key, value in d.items():
if d is not _Unbound:
for key, value in d.items():
_instance_setattr(instance, key, value)
for key, value in kwargs.items():
_instance_setattr(instance, key, value)

def get(self, key, default=None):
Expand Down
24 changes: 24 additions & 0 deletions library/builtins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6898,6 +6898,30 @@ def __init__(self):
instance.__dict__ = {"hello": "world"}
self.assertEqual(list(instance.__dict__.keys()), ["hello"])

def test_vars_update_with_no_args_does_not_raise(self):
class C:
pass

instance = C()
vars(instance).update()

def test_vars_update_with_only_kwargs_sets_attribute(self):
class C:
pass

instance = C()
vars(instance).update(hello=1)
self.assertEqual(instance.hello, 1)

def test_vars_update_with_args_and_kwargs_sets_attributes(self):
class C:
pass

instance = C()
vars(instance).update({"hello": 1}, world=2)
self.assertEqual(instance.hello, 1)
self.assertEqual(instance.world, 2)


class IsInstanceTests(unittest.TestCase):
def test_isinstance_with_same_types_returns_true(self):
Expand Down

0 comments on commit b02630f

Please sign in to comment.