-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement strict type checking
- Loading branch information
Showing
22 changed files
with
622 additions
and
189 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Type Checking | ||
|
||
`pyserde` provides 3 kinds of type checks. | ||
|
||
### `NoCheck` | ||
|
||
This is the default behavior until pyserde v0.8.3. No type coercion or checks are applied. If a user puts a wrong value, pyserde doesn't complain anything. | ||
|
||
```python | ||
@serde | ||
@dataclass | ||
class Foo | ||
s: str | ||
|
||
foo = Foo(10) | ||
# pyserde doesn't complain anything. {"s": 10} will be printed. | ||
print(to_json(foo, type_check=NoCheck)) | ||
|
||
``` | ||
|
||
### `Coerce` | ||
|
||
Type coercion is not yet implemented but this will be the default behavior in pyserde v0.9.0 onward. | ||
|
||
```python | ||
foo = Foo(10) | ||
# pyserde automatically coerce the int value 10 into "10". | ||
# {"s": "10"} will be printed. | ||
print(to_json(foo, type_check=Coerce)) | ||
``` | ||
|
||
Not yet implemented. | ||
|
||
### `Strict` | ||
|
||
Strict type checking is to check every value against the declared type. | ||
|
||
```python | ||
foo = Foo(10) | ||
# pyserde checks the value 10 is instance of `str`. | ||
# SerdeError will be raised in this case because of the type mismatch. | ||
print(to_json(foo, type_check=Strict)) | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from dataclasses import dataclass | ||
from typing import Dict, List | ||
|
||
from serde import SerdeError, Strict, serde | ||
from serde.json import from_json, to_json | ||
|
||
|
||
@serde | ||
@dataclass | ||
class Foo: | ||
a: int | ||
b: List[int] | ||
c: List[Dict[str, int]] | ||
|
||
|
||
def main(): | ||
f = Foo(a=1.0, b=[1.0], c=[{"k": 1.0}]) | ||
try: | ||
print(f"Into Json: {to_json(f, type_check=Strict)}") | ||
except Exception as e: | ||
print(e) | ||
|
||
s = '{"a": 1, "b": [1], "c": [{"k": 1.0}]}' | ||
try: | ||
print(f"From Json: {from_json(Foo, s, type_check=Strict)}") | ||
except Exception as e: | ||
print(e) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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
Oops, something went wrong.