Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit b480c9e

Browse files
committed
fix mypy
1 parent 2fbdbf8 commit b480c9e

5 files changed

Lines changed: 54 additions & 48 deletions

File tree

tests/test_schemas.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -186,44 +186,51 @@ def test_schema_datetime_serialization():
186186
assert data["check_out"] is None
187187

188188

189-
#
190-
#
191-
# def test_schema_decimal_serialization():
192-
# inventory_item = typesystem.Schema(fields={
193-
# "name": typesystem.String(),
194-
# "price": typesystem.Decimal(precision="0.01", allow_null=True),
195-
# })
196-
#
197-
# item = {"name": "Example", "price": 123.45}
198-
#
199-
# assert item.price ==
200-
# assert item["price"] == 123.45
201-
#
202-
# item = InventoryItem(name="test")
203-
# assert dict(item) == {"name": "test", "price": None}
204-
# item = InventoryItem(name="test", price=0)
205-
# assert dict(item) == {"name": "test", "price": 0}
206-
#
207-
#
208-
# def test_schema_uuid_serialization():
209-
# class User(typesystem.Schema):
210-
# id = typesystem.String(format="uuid")
211-
# username = typesystem.String()
212-
#
213-
# item = User(id="b769df4a-18ec-480f-89ef-8ea961a82269", username="tom")
214-
#
215-
# assert item.id == uuid.UUID("b769df4a-18ec-480f-89ef-8ea961a82269")
216-
# assert item["id"] == "b769df4a-18ec-480f-89ef-8ea961a82269"
217-
#
218-
#
219-
# def test_schema_with_callable_default():
220-
# class Example(typesystem.Schema):
221-
# created = typesystem.Date(default=datetime.date.today)
222-
#
223-
# value, error = Example.validate_or_error({})
224-
# assert value.created == datetime.date.today()
225-
#
226-
#
189+
def test_schema_decimal_serialization():
190+
inventory = typesystem.Schema(
191+
fields={
192+
"name": typesystem.String(),
193+
"price": typesystem.Decimal(precision="0.01", allow_null=True),
194+
}
195+
)
196+
197+
item = {"name": "example", "price": 123.45}
198+
data = inventory.serialize(item)
199+
200+
assert data["name"] == "example"
201+
assert data["price"] == 123.45
202+
203+
item = {"name": "example", "price": None}
204+
assert inventory.serialize(item) == {"name": "example", "price": None}
205+
206+
item = {"name": "example", "price": 0}
207+
assert inventory.serialize(item) == {"name": "example", "price": 0}
208+
209+
210+
def test_schema_uuid_serialization():
211+
user = typesystem.Schema(
212+
fields={
213+
"id": typesystem.String(format="uuid"),
214+
"username": typesystem.String(),
215+
}
216+
)
217+
218+
item = {"id": "b769df4a-18ec-480f-89ef-8ea961a82269", "username": "tom"}
219+
data = user.serialize(item)
220+
221+
assert data["id"] == "b769df4a-18ec-480f-89ef-8ea961a82269"
222+
assert data["username"] == "tom"
223+
224+
225+
def test_schema_with_callable_default():
226+
schema = typesystem.Schema(
227+
fields={"created": typesystem.Date(default=datetime.date.today)}
228+
)
229+
230+
value, _ = schema.validate_or_error({})
231+
assert value["created"] == datetime.date.today()
232+
233+
227234
# def test_nested_schema():
228235
# class Artist(typesystem.Schema):
229236
# name = typesystem.String(max_length=100)

typesystem/forms.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,29 @@ def __init__(
3434
self,
3535
*,
3636
env: "jinja2.Environment",
37-
schema: typing.Type[Schema],
37+
schema: Schema,
3838
instance: typing.Any = None,
3939
) -> None:
4040
self.env = env
4141
self.schema = schema
4242
self.instance = instance
4343
self.values = None if instance is None else self.schema.serialize(instance)
44-
self.errors = None
44+
self.errors: typing.Optional[typing.Dict[str, typing.Any]] = None
4545
self._validate_called = False
4646

47-
def validate(self, data: dict = None):
47+
def validate(self, data: dict = None) -> None:
4848
assert not self._validate_called, "validate() has already been called."
4949
self.data = data
5050
self.values, self.errors = self.schema.validate_or_error(data)
5151
self._validate_called = True
5252

5353
@property
54-
def is_valid(self):
54+
def is_valid(self) -> bool:
5555
assert self._validate_called, "validate() has not been called."
5656
return self.errors is None
5757

5858
@property
59-
def validated_data(self):
59+
def validated_data(self) -> typing.Any:
6060
return self.values
6161

6262
def render_fields(self) -> str:
@@ -156,7 +156,5 @@ def load_template_env(
156156
)
157157
return jinja2.Environment(loader=loader, autoescape=True)
158158

159-
def create_form(
160-
self, schema: typing.Type[Schema], instance: typing.Any = None
161-
) -> Form: # type: ignore
159+
def create_form(self, schema: Schema, instance: typing.Any = None) -> Form:
162160
return Form(env=self.env, schema=schema, instance=instance)

typesystem/json_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ def to_json_schema(
408408
elif isinstance(arg, NeverMatch):
409409
return False
410410

411+
field: typing.Optional[Field]
411412
data: dict = {}
412413
is_root = _definitions is None
413414
definitions = {} if _definitions is None else _definitions

typesystem/tokenize/positional_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
def validate_with_positions(
10-
*, token: Token, validator: typing.Union[Field, typing.Type[Schema]]
10+
*, token: Token, validator: typing.Union[Field, Schema]
1111
) -> typing.Any:
1212
try:
1313
return validator.validate(token.value)

typesystem/tokenize/tokenize_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def tokenize_json(content: typing.Union[str, bytes]) -> Token:
182182

183183
def validate_json(
184184
content: typing.Union[str, bytes],
185-
validator: typing.Union[Field, typing.Type[Schema]],
185+
validator: typing.Union[Field, Schema],
186186
) -> typing.Any:
187187
"""
188188
Parse and validate a JSON string, returning positionally marked error

0 commit comments

Comments
 (0)