You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pyserde's strict type check is overhauled by beartype - O(1)
runtime type checker. all pyserde classes now implements beartype
decorator, which runs automatic validations by default.
If you want to disable type check
```python
from serde import serde, disabled
@serde(type_check=disabled)
class Foo:
v: int
```
Closes#237, #347
Copy file name to clipboardExpand all lines: docs/en/decorators.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,11 +91,13 @@ class Wrapper(External):
91
91
pyserde supports forward references. If you replace a nested class name with with string, pyserde looks up and evaluate the decorator after nested class is defined.
92
92
93
93
```python
94
+
from__future__import annotations # make sure to import annotations
95
+
94
96
@dataclass
95
97
classFoo:
96
98
i: int
97
99
s: str
98
-
bar: 'Bar'#Specify type annotation in string.
100
+
bar: Bar #Bar can be specified although it's declared afterward.
Copy file name to clipboardExpand all lines: docs/en/type-check.md
+46-33Lines changed: 46 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,59 @@
1
1
# Type Checking
2
2
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.
3
+
pyserde offers runtime type checking since v0.9. It was completely reworked at v0.14 using [beartype](https://github.com/beartype/beartype) and it became more sophisticated and reliable. It is highly recommended to enable type checking always as it helps writing type-safe and robust programs.
4
4
5
-
### `NoCheck`
5
+
##`strict`
6
6
7
-
This is the default behavior until pyserde v0.8.3 and v0.9.x. No type coercion or checks are run. Even if a user puts a wrong value, pyserde doesn't complain anything.
7
+
Strict type checking is to check every field value against the declared type during (de)serialization and object construction. This is the default type check mode since v0.14. What will happen with this mode is if you declare a class with `@serde` decorator without any class attributes, `@serde(type_check=strict)` is assumed and strict type checking is enabled.
8
8
9
9
```python
10
10
@serde
11
-
@dataclass
12
11
class Foo
13
12
s: str
13
+
```
14
14
15
+
If you call `Foo` with wrong type of object,
16
+
```python
15
17
foo = Foo(10)
16
-
# pyserde doesn't complain anything. {"s": 10} will be printed.
18
+
```
19
+
20
+
you get an error
21
+
```python
22
+
beartype.roar.BeartypeCallHintParamViolation: Method __main__.Foo.__init__() parameter s=10 violates type hint <class'str'>, asint10not instance of str.
23
+
```
24
+
25
+
> **NOTE:** beartype exception instead of SerdeError is raised from constructor because beartype does not provide post validation hook as of Feb. 2024.
26
+
27
+
similarly, if you call (de)serialize APIs with wrong type of object,
28
+
29
+
```python
17
30
print(to_json(foo))
18
31
```
19
32
20
-
### `Coerce`
33
+
again you get an error
34
+
35
+
```python
36
+
serde.compat.SerdeError: Method __main__.Foo.__init__() parameter s=10 violates type hint <class'str'>, asint10not instance of str.
37
+
```
38
+
39
+
> **NOTE:** There are several caveats regarding type checks by beartype.
40
+
>
41
+
> 1. beartype can not validate on mutated properties
42
+
>
43
+
> The following code mutates the property "s" at the bottom. beartype can not detect this case.
44
+
> ```python
45
+
>@serde
46
+
>class Foo
47
+
> s: str
48
+
>
49
+
> f = Foo("foo")
50
+
> f.s =100
51
+
>```
52
+
>
53
+
>2. beartype can not validate every one of elements in containers. This isnot a bug. This is desgin principle of beartype. See [Does beartype actually do anything?](https://beartype.readthedocs.io/en/latest/faq/#faq-o1].
54
+
>```
55
+
56
+
## `coerce`
21
57
22
58
Type coercing automatically converts a value into the declared type during (de)serialization. If the value is incompatible e.g. value is"foo"andtypeisint, pyserde raises an `SerdeError`.
23
59
@@ -33,40 +69,17 @@ foo = Foo(10)
33
69
print(to_json(foo))
34
70
```
35
71
36
-
### `Strict`
72
+
## `disabled`
37
73
38
-
Strict type checking is to check every value against the declared type during (de)serialization. We plan to make `Strict` a default type checker in the future release.
74
+
This is the default behavior until pyserde v0.8.3 and v0.9.x. Notypecoercion or checks are run. Even if a user puts a wrong value, pyserde doesn't complain anything.
39
75
40
76
```python
41
-
@serde(type_check=Strict)
77
+
@serde
42
78
@dataclass
43
79
class Foo
44
80
s: str
45
81
46
82
foo= Foo(10)
47
-
# pyserde checks the value 10 is instance of `str`.
48
-
# SerdeError will be raised in this case because of the type mismatch.
83
+
# pyserde doesn't complain anything. {"s": 10} will be printed.
49
84
print(to_json(foo))
50
85
```
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).
0 commit comments