Skip to content

Commit

Permalink
Merge pull request #280 from yukinarit/add-init-class-var-examples
Browse files Browse the repository at this point in the history
test: Add InitVar and ClassVar examples
  • Loading branch information
yukinarit committed Nov 24, 2022
2 parents 83286ce + f5171fa commit db792cf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/class_var.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from dataclasses import dataclass
from typing import ClassVar, Optional

from serde import serde
from serde.json import from_json, to_json


@serde
@dataclass
class Foo:
i: int
j: Optional[int] = None
k: ClassVar[int] = 100


def main():
f = Foo(i=10, j=20)
print(f"Into Json: {to_json(f)}")

s = '{"i": 10, "j": 20}'
print(f"From Json: {from_json(Foo, s)}")

print(f"Class variable: Foo.k={f.k}")


if __name__ == '__main__':
main()
25 changes: 25 additions & 0 deletions examples/init_var.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dataclasses import InitVar, dataclass
from typing import Optional

from serde import serde
from serde.json import from_json, to_json


@serde
@dataclass
class Foo:
i: int
j: Optional[int] = None
k: InitVar[Optional[int]] = None


def main():
f = Foo(i=10, j=20)
print(f"Into Json: {to_json(f)}")

s = '{"i": 10, "j": 20}'
print(f"From Json: {from_json(Foo, s)}")


if __name__ == '__main__':
main()
4 changes: 4 additions & 0 deletions examples/runner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys

import any
import class_var
import collection
import custom_class_serializer
import custom_field_serializer
Expand All @@ -11,6 +12,7 @@
import forward_reference
import generics
import generics_nested
import init_var
import jsonfile
import lazy_type_evaluation
import literal
Expand Down Expand Up @@ -63,6 +65,8 @@ def run_all():
run(user_exception)
run(pep681)
run(ellipsis)
run(init_var)
run(class_var)
if PY310:
import union_operator

Expand Down

0 comments on commit db792cf

Please sign in to comment.