Weird Pydantic Errors

If your Pydantic object validation errors look like this, the reason is simple:

2 validation errors for DetailedDemand
ordqty
  __init__() takes exactly 3 positional arguments (2 given) (type=type_error)
Z
  __init__() takes exactly 3 positional arguments (2 given) (type=type_error)

You’ve raised ValidationErrors in the validator, rather than ValueErrors, TypeErrors or similar. Don’t raise ValidationErrors there, those are for collecting other errors.

Validating a DataFrame against a Pydantic model

This works:

valid_instances = []
validation_exceptions = [] 
for row in df_in.itertuples(index=False):
    try:
        valid_instances.append(
            validity_class(
                **{
                    key: row[i]
                    for i, key in enumerate(validity_class.__
                }
            )
        )
    except ValidationError as ve:  # pylint: disable=invalid-name
        validation_exceptions.append(ve)
 

The main issue seems to be getting good validation error messages.

Basic Python Logging

This works quite well, after importing the sys and logging packages. It sets the level to INFO, and sends the log to STDOUT:

log = logging.getLogger()
log.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
log.addHandler(handler)

Maybe best in a config module?