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

dedup dcbase.replace #44

Merged
merged 5 commits into from
Jul 20, 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "ops-scenario"

version = "4.0.1"
version = "4.0.2"

authors = [
{ name = "Pietro Pasotti", email = "[email protected]" }
Expand Down
4 changes: 3 additions & 1 deletion scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ class StateValidationError(RuntimeError):
@dataclasses.dataclass(frozen=True)
class _DCBase:
def replace(self, *args, **kwargs):
return dataclasses.replace(self, *args, **kwargs)
"""Produce a deep copy of this class, with some arguments replaced with new ones."""
return dataclasses.replace(self.copy(), *args, **kwargs)

def copy(self) -> "Self":
"""Produce a deep copy of this object."""
return copy.deepcopy(self)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def test_relation_without_endpoint():

assert_consistent(
State(
relations=[Relation("foo", relation_id=1), Relation("bar", relation_id=1)]
relations=[Relation("foo", relation_id=1), Relation("bar", relation_id=2)]
),
Event("start"),
_CharmSpec(
Expand Down
41 changes: 41 additions & 0 deletions tests/test_dcbase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import dataclasses
from typing import Dict, List

from scenario.state import _DCBase


@dataclasses.dataclass(frozen=True)
class Foo(_DCBase):
a: int
b: List[int]
c: Dict[int, List[int]]


def test_base_case():
l = [1, 2]
l1 = [1, 2, 3]
d = {1: l1}
f = Foo(1, l, d)
g = f.replace(a=2)

assert g.a == 2
assert g.b == l
assert g.c == d
assert g.c[1] == l1


def test_dedup_on_replace():
l = [1, 2]
l1 = [1, 2, 3]
d = {1: l1}
f = Foo(1, l, d)
g = f.replace(a=2)

l.append(3)
l1.append(4)
d[2] = "foobar"

assert g.a == 2
assert g.b == [1, 2]
assert g.c == {1: [1, 2, 3]}
assert g.c[1] == [1, 2, 3]