-
Notifications
You must be signed in to change notification settings - Fork 1
fix: rename _get_handlers to get_default_handlers and improve handler… #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |||
| from .schemas import ExtraBaseModel, LogHandlerPM, LoguruHandlerPM | ||||
|
|
||||
|
|
||||
| def _get_handlers() -> dict[str, LogHandlerPM]: | ||||
| def get_default_handlers() -> dict[str, LogHandlerPM]: | ||||
| """Get default log handlers. | ||||
|
|
||||
| Returns: | ||||
|
|
@@ -131,36 +131,42 @@ class LoggerConfigPM(ExtraBaseModel): | |||
| ) | ||||
| default: DefaultConfigPM = Field(default_factory=DefaultConfigPM) | ||||
| intercept: InterceptConfigPM = Field(default_factory=InterceptConfigPM) | ||||
| handlers: dict[str, LogHandlerPM] = Field(default_factory=_get_handlers) | ||||
| handlers: dict[str, LogHandlerPM] = Field(default_factory=get_default_handlers) | ||||
| extra: ExtraConfigPM | None = Field(default_factory=ExtraConfigPM) | ||||
|
|
||||
| @field_validator("handlers", mode="before") | ||||
| @classmethod | ||||
| def _check_handlers(cls, val: Any) -> Any: | ||||
| if val: | ||||
| if not isinstance(val, dict): | ||||
| def _check_handlers(cls, val: Any) -> dict[str, LogHandlerPM]: | ||||
| _default_handlers = get_default_handlers() | ||||
|
|
||||
| if not val: | ||||
| return _default_handlers | ||||
|
|
||||
| if not isinstance(val, dict): | ||||
| raise TypeError( | ||||
| f"'handlers' attribute type {type(val).__name__} is invalid, must be a dict of <LogHandlerPM>, " | ||||
| f"<LoguruHandlerPM> or dict!" | ||||
| ) | ||||
|
|
||||
| for _i, _handler in val.items(): | ||||
| if not isinstance(_handler, (LogHandlerPM, LoguruHandlerPM, dict)): | ||||
| raise TypeError( | ||||
| f"'handlers' attribute type {type(val).__name__} is invalid, must be a dict of <LogHandlerPM>, " | ||||
| f"<LoguruHandlerPM> or dict!" | ||||
| f"'handlers' attribute index {_i} type {type(_handler).__name__} is invalid, must be " | ||||
| f"<LogHandlerPM>, <LoguruHandlerPM> or dict!" | ||||
| ) | ||||
|
|
||||
| for _i, _handler in val.items(): | ||||
| if not isinstance(_handler, (LogHandlerPM, LoguruHandlerPM, dict)): | ||||
| raise TypeError( | ||||
| f"'handlers' attribute index {_i} type {type(_handler).__name__} is invalid, must be " | ||||
| f"<LogHandlerPM>, <LoguruHandlerPM> or dict!" | ||||
| ) | ||||
|
|
||||
| if isinstance(_handler, LoguruHandlerPM): | ||||
| val[_i] = LogHandlerPM( | ||||
| **_handler.model_dump(exclude_none=True, exclude_unset=True) | ||||
| ) | ||||
| elif isinstance(_handler, dict): | ||||
| val[_i] = LogHandlerPM(**_handler) | ||||
| if isinstance(_handler, LoguruHandlerPM): | ||||
| val[_i] = LogHandlerPM( | ||||
| **_handler.model_dump(exclude_none=True, exclude_unset=True) | ||||
| ) | ||||
| elif isinstance(_handler, dict): | ||||
| val[_i] = LogHandlerPM(**_handler) | ||||
|
|
||||
| val = {**_default_handlers, **val} | ||||
|
||||
| return val | ||||
|
Comment on lines
+139
to
166
|
||||
|
|
||||
|
|
||||
| __all__ = [ | ||||
| "LoggerConfigPM", | ||||
| "get_default_handlers", | ||||
|
||||
| "get_default_handlers", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the validator using mode="before", when a user doesn't provide handlers, the validator will receive None or an undefined value (not the result of default_factory), check "if not val" at line 142, and return _default_handlers. This means the default_factory=get_default_handlers on line 134 will never actually be invoked, making it redundant.
In Pydantic v2, validators with mode="before" execute before default values are applied, so the default_factory is bypassed when the validator returns a value for a missing field.
Consider either:
The current implementation works correctly but has unnecessary redundancy.