Skip to content

Commit 883b761

Browse files
committed
Pydantic v2 doesn't convert floats into ints, so added a 'field_validator' to the 'File' BaseModel to do so
1 parent 417f766 commit 883b761

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/murfey/util/models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from __future__ import annotations
22

3+
import math
34
from datetime import datetime
45
from pathlib import Path
56
from typing import Any, Dict, List, Optional
67

7-
from pydantic import BaseModel
8+
from pydantic import BaseModel, field_validator
89

910
"""
1011
General Models
@@ -46,6 +47,15 @@ class File(BaseModel):
4647
timestamp: datetime
4748
full_path: str
4849

50+
@field_validator("size", mode="before")
51+
@classmethod
52+
def round_file_size_correctly(cls, v: Any) -> int:
53+
if isinstance(v, float):
54+
if v - math.floor(v) == 0.5:
55+
return math.ceil(v)
56+
return round(v)
57+
return v
58+
4959

5060
class ConnectionFileParameters(BaseModel):
5161
filename: str

0 commit comments

Comments
 (0)