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

fix: the other argument to RelatationDataContent.update(...) should be optional #1226

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 1 addition & 11 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@
})


# Copied from typeshed.
_KT = typing.TypeVar("_KT")
_VT_co = typing.TypeVar("_VT_co", covariant=True)


class _SupportsKeysAndGetItem(typing.Protocol[_KT, _VT_co]):
def keys(self) -> typing.Iterable[_KT]: ...
def __getitem__(self, __key: _KT) -> _VT_co: ...


logger = logging.getLogger(__name__)

MAX_LOG_LINE_LEN = 131071 # Max length of strings to pass to subshell.
Expand Down Expand Up @@ -1722,7 +1712,7 @@ def __getitem__(self, key: str) -> str:
self._validate_read()
return super().__getitem__(key)

def update(self, other: _SupportsKeysAndGetItem[str, str], **kwargs: str):
def update(self, other: typing.Any = (), /, **kwargs: str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL that typeshed doesn't even define dict.update

https://github.com/python/typeshed/blob/main/stdlib/builtins.pyi#L1029

So, all good here!

"""Update the data from dict/iterable other and the kwargs."""
super().update(other, **kwargs)

Expand Down
52 changes: 52 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,58 @@ def test_get_app_relation_data(self, harness: ops.testing.Harness[ops.CharmBase]
relation_id, harness.model.app) == harness.get_relation_data(
relation_id, local_app) == {'foo': 'bar'}

def test_update_app_relation_data_other_map(self, harness: ops.testing.Harness[ops.CharmBase]):
harness.set_leader(True)
harness.begin()
relation_id = harness.add_relation('db1', 'remote')
harness.add_relation_unit(relation_id, 'remote/0')
with harness._event_context('foo_event'):
harness.update_relation_data(
relation_id,
harness.model.app.name,
{'foo': 'bar'})
rel = harness.model.get_relation('db1', relation_id)
assert rel is not None
rel.data[harness.model.app].update({'foo': 'baz'})
assert harness.get_relation_data(
relation_id, harness.model.app) == harness.get_relation_data(
relation_id, harness.model.app.name) == {'foo': 'baz'}

def test_update_app_relation_data_other_iter(
self, harness: ops.testing.Harness[ops.CharmBase]):
harness.set_leader(True)
harness.begin()
relation_id = harness.add_relation('db1', 'remote')
harness.add_relation_unit(relation_id, 'remote/0')
with harness._event_context('foo_event'):
harness.update_relation_data(
relation_id,
harness.model.app.name,
{'foo': 'bar'})
rel = harness.model.get_relation('db1', relation_id)
assert rel is not None
rel.data[harness.model.app].update((('foo', 'baz'), ))
assert harness.get_relation_data(
relation_id, harness.model.app) == harness.get_relation_data(
relation_id, harness.model.app.name) == {'foo': 'baz'}

def test_update_app_relation_data_kwarg(self, harness: ops.testing.Harness[ops.CharmBase]):
harness.set_leader(True)
harness.begin()
relation_id = harness.add_relation('db1', 'remote')
harness.add_relation_unit(relation_id, 'remote/0')
with harness._event_context('foo_event'):
harness.update_relation_data(
relation_id,
harness.model.app.name,
{'foo': 'bar'})
rel = harness.model.get_relation('db1', relation_id)
assert rel is not None
rel.data[harness.model.app].update(foo='baz')
tonyandrewmeyer marked this conversation as resolved.
Show resolved Hide resolved
assert harness.get_relation_data(
relation_id, harness.model.app) == harness.get_relation_data(
relation_id, harness.model.app.name) == {'foo': 'baz'}

def test_unit_relation_data(self, harness: ops.testing.Harness[ops.CharmBase]):
relation_id = harness.add_relation('db1', 'remoteapp1')
harness.add_relation_unit(relation_id, 'remoteapp1/0')
Expand Down
Loading