Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Update for Pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Nov 19, 2023
1 parent abb75a4 commit f43e2c5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v0.5.0 (in development)
-----------------------
- Migrated from setuptools to hatch
- Update for Pydantic v2

v0.4.0 (2022-10-29)
-------------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies = [
"click >= 8.0",
"click-loglevel ~= 0.5",
"linesep ~= 0.4",
"pydantic ~= 1.7",
"pydantic ~= 2.0",
]

[project.scripts]
Expand Down
4 changes: 2 additions & 2 deletions src/gamdam/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async def readfile(fp: TextIO) -> AsyncIterator[Downloadable]:
async with aclosing(aiter(afp)) as lineiter: # type: ignore[type-var]
async for line in lineiter:
try:
dl = Downloadable.parse_raw(line)
dl = Downloadable.model_validate_json(line)
except ValueError:
log.exception("Invalid input line: %r; discarding", line)
else:
Expand Down Expand Up @@ -203,7 +203,7 @@ async def write_failures(
async with receiver:
async for r in receiver:
if not r.success:
await afp.write(r.downloadable.json() + "\n")
await afp.write(r.downloadable.model_dump_json() + "\n")


if __name__ == "__main__":
Expand Down
7 changes: 4 additions & 3 deletions src/gamdam/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Dict, List, Optional, TypeVar
import anyio
from anyio.streams.text import TextReceiveStream, TextSendStream
from pydantic import AnyHttpUrl, BaseModel, validator
from pydantic import AnyHttpUrl, BaseModel, field_validator
from .aioutil import LineReceiveStream

if sys.version_info[:2] >= (3, 10):
Expand All @@ -37,8 +37,9 @@ class Downloadable(BaseModel):
metadata: Optional[Dict[str, List[str]]] = None
extra_urls: Optional[List[AnyHttpUrl]] = None

@validator("path")
def _no_abs_path(cls, v: Path) -> Path: # noqa: B902, U100
@field_validator("path")
@classmethod
def _no_abs_path(cls, v: Path) -> Path:
if v.is_absolute():
raise ValueError("Download target paths cannot be absolute")
return v
Expand Down
6 changes: 3 additions & 3 deletions test/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def runner(repo: Path, objects: AsyncIterator[Downloadable]) -> Report:
return await download(repo, objects, subscriber=sender)

with (DATA_DIR / infile).open() as fp:
items = [Downloadable.parse_raw(line) for line in fp]
items = [Downloadable.model_validate_json(line) for line in fp]
fp.seek(0)
report = anyio.run(runner, annex_path, readfile(fp))
assert report.downloaded == len(items)
Expand All @@ -102,7 +102,7 @@ def test_download_mixed(annex_path: Path) -> None:
with (DATA_DIR / "mixed-meta.jsonl").open() as fp:
for line in fp:
data = json.loads(line)
item = Downloadable.parse_obj(data["item"])
item = Downloadable.model_validate(data["item"])
items.append(item)
if data["success"]:
expected.successful[item.path] = item
Expand All @@ -129,6 +129,6 @@ async def runner(repo: Path, objects: AsyncIterator[Downloadable]) -> Report:
for k, v in (dl.metadata or {}).items():
assert md.get(k) == v
expected_urls = [dl.url] + (dl.extra_urls or [])
assert get_annex_urls(annex_path, dl.path) == expected_urls
assert get_annex_urls(annex_path, dl.path) == list(map(str, expected_urls))
for dl in expected.failed.values():
assert not (annex_path / dl.path).exists()
2 changes: 1 addition & 1 deletion test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_main_mixed_failures(caplog: pytest.LogCaptureFixture, tmp_path: Path) -
) in caplog.record_tuples
assert sorted(
(
Downloadable.parse_raw(line)
Downloadable.model_validate_json(line)
for line in (tmp_path / "failures.txt").read_text().splitlines()
),
key=attrgetter("path"),
Expand Down

0 comments on commit f43e2c5

Please sign in to comment.