Add convenience functions to TypedStruct
Opened this issue · 0 comments
idanarye commented
Assuming:
class Foo(TypedStruct):
a = int
a.default = 1
b = str
b.default = 'two'
We want the following functions:
- Setting a field to
DEFAULT
should use the default as defined in the typed struct:assert Foo(a=DEFAULT, b='four') == Foo(a=1, b='four') foo = Foo(a=3, b='four') foo.b = DEFAULT assert foo == Foo(a=3, b='two')
.copy()
:Current behavior:foo = Foo(a=3, b='four') assert foo.copy() == foo assert foo.copy() is not foo
foo.copy()
returns adict
..update()
:Current behavior:foo = Foo(a=3, b='four') foo.update(a=5) assert foo == Foo(a=5, b='four') with pytest.raises(): foo.update(a='six')
update
isdict.update
which bypasses the verifications..but_with()
:foo = Foo(a=3, b='four') assert foo.but_with(a=5) == Foo(a=5, b='four')
- Setting a field to
AUTO
should leave it unchanged:This will be useful withfoo = Foo(a=3, b='four') foo.b = AUTO assert foo == Foo(a=3, b='four')
update()
andbut_with()
:def make_foo(a=AUTO, b=AUTO): foo = Foo(a=3, b='four') foo.update(a=a, b=b) return foo assert make_foo() == Foo(a=3, b='four') assert make_foo(a=5) == Foo(a=5, b='four') assert make_foo(b='six') == Foo(a=3, b='six')