Skip to content

Commit faf6065

Browse files
authored
Merge pull request #256 from yukinarit/add-example
Add type coercing example
2 parents 9ae809e + 96c103e commit faf6065

File tree

7 files changed

+74
-18
lines changed

7 files changed

+74
-18
lines changed

bench/pyproject.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ click = "*"
1010
pyserde = {path = "../"}
1111

1212
[tool.poetry.dev-dependencies]
13-
dacite = "*"
14-
mashumaro = "^2.11.0"
15-
marshmallow = "~=3.2"
16-
attrs = "*"
17-
cattrs = "*"
18-
seaborn = "==0.9.0"
19-
matplotlib = "==3.1.1"
20-
numpy = "==1.17.2"
13+
# dacite = "*"
14+
# mashumaro = "^2.11.0"
15+
# marshmallow = "~=3.2"
16+
# attrs = "*"
17+
# cattrs = "*"
18+
# seaborn = "*"
19+
# matplotlib = "*"
20+
# numpy = "*"

docs/features/type-check.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Type Checking
22

3-
This is one of the most awaited features. `pyserde` v0.9 adds the experimental type checkers. As this feature is still experimental, the type checking is not perfect. Also, [@tbsexton](https://github.com/tbsexton) is looking into more beautiful solution, the entire backend of type checker may be replaced by [beartype](https://github.com/beartype/beartype) in the future.
3+
This is one of the most awaited features. `pyserde` v0.9 adds the experimental type checkers. As this feature is still experimental, the type checking is not perfect. Also, [@tbsexton](https://github.com/tbsexton) is looking into [more beautiful solution](https://github.com/yukinarit/pyserde/issues/237#issuecomment-1191714102), the entire backend of type checker may be replaced by [beartype](https://github.com/beartype/beartype) in the future.
44

55
### `NoCheck`
66

@@ -48,3 +48,25 @@ foo = Foo(10)
4848
# SerdeError will be raised in this case because of the type mismatch.
4949
print(to_json(foo))
5050
```
51+
52+
> **NOTE:** Since pyserde is a serialization framework, it provides type checks or coercing only during (de)serialization. For example, pyserde doesn't complain even if incompatible value is assigned in the object below.
53+
>
54+
> ```python
55+
> @serde(type_check=Strict)
56+
> @dataclass
57+
> class Foo
58+
> s: str
59+
>
60+
> f = Foo(100) # pyserde doesn't raise an error
61+
> ```
62+
>
63+
> If you want to detect runtime type errors, I recommend to use [beartype](https://github.com/beartype/beartype).
64+
> ```python
65+
> @beartype
66+
> @serde(type_check=Strict)
67+
> @dataclass
68+
> class Foo
69+
> s: str
70+
>
71+
> f = Foo(100) # beartype raises an error
72+
> ```

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Foo:
3535

3636
pyserde generates methods necessary for (de)serialization by `@serde` when a class is loaded into python interpreter. The code generation occurs only once and there is no overhead when you use the generated methods. Now your class is serializable and deserializable in the data formats supported by pyserde.
3737

38-
> **Note:** If you need only either serialization or deserialization functionality, you can use `@serialize` or `@deserialize` instead of `@serde` decorator.
38+
> **NOTE:** If you need only either serialization or deserialization functionality, you can use `@serialize` or `@deserialize` instead of `@serde` decorator.
3939
>
4040
> e.g. If you don't need deserialization functionality, you can add `@serialize` decorator only. But, calling deserialize API e.g. `from_json` for `Foo` will raise an error.
4141
> ```python

examples/runner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import simple
1919
import skip
2020
import tomlfile
21+
import type_check_coerce
2122
import type_check_strict
2223
import type_datetime
2324
import type_decimal
@@ -53,8 +54,9 @@ def run_all():
5354
run(generics)
5455
run(lazy_type_evaluation)
5556
run(literal)
56-
run(user_exception)
5757
run(type_check_strict)
58+
run(type_check_coerce)
59+
run(user_exception)
5860
if PY310:
5961
import union_operator
6062

examples/simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from dataclasses import dataclass
22

3-
from serde import Strict, serde
3+
from serde import serde
44
from serde.json import from_json, to_json
55

66

7-
@serde(type_check=Strict)
7+
@serde
88
@dataclass
99
class Foo:
1010
i: int

examples/type_check_coerce.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from dataclasses import dataclass
2+
from typing import Dict, List, Optional
3+
4+
from serde import Coerce, SerdeError, serde
5+
from serde.json import from_json, to_json
6+
7+
8+
@serde(type_check=Coerce)
9+
@dataclass
10+
class Bar:
11+
e: int
12+
13+
14+
@serde(type_check=Coerce)
15+
@dataclass
16+
class Foo:
17+
a: int
18+
b: List[int]
19+
c: List[Dict[str, int]]
20+
d: Optional[Bar] = None
21+
22+
23+
def main():
24+
f = Foo(a="1", b=[True], c=[{10: 1.0}])
25+
print(f"Into Json: {to_json(f)}")
26+
27+
s = '{"a": "1", "b": [false], "c": [{"10": 1.0}], "d": {"e": "100"}}'
28+
print(f"From Json: {from_json(Foo, s)}")
29+
30+
31+
if __name__ == '__main__':
32+
main()

examples/type_check_strict.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from serde.json import from_json, to_json
66

77

8-
@serde
8+
@serde(type_check=Strict)
99
@dataclass
1010
class Foo:
1111
a: int
@@ -16,14 +16,14 @@ class Foo:
1616
def main():
1717
f = Foo(a=1.0, b=[1.0], c=[{"k": 1.0}])
1818
try:
19-
print(f"Into Json: {to_json(f, type_check=Strict)}")
20-
except Exception as e:
19+
print(f"Into Json: {to_json(f)}")
20+
except SerdeError as e:
2121
print(e)
2222

2323
s = '{"a": 1, "b": [1], "c": [{"k": 1.0}]}'
2424
try:
25-
print(f"From Json: {from_json(Foo, s, type_check=Strict)}")
26-
except Exception as e:
25+
print(f"From Json: {from_json(Foo, s)}")
26+
except SerdeError as e:
2727
print(e)
2828

2929

0 commit comments

Comments
 (0)