Skip to content

Commit

Permalink
Remove Pydantic dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Jan 4, 2024
1 parent 27b55cb commit d36de14
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
5 changes: 0 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ boto3 = "^1.24"
zarr = "^2.12"
click = "^8.1.3"
tqdm = "^4.64.1"
pydantic = "^2.0"

# Optional
boto3-stubs = {version = "^1.24", optional = true}
Expand Down Expand Up @@ -96,14 +95,10 @@ strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
plugins = ["pydantic.mypy"]

[[tool.mypy.overrides]]
module = [
# <https://github.com/zarr-developers/zarr-python/issues/1566>
"zarr.*",
]
ignore_missing_imports = true

[tool.pydantic-mypy]
init_forbid_extra = true
23 changes: 11 additions & 12 deletions zarr_checksum/checksum.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import annotations

from dataclasses import asdict, dataclass, field
from functools import total_ordering
import hashlib
from json import dumps
import re
from typing import List

import pydantic

ZARR_DIGEST_PATTERN = "([0-9a-f]{32})-([0-9]+)--([0-9]+)"

Expand All @@ -15,7 +13,8 @@ class InvalidZarrChecksum(Exception):
pass


class ZarrDirectoryDigest(pydantic.BaseModel):
@dataclass
class ZarrDirectoryDigest:
"""The data that can be serialized to / deserialized from a checksum string."""

md5: str
Expand All @@ -32,7 +31,7 @@ def parse(cls, checksum: str | None) -> ZarrDirectoryDigest:
raise InvalidZarrChecksum()

md5, count, size = match.groups()
return cls(md5=md5, count=count, size=size)
return cls(md5=md5, count=int(count), size=int(size))

def __str__(self) -> str:
return self.digest
Expand All @@ -43,7 +42,8 @@ def digest(self) -> str:


@total_ordering
class ZarrChecksum(pydantic.BaseModel):
@dataclass
class ZarrChecksum:
"""
A checksum for a single file/directory in a zarr file.
Expand All @@ -63,15 +63,16 @@ def __lt__(self, other: ZarrChecksum) -> bool:
return self.name < other.name


class ZarrChecksumManifest(pydantic.BaseModel):
@dataclass
class ZarrChecksumManifest:
"""
A set of file and directory checksums.
This is the data hashed to calculate the checksum of a directory.
"""

directories: List[ZarrChecksum] = pydantic.Field(default_factory=list)
files: List[ZarrChecksum] = pydantic.Field(default_factory=list)
directories: list[ZarrChecksum] = field(default_factory=list)
files: list[ZarrChecksum] = field(default_factory=list)

@property
def is_empty(self) -> bool:
Expand All @@ -94,9 +95,7 @@ def generate_digest(self) -> ZarrDirectoryDigest:
)

# Serialize json without any spacing
# Pydantic's model_dump_json() doesn't support specifying separators,
# so we have to serialize via the json module instead.
json = dumps(self.model_dump(mode="json"), separators=(",", ":"))
json = dumps(asdict(self), separators=(",", ":"))

# Generate digest
md5 = hashlib.md5(json.encode("utf-8")).hexdigest()
Expand Down

0 comments on commit d36de14

Please sign in to comment.