Skip to content
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

Specify value in UserWarning "serialized value may not be as expected" #1336

Closed
franklinvp opened this issue Jun 21, 2024 · 2 comments · Fixed by #1377
Closed

Specify value in UserWarning "serialized value may not be as expected" #1336

franklinvp opened this issue Jun 21, 2024 · 2 comments · Fixed by #1377
Assignees

Comments

@franklinvp
Copy link

franklinvp commented Jun 21, 2024

Would it be possible to make this warning more specific by mentioning either the value or perhaps the field that caused it?

I am calling request.model_dump_json(indent=2) and getting

UserWarning: Pydantic serializer warnings:
  Expected `enum` but got `str` - serialized value may not be as expected
  return self.__pydantic_serializer__.to_json(

but there so many enum.Enum in the request that is hard to find which is causing the warning.

The place where the warning message is composed seems1 to know the value.

fn fallback_warning(&self, field_type: &str, value: &Bound<'_, PyAny>) {
        if self.mode != WarningsMode::None {
            let type_name = value
                .get_type()
                .qualname()
                .unwrap_or_else(|_| "<unknown python object>".to_owned());
            self.add_warning(format!(
                "Expected `{field_type}` but got `{type_name}` - serialized value may not be as expected"
            ));
        }
    }

Footnotes

  1. I am not sure, since I am not familiar with Rust.

@franklinvp
Copy link
Author

I think that what #828 is asking (traceback information) has a similar purpose. Although, there it is for serialization errors.

@franklinvp
Copy link
Author

By the way, I was able to find the value and field causing the warning by building pydantic-core with the following modification to the error message

-                "Expected `{field_type}` but got `{type_name}` - serialized value may not be as expected"
+                "Expected `{field_type}` but got `{type_name}` value: `{value}` - serialized value may not be as expected"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants