I’m trying to use model validator, to display a warning if two fields have certain value. So the code looks like this:
import logging
log = logging.getLogger(__name__)
class MyModel(BaseModel):
a: Int
b: Int
@model_validator(mode="before")
@classmethod
def check_values(cls,data):
if (data.get("a") == 2 and data.get("b") == 2):
log.warning("both values set to 2")
print("both values set to 2")
my_model = MyModel(2,2)
When i debug the code, i see that the condition is met and bof print and log instructions are reached.
However, i can’t see the messages in stdin/err.
Is there a way to use validator to display/log those messages?
1
The issue likely lies in the configuration of the logging module. By default, Python’s logging module does not output messages to the console unless a handler is set up to do so. Additionally, the default logging level may prevent the warning from being shown.
# Set up logging
logging.basicConfig(level=logging.WARNING) # Ensures WARNING messages are shown
log = logging.getLogger(__name__)
1