Skip to content

Commit f0dfa03

Browse files
committed
fix: simplify pydantic version checks and improve model validation
1 parent 0dd3bb0 commit f0dfa03

File tree

3 files changed

+35
-100
lines changed

3 files changed

+35
-100
lines changed

src/beans_logging/_base.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
import yaml
1010
from loguru import logger
1111
from loguru._logger import Logger
12-
import pydantic
13-
14-
if "2.0.0" <= pydantic.__version__:
15-
from pydantic import validate_call
16-
else:
17-
from pydantic import validate_arguments as validate_call
12+
from pydantic import validate_call
1813

1914
# Internal modules
2015
from ._utils import create_dir, deep_merge
@@ -171,11 +166,7 @@ def update_config(self, config: LoggerConfigPM | dict[str, Any]):
171166
"""
172167

173168
if isinstance(config, dict):
174-
if "2.0.0" <= pydantic.__version__:
175-
_config_dict = self.config.model_dump()
176-
else:
177-
_config_dict = self.config.dict()
178-
169+
_config_dict = self.config.model_dump()
179170
_merged_dict = deep_merge(_config_dict, config)
180171
try:
181172
self.config = LoggerConfigPM(**_merged_dict)
@@ -224,10 +215,7 @@ def _load_config_file(self):
224215
return
225216

226217
_new_config_dict = _new_config_dict["logger"]
227-
if "2.0.0" <= pydantic.__version__:
228-
_config_dict = self.config.model_dump()
229-
else:
230-
_config_dict = self.config.dict()
218+
_config_dict = self.config.model_dump()
231219

232220
_merged_dict = deep_merge(_config_dict, _new_config_dict)
233221
self.config = LoggerConfigPM(**_merged_dict)
@@ -247,10 +235,7 @@ def _load_config_file(self):
247235
return
248236

249237
_new_config_dict = _new_config_dict["logger"]
250-
if "2.0.0" <= pydantic.__version__:
251-
_config_dict = self.config.model_dump()
252-
else:
253-
_config_dict = self.config.dict()
238+
_config_dict = self.config.model_dump()
254239

255240
_merged_dict = deep_merge(_config_dict, _new_config_dict)
256241
self.config = LoggerConfigPM(**_merged_dict)
@@ -274,10 +259,7 @@ def _load_config_file(self):
274259
# return
275260

276261
# _new_config_dict = _new_config_dict["logger"]
277-
# if "2.0.0" <= pydantic.__version__:
278-
# _config_dict = self.config.model_dump()
279-
# else:
280-
# _config_dict = self.config.dict()
262+
# _config_dict = self.config.model_dump()
281263

282264
# _merged_dict = deep_merge(_config_dict, _new_config_dict)
283265
# self.config = LoggerConfigPM(**_merged_dict)

src/beans_logging/_utils.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
import errno
55

66
from loguru import logger
7-
import pydantic
8-
9-
if "2.0.0" <= pydantic.__version__:
10-
from pydantic import validate_call
11-
else:
12-
from pydantic import validate_arguments as validate_call
7+
from pydantic import validate_call
138

149
from ._consts import WarnEnum
1510

src/beans_logging/schemas.py

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import datetime
22

3-
4-
import pydantic
5-
from pydantic import BaseModel, constr, Field
6-
7-
if "2.0.0" <= pydantic.__version__:
8-
from pydantic import field_validator, model_validator, ConfigDict
9-
else:
10-
from pydantic import validator, root_validator
11-
3+
from pydantic import (
4+
BaseModel,
5+
constr,
6+
Field,
7+
field_validator,
8+
model_validator,
9+
ConfigDict,
10+
)
1211

1312
from ._consts import LogLevelEnum
1413
from ._utils import get_default_logs_dir, get_app_name
1514

1615

1716
class ExtraBaseModel(BaseModel):
18-
if "2.0.0" <= pydantic.__version__:
19-
model_config = ConfigDict(extra="allow")
20-
else:
21-
22-
class Config:
23-
extra = "allow"
17+
model_config = ConfigDict(extra="allow")
2418

2519

2620
class StdHandlerPM(ExtraBaseModel):
@@ -55,26 +49,13 @@ class LogHandlersPM(ExtraBaseModel):
5549
default="{app_name}.std.err.log", min_length=4, max_length=1023
5650
)
5751

58-
if "2.0.0" <= pydantic.__version__:
59-
60-
@model_validator(mode="after")
61-
def _check_log_path(self) -> "LogHandlersPM":
62-
if self.log_path == self.err_path:
63-
raise ValueError(
64-
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
65-
)
66-
return self
67-
68-
else:
69-
70-
@root_validator
71-
def _check_log_path(cls, values):
72-
_log_path, _err_path = values.get("log_path"), values.get("err_path")
73-
if _log_path == _err_path:
74-
raise ValueError(
75-
f"`log_path` and `err_path` attributes are same: '{_log_path}', must be different!"
76-
)
77-
return values
52+
@model_validator(mode="after")
53+
def _check_log_path(self) -> "LogHandlersPM":
54+
if self.log_path == self.err_path:
55+
raise ValueError(
56+
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
57+
)
58+
return self
7859

7960

8061
class JsonHandlersPM(ExtraBaseModel):
@@ -87,26 +68,13 @@ class JsonHandlersPM(ExtraBaseModel):
8768
default="{app_name}.json.err.log", min_length=4, max_length=1023
8869
)
8970

90-
if "2.0.0" <= pydantic.__version__:
91-
92-
@model_validator(mode="after")
93-
def _check_log_path(self) -> "JsonHandlersPM":
94-
if self.log_path == self.err_path:
95-
raise ValueError(
96-
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
97-
)
98-
return self
99-
100-
else:
101-
102-
@root_validator
103-
def _check_log_path(cls, values):
104-
_log_path, _err_path = values.get("log_path"), values.get("err_path")
105-
if _log_path == _err_path:
106-
raise ValueError(
107-
f"`log_path` and `err_path` attributes are same: '{_log_path}', must be different!"
108-
)
109-
return values
71+
@model_validator(mode="after")
72+
def _check_log_path(self) -> "JsonHandlersPM":
73+
if self.log_path == self.err_path:
74+
raise ValueError(
75+
f"`log_path` and `err_path` attributes are same: '{self.log_path}', must be different!"
76+
)
77+
return self
11078

11179

11280
class FilePM(ExtraBaseModel):
@@ -124,22 +92,12 @@ class FilePM(ExtraBaseModel):
12492
log_handlers: LogHandlersPM = Field(default_factory=LogHandlersPM)
12593
json_handlers: JsonHandlersPM = Field(default_factory=JsonHandlersPM)
12694

127-
if "2.0.0" <= pydantic.__version__:
128-
129-
@field_validator("rotate_time", mode="before")
130-
@classmethod
131-
def _check_rotate_time(cls, val):
132-
if isinstance(val, str):
133-
val = datetime.time.fromisoformat(val)
134-
return val
135-
136-
else:
137-
138-
@validator("rotate_time", pre=True, always=True)
139-
def _check_rotate_time(cls, val):
140-
if val and isinstance(val, str):
141-
val = datetime.time.fromisoformat(val)
142-
return val
95+
@field_validator("rotate_time", mode="before")
96+
@classmethod
97+
def _check_rotate_time(cls, val):
98+
if isinstance(val, str):
99+
val = datetime.time.fromisoformat(val)
100+
return val
143101

144102

145103
class AutoLoadPM(ExtraBaseModel):

0 commit comments

Comments
 (0)