Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add .venv/ to .gitignore * Allow typecheckers to infer Json inner type * Fix and improve mypy tests * Add type tests * Add Json[Any] case to schema test * Update example in docs * Add changes file * Use <3.9 compatible annotations for tests
- Loading branch information
Showing
9 changed files
with
86 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.idea/ | ||
env/ | ||
venv/ | ||
.venv/ | ||
env3*/ | ||
Pipfile | ||
*.lock | ||
|
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,3 @@ | ||
Allow type checkers to infer inner type of `Json` type. `Json[list[str]]` will be now inferred as `list[str]`. | ||
`Json[Any]` should be used instead of plain `Json`. | ||
Runtime behaviour is not changed. |
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,29 +1,29 @@ | ||
from typing import List | ||
from typing import Any, List | ||
|
||
from pydantic import BaseModel, Json, ValidationError | ||
|
||
|
||
class SimpleJsonModel(BaseModel): | ||
json_obj: Json | ||
class AnyJsonModel(BaseModel): | ||
json_obj: Json[Any] | ||
|
||
|
||
class ComplexJsonModel(BaseModel): | ||
class ConstrainedJsonModel(BaseModel): | ||
json_obj: Json[List[int]] | ||
|
||
|
||
print(SimpleJsonModel(json_obj='{"b": 1}')) | ||
print(ComplexJsonModel(json_obj='[1, 2, 3]')) | ||
print(AnyJsonModel(json_obj='{"b": 1}')) | ||
print(ConstrainedJsonModel(json_obj='[1, 2, 3]')) | ||
try: | ||
ComplexJsonModel(json_obj=12) | ||
ConstrainedJsonModel(json_obj=12) | ||
except ValidationError as e: | ||
print(e) | ||
|
||
try: | ||
ComplexJsonModel(json_obj='[a, b]') | ||
ConstrainedJsonModel(json_obj='[a, b]') | ||
except ValidationError as e: | ||
print(e) | ||
|
||
try: | ||
ComplexJsonModel(json_obj='["a", "b"]') | ||
ConstrainedJsonModel(json_obj='["a", "b"]') | ||
except ValidationError as e: | ||
print(e) |
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 +1,2 @@ | ||
20: error: Unsupported operand types for + ("int" and "str") [operator] | ||
22: error: Unsupported operand types for + ("int" and "str") [operator] | ||
23: error: Unsupported operand types for + ("int" and "str") [operator] |
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