Allow for decimal parsing in pydantic model#141
Conversation
There was a problem hiding this comment.
Pull request overview
Adds support in extract_records to coerce Pydantic Decimal values into float to simplify downstream numeric processing (e.g., DataFrame usage).
Changes:
- Introduces recursive Decimal-to-float coercion and a new
decimal_to_floatflag inextract_records. - Adds unit tests covering Decimal coercion (including nested/domain extraction) and the disabled behavior.
- Updates documentation examples to mention/use the Decimal parsing option.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/entsoe/utils/records.py |
Adds decimal_to_float parameter and conversion/serialization path for Decimal handling. |
tests/test_extract_records.py |
Adds tests validating Decimal coercion and the opt-out behavior. |
docs/examples.md |
Updates examples and documents Decimal-to-float conversion behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if decimal_to_float: | ||
| # Keep Decimal values typed, coerce them, then normalize to JSON-compatible | ||
| # values so the output structure stays aligned with mode="json". | ||
| data_dict = [item.model_dump(mode="python") for item in data_list] | ||
| data_dict = [_coerce_decimals_to_float(item_dict) for item_dict in data_dict] | ||
| data_dict = [_ANY_ADAPTER.dump_python(item_dict, mode="json") for item_dict in data_dict] |
There was a problem hiding this comment.
The decimal_to_float=True path switches from model_dump(mode="json") to model_dump(mode="python") plus a generic TypeAdapter(Any) JSON dump. This is only covered by tests using built-in datetime; please add a regression test using one of the library’s real xsdata datatypes (e.g., xsdata.models.datatype.XmlDateTime/XmlDate) to confirm this serialization path remains JSON-compatible for actual API models.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
… into pydantic-decimal
This MR allows the
extract_recordsfunction to parse decimals to float for further processing.