Closed
Description
Situation
The code defining ManifestVersion
in "Combining Pydantic and semver" documentation
python-semver/docs/advanced/combine-pydantic-and-semver.rst
Lines 17 to 60 in 1f08632
has a bug when ManifestVersion
is used for serialization to JSON, resulting in the following raised exception:
pydantic_core._pydantic_core.PydanticSerializationError: Error calling function `<lambda>`: AttributeError: 'Version' object has no attribute 'x'
To Reproduce
from pydantic import TypeAdapter
from semver import Version
version = Version.parse('1.2.3')
json_version = TypeAdapter(ManifestVersion).dump_json(version)
Expected Behavior
is described in the following pytest module.
(with the ManifestVersion
defined in __init__.py
.)
import json
import pytest
from pydantic import TypeAdapter
from semver import Version
from . import ManifestVersion
@pytest.mark.parametrize(
'version',
[
'0.0.1',
'1.9.8',
'2.3.4-rc+generic',
'11.12.13-0.1.2',
],
)
def test_serialize(version):
expected_json = json.dumps(version).encode()
expected_python = version = Version.parse(version)
result_python = TypeAdapter(ManifestVersion).dump_python(version)
result_json = TypeAdapter(ManifestVersion).dump_json(version)
assert result_python == expected_python
assert result_json == expected_json
Solution
Change
python-semver/docs/advanced/combine-pydantic-and-semver.rst
Lines 49 to 51 in 1f08632
to
serialization = core_schema.to_string_ser_schema(),
Metadata
Metadata
Assignees
Labels
No labels