generated from tophat/new-project-kit
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: default serialization for custom objects (#101)
* wip: custom repr example * wip: provide sensible default serialization for custom objects * chore: update changelog * refactor: perform check on class * refactor: one less line * cr: indent keys * fix: support callable and limit repr to public * cr: skip methods when serializing custom objects
- Loading branch information
Showing
7 changed files
with
112 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
# name: test_snapshot_attr_class | ||
MyCustomReprClass(prop1=1, prop2='a', prop3={1, 2, 3}) | ||
--- | ||
# name: test_snapshot_smart_class | ||
MyCustomSmartReprClass( | ||
# name: test_snapshot_custom_class | ||
<class 'MyCustomClass'> { | ||
prop1=1 | ||
prop2=a | ||
prop3={1, 2, 3} | ||
prop2='a' | ||
prop3=<class 'set'> { | ||
1, | ||
2, | ||
3, | ||
} | ||
} | ||
--- | ||
# name: test_snapshot_custom_repr_class | ||
MyCustomReprClass( | ||
prop1=1, | ||
prop2='a', | ||
prop3={1, 2, 3}, | ||
) | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
import attr | ||
|
||
|
||
@attr.s | ||
class MyCustomReprClass: | ||
prop1 = attr.ib(default=1) | ||
prop2 = attr.ib(default="a") | ||
prop3 = attr.ib(default={1, 2, 3}) | ||
|
||
|
||
def test_snapshot_attr_class(snapshot): | ||
assert MyCustomReprClass() == snapshot | ||
|
||
|
||
class MyCustomSmartReprClass: | ||
class MyCustomClass: | ||
prop1 = 1 | ||
prop2 = "a" | ||
prop3 = {1, 2, 3} | ||
|
||
|
||
def test_snapshot_custom_class(snapshot): | ||
assert MyCustomClass() == snapshot | ||
|
||
|
||
class MyCustomReprClass(MyCustomClass): | ||
def __repr__(self): | ||
state = "\n".join( | ||
f" {a}={getattr(self, a)}" | ||
f" {a}={repr(getattr(self, a))}," | ||
for a in sorted(dir(self)) | ||
if not a.startswith("__") | ||
if not a.startswith("_") | ||
) | ||
return f"{self.__class__.__name__}(\n{state}\n)" | ||
|
||
|
||
def test_snapshot_smart_class(snapshot): | ||
assert MyCustomSmartReprClass() == snapshot | ||
def test_snapshot_custom_repr_class(snapshot): | ||
assert MyCustomReprClass() == snapshot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters