Skip to content

Commit

Permalink
Update get-upload-stats.py for Pydantic 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Dec 4, 2023
1 parent e4bce86 commit e6b50b0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
27 changes: 15 additions & 12 deletions tools/get-upload-stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python3
from __future__ import annotations

from collections import Counter
from collections.abc import Iterator
from datetime import datetime, time, timezone
Expand All @@ -8,13 +10,14 @@
from pathlib import Path, PurePosixPath
import re
import subprocess
from typing import Any, TextIO
from typing import Annotated, Any, TextIO

import click
from datalad.support.annexrepo import AnnexRepo
from dateutil.parser import parse
from humanize import naturalsize
from pydantic import BaseModel
from pydantic.functional_serializers import PlainSerializer
from ruamel.yaml import YAML # type: ignore[attr-defined]

log = logging.getLogger("get-upload-stats")
Expand Down Expand Up @@ -51,20 +54,20 @@ class CommitInfo(BaseModel):
created: datetime


class MetadataSummary(BaseModel):
specimens: set[str] | None
species: set[str] | None
modalities: set[str] | None
techniques: set[str] | None
anatomies: set[str]
SortedSet = Annotated[set[str], PlainSerializer(lambda s: sorted(s))]

class Config:
json_encoders = {set: sorted}

class MetadataSummary(BaseModel):
specimens: SortedSet | None
species: SortedSet | None
modalities: SortedSet | None
techniques: SortedSet | None
anatomies: SortedSet

@classmethod
def from_metadata(
cls, dandiset_metadata: dict, asset_metadata: list[dict] | None
) -> "MetadataSummary":
) -> MetadataSummary:
specimens: set[str] | None
if asset_metadata is not None:
specimens = {sp for am in asset_metadata for sp in cls.get_specimens(am)}
Expand Down Expand Up @@ -144,7 +147,7 @@ def cmp(one: set[str] | None, two: set[str] | None) -> SetAddedRemoved | None:
return cls(
**{
k: cmp(getattr(first, k), getattr(second, k))
for k in cls.__fields__.keys()
for k in cls.model_fields.keys()
}
)

Expand Down Expand Up @@ -572,7 +575,7 @@ def main(
since_latest=since_latest,
)
if fmt == "json":
print(report.json(indent=4), file=outfile)
print(report.model_dump_json(indent=4), file=outfile)
elif fmt == "yaml":
yaml = YAML(typ="safe")
yaml.default_flow_style = False
Expand Down
2 changes: 1 addition & 1 deletion tools/get-upload-stats.req.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
click >= 8.0
datalad
humanize
pydantic ~= 1.7
pydantic ~= 2.0
python-dateutil ~= 2.0
ruamel.yaml ~= 0.15
1 change: 0 additions & 1 deletion tools/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ ignore_missing_imports = True

[pydantic-mypy]
init_forbid_extra = True
warn_untypes_fields = True

0 comments on commit e6b50b0

Please sign in to comment.