Skip to content

Commit

Permalink
Add bytes as possible field type
Browse files Browse the repository at this point in the history
  • Loading branch information
simw committed Nov 13, 2023
1 parent 812dc81 commit f55733c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pydantic_to_pyarrow"
version = "0.1"
version = "0.1.1"
description = "Conversion from pydantic models to pyarrow schemas"
authors = ["Simon Wicks <[email protected]>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion src/pydantic_to_pyarrow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .schema import SchemaCreationError, get_pyarrow_schema

__version__ = "0.1"
__version__ = "0.1.1"

__all__ = [
"__version__",
Expand Down
Empty file.
1 change: 1 addition & 0 deletions src/pydantic_to_pyarrow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SchemaCreationError(Exception):

FIELD_MAP = {
str: pa.string(),
bytes: pa.binary(),
bool: pa.bool_(),
float: pa.float64(),
datetime.date: pa.date32(),
Expand Down
9 changes: 7 additions & 2 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
NaiveDatetime,
PositiveInt,
StrictBool,
StrictBytes,
StrictFloat,
StrictInt,
StrictStr,
Expand Down Expand Up @@ -51,20 +52,22 @@ class SimpleModel(BaseModel):
b: bool
c: int
d: float
e: bytes

expected = pa.schema(
[
pa.field("a", pa.string(), nullable=False),
pa.field("b", pa.bool_(), nullable=False),
pa.field("c", pa.int64(), nullable=False),
pa.field("d", pa.float64(), nullable=False),
pa.field("e", pa.binary(), nullable=False),
]
)

actual = get_pyarrow_schema(SimpleModel)
assert actual == expected

objs = [{"a": "a", "b": True, "c": 1, "d": 1.01}]
objs = [{"a": "a", "b": True, "c": 1, "d": 1.01, "e": b"e"}]
new_schema, new_objs = _write_pq_and_read(objs, expected)
assert new_schema == expected
assert new_objs == objs
Expand All @@ -76,20 +79,22 @@ class SimpleModel(BaseModel):
b: StrictBool
c: StrictInt
d: StrictFloat
e: StrictBytes

expected = pa.schema(
[
pa.field("a", pa.string(), nullable=False),
pa.field("b", pa.bool_(), nullable=False),
pa.field("c", pa.int64(), nullable=False),
pa.field("d", pa.float64(), nullable=False),
pa.field("e", pa.binary(), nullable=False),
]
)

actual = get_pyarrow_schema(SimpleModel)
assert actual == expected

objs = [{"a": "a", "b": True, "c": 1, "d": 1.01}]
objs = [{"a": "a", "b": True, "c": 1, "d": 1.01, "e": b"e"}]
new_schema, new_objs = _write_pq_and_read(objs, expected)
assert new_schema == expected
assert new_objs == objs
Expand Down

0 comments on commit f55733c

Please sign in to comment.