https://github.com/lewiscowles1986/pydantic-gotcha-cannot-serialize-fieldinfo
Minimal reproducible case, to showcase pydantic error, which surprised me 2023-09-15
https://github.com/lewiscowles1986/pydantic-gotcha-cannot-serialize-fieldinfo
fix learn-in-public pydantic python reproduction
Last synced: 3 months ago
JSON representation
Minimal reproducible case, to showcase pydantic error, which surprised me 2023-09-15
- Host: GitHub
- URL: https://github.com/lewiscowles1986/pydantic-gotcha-cannot-serialize-fieldinfo
- Owner: Lewiscowles1986
- Created: 2023-09-15T23:48:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-15T23:56:07.000Z (over 1 year ago)
- Last Synced: 2025-03-06T14:55:07.771Z (3 months ago)
- Topics: fix, learn-in-public, pydantic, python, reproduction
- Language: Python
- Homepage:
- Size: 7.81 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Minimal reproducible example
This is a Minimal reproducible example of a weird error from Pydantic, I got a bit turned around by today. Thanks to Robert Cox for rubber-ducking my hellish code (it wasn't this small)
```
Exception has occurred: PydanticSerializationError
Unable to serialize unknown type:
File "/Users/lewiscowles/Projects/study/pydantic-horror-show/pydantic_horror_show/__init__.py", line 13, in
json = c.json(by_alias=True)
^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type:
```## The fix
Well it's simple, just remove the comma, after field b is defined.
https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/trailing-comma-tuple.html
Annoyingly I was using Ruff, black, mypy and some more linting in my project; they all missed this.
My Google search also turned up nothing of much use.So here is a repo with a minimal example and the advice on fixing 🙃
## Author note
Pydantic is great. I'd consider this a weakness of implementation though. It's a bit of a footgun.
Maybe the error could be nicer; and specifically call out two fields in a tuple being unable to de-serialize.It's still a great tool, and I hope to avoid this footgun in future 😄.