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

ser_json_inf_nan not respected when used with an Any list #8693

Open
1 task done
matsjoyce-refeyn opened this issue Feb 1, 2024 · 1 comment · May be fixed by #9150
Open
1 task done

ser_json_inf_nan not respected when used with an Any list #8693

matsjoyce-refeyn opened this issue Feb 1, 2024 · 1 comment · May be fixed by #9150
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@matsjoyce-refeyn
Copy link
Contributor

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When using a dataclass with a non-specialised list, NaNs are serialised in json mode as None, not NaN, even when ser_json_inf_nan is set to constants. This behaviour goes away if list[float] is used. But for my use case, the list might be arbitrarily nested (e.g. list[list[list[float]]]) so leaving it as list[Any] seems to be the only solution (unless recursive type aliases are supported).

Example Code

import pprint

import pydantic

@dataclasses.dataclass(frozen=True)
class Z:
    inner: list
    __pydantic_config__ = {"ser_json_inf_nan": "constants"}

a = pydantic.TypeAdapter(Z)

pprint.pprint(a.core_schema)
# Output:
# {'cls': <class 'tests.parsing.test_utils.test_parse_then_unparse_nans.<locals>.Z'>,
#  'config': {'ser_json_inf_nan': 'constants', 'title': 'Z'},
#  'fields': ['inner'],
#  'metadata': {'pydantic.internal.needs_apply_discriminated_union': False},
#  'post_init': False,
#  'ref': 'tests.parsing.test_utils.test_parse_then_unparse_nans.<locals>.Z:1355051499120',
#  'schema': {'collect_init_only': False,
#             'computed_fields': [],
#             'dataclass_name': 'Z',
#             'fields': [{'init': True,
#                         'kw_only': False,
#                         'metadata': {'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func at 0x0000013B4670E9E0>],
#                                      'pydantic_js_functions': []},
#                         'name': 'inner',
#                         'schema': {'items_schema': {'type': 'any'},
#                                    'strict': False,
#                                    'type': 'list'},
#                         'type': 'dataclass-field'}],
#             'type': 'dataclass-args'},
#  'slots': False,
#  'type': 'dataclass'}

pprint.pprint(a.dump_python(Z([math.nan]), mode="json"))
# Output:
# {'inner': [None]}

Python, Pydantic & OS Version

pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=true
                 install path: C:\Users\matthew.joyce\Repos\iscat\venv\venv_develop\Lib\site-packages\pydantic
               python version: 3.10.10 (tags/v3.10.10:aad5f6a, Feb  7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
             related packages: mypy-1.2.0 typing_extensions-4.9.0
                       commit: unknown
@matsjoyce-refeyn matsjoyce-refeyn added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 1, 2024
@sydney-runkle
Copy link
Member

@matsjoyce-refeyn,

Thanks for reporting this. This looks pretty similar to #8440, and is definitely something that we want to fix.

I'll add this to the 2.7.0 milestone and make sure we get a fix out for this with the next minor release 👍. Thanks for bringing this to our attention!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants