Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
322 additions
and
103 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
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
Empty file.
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,25 @@ | ||
from typeit.compat import PY_VERSION | ||
|
||
if PY_VERSION >= (3, 7): | ||
from dataclasses import dataclass | ||
from typeit import parser as p | ||
|
||
|
||
def test_dataclasses(): | ||
|
||
@dataclass | ||
class InventoryItem: | ||
name: str | ||
unit_price: float | ||
quantity_on_hand: int = 0 | ||
|
||
mk_inv, dict_inv = p.type_constructor(InventoryItem) | ||
|
||
serialized = { | ||
'name': 'test', | ||
'unit_price': 1.0, | ||
'quantity_on_hand': 5, | ||
} | ||
x = mk_inv(serialized) | ||
assert isinstance(x, InventoryItem) | ||
assert dict_inv(x) == serialized |
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,51 @@ | ||
from typing import NamedTuple, Optional | ||
|
||
from money.currency import Currency | ||
from money.money import Money | ||
|
||
import typeit | ||
from typeit import schema, parser as p | ||
|
||
|
||
def test_extending(): | ||
class X(NamedTuple): | ||
x: Money | ||
|
||
class MoneySchema(schema.types.Tuple): | ||
def deserialize(self, node, cstruct): | ||
r = super().deserialize(node, cstruct) | ||
if r in (schema.types.Null, None): | ||
return r | ||
try: | ||
currency = Currency(r[0]) | ||
except ValueError: | ||
raise typeit.Invalid(node, f'Invalid currency token in {r}', cstruct) | ||
|
||
try: | ||
rv = Money(r[1], currency) | ||
except: | ||
raise typeit.Invalid(node, f'Invalid amount in {r}', cstruct) | ||
|
||
return rv | ||
|
||
def serialize(self, node, appstruct: Optional[Money]): | ||
if appstruct is None or appstruct is schema.types.Null: | ||
# if appstruct is None or appstruct is schema.types.Null: | ||
return appstruct | ||
|
||
r = (appstruct.currency, appstruct.amount) | ||
return super().serialize(node, r) | ||
|
||
mk_x, dict_x = ( | ||
p.type_constructor | ||
& MoneySchema[Money] << schema.types.Enum(Currency) << schema.primitives.NonStrictStr() | ||
^ X | ||
) | ||
|
||
serialized = { | ||
'x': ('GBP', '10') | ||
} | ||
|
||
x = mk_x(serialized) | ||
assert isinstance(x.x, Money) | ||
assert dict_x(x) == serialized |
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,18 @@ | ||
from money.money import Money | ||
|
||
from typeit import parser as p | ||
from typeit import flags | ||
|
||
|
||
def test_regular_classes(): | ||
|
||
mk_x, dict_x = p.type_constructor & flags.NON_STRICT_PRIMITIVES ^ Money | ||
|
||
serialized = { | ||
'amount': '10', | ||
'currency': 'GBP', | ||
} | ||
|
||
x = mk_x(serialized) | ||
assert isinstance(x, Money) | ||
assert dict_x(x) == serialized |
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 |
---|---|---|
|
@@ -125,5 +125,3 @@ class Right: ... | |
|
||
assert x.errmsg == 'Error' | ||
assert y.payload == {'success': True} | ||
|
||
|
Oops, something went wrong.