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

potential memory leak fix #223

Merged
merged 5 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_stages:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v5.0.0
hooks:
- id: check-yaml
- id: check-toml
Expand All @@ -30,16 +30,16 @@ repos:
- id: add-trailing-comma
stages:
- commit
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.4
- repo: https://github.com/hhatto/autopep8
rev: v2.3.1
hooks:
- id: autopep8
stages:
- commit
args:
- --diff
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
rev: 7.1.1
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
Expand All @@ -63,7 +63,7 @@ repos:
- --multi-line=9
- --project=pydantic_xml
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
rev: v1.13.0
hooks:
- id: mypy
stages:
Expand Down
3 changes: 2 additions & 1 deletion examples/generic-model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ class CreateCompanyMethod(

request = CreateCompanyRequest.from_xml(xml_doc)

assert request == CreateCompanyRequest.parse_file('./doc.json')
json_doc = pathlib.Path('./doc.json').read_text()
assert request == CreateCompanyRequest.model_validate_json(json_doc)
2 changes: 1 addition & 1 deletion examples/snippets/mapping_typed.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ class Company(BaseXmlModel):
''' # [json-end]

company = Company.from_xml(xml_doc)
assert company == Company.parse_raw(json_doc)
assert company == Company.model_validate_json(json_doc)
2 changes: 1 addition & 1 deletion pydantic_xml/serializers/factories/heterogeneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def deserialize(
item_errors[idx] = err

if item_errors:
raise utils.build_validation_error(title=self._model_name, errors_map=item_errors)
raise utils.into_validation_error(title=self._model_name, errors_map=item_errors)

if all((value is None for value in result)):
return None
Expand Down
2 changes: 1 addition & 1 deletion pydantic_xml/serializers/factories/homogeneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def deserialize(
result.append(value)

if item_errors:
raise utils.build_validation_error(title=self._model_name, errors_map=item_errors)
raise utils.into_validation_error(title=self._model_name, errors_map=item_errors)

return result or None

Expand Down
4 changes: 2 additions & 2 deletions pydantic_xml/serializers/factories/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def deserialize(
field_errors[field_name] = err

if field_errors:
raise utils.build_validation_error(title=self._model.__name__, errors_map=field_errors)
raise utils.into_validation_error(title=self._model.__name__, errors_map=field_errors)

if self._model.model_config.get('extra', 'ignore') == 'forbid':
self._check_extra(self._model.__name__, element)
Expand Down Expand Up @@ -322,7 +322,7 @@ def deserialize(
if result is None:
result = pdc.PydanticUndefined
except pd.ValidationError as err:
raise utils.build_validation_error(title=self._model.__name__, errors_map={None: err})
raise utils.into_validation_error(title=self._model.__name__, errors_map={None: err})

if self._model.model_config.get('extra', 'ignore') == 'forbid':
self._check_extra(self._model.__name__, element)
Expand Down
18 changes: 10 additions & 8 deletions pydantic_xml/serializers/factories/union.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Any, Dict, List, Optional, Set, Tuple
from typing import Any, Dict, List, Optional, Set, Tuple, Union

import pydantic as pd
from pydantic_core import core_schema as pcs

import pydantic_xml as pxml
from pydantic_xml import errors
from pydantic_xml import errors, utils
from pydantic_xml.element import XmlElementReader, XmlElementWriter
from pydantic_xml.serializers.factories.model import ModelProxySerializer
from pydantic_xml.serializers.serializer import TYPE_FAMILY, SchemaTypeFamily, Serializer
Expand Down Expand Up @@ -65,6 +65,7 @@ def deserialize(
class ModelSerializer(Serializer):
@classmethod
def from_core_schema(cls, schema: pcs.UnionSchema, ctx: Serializer.Context) -> 'ModelSerializer':
model_name = ctx.model_name
computed = ctx.field_computed
inner_serializers: List[ModelProxySerializer] = []
for choice_schema in schema['choices']:
Expand All @@ -78,9 +79,10 @@ def from_core_schema(cls, schema: pcs.UnionSchema, ctx: Serializer.Context) -> '

assert len(inner_serializers) > 0, "union choice is not provided"

return cls(computed, tuple(inner_serializers))
return cls(model_name, computed, tuple(inner_serializers))

def __init__(self, computed: bool, inner_serializers: Tuple[ModelProxySerializer, ...]):
def __init__(self, model_name: str, computed: bool, inner_serializers: Tuple[ModelProxySerializer, ...]):
self._model_name = model_name
self._computed = computed
self._inner_serializers = inner_serializers

Expand Down Expand Up @@ -119,7 +121,7 @@ def deserialize(
if element is None:
return None

last_error: Optional[Exception] = None
union_errors: Dict[Union[None, str, int], pd.ValidationError] = {}
result: Any = None
for serializer in self._inner_serializers:
snapshot = element.create_snapshot()
Expand All @@ -130,11 +132,11 @@ def deserialize(
element.apply_snapshot(snapshot)
return result
except pd.ValidationError as e:
last_error = e
union_errors[e.title] = e

if last_error is not None:
if union_errors:
element.step_forward()
raise last_error
raise utils.into_validation_error(title=self._model_name, errors_map=union_errors)

return result

Expand Down
9 changes: 5 additions & 4 deletions pydantic_xml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import itertools as it
import re
from collections import ChainMap
from typing import Dict, Iterable, List, Mapping, Optional, Union, cast
from typing import Dict, Iterable, List, Optional, Union, cast

import pydantic as pd
import pydantic_core as pdc
Expand Down Expand Up @@ -97,12 +97,13 @@ def select_ns(*nss: Optional[str]) -> Optional[str]:
return None


def build_validation_error(
def into_validation_error(
title: str,
errors_map: Mapping[Union[None, str, int], pd.ValidationError],
errors_map: Dict[Union[None, str, int], pd.ValidationError],
) -> pd.ValidationError:
line_errors: List[pdc.InitErrorDetails] = []
for location, validation_error in errors_map.items():
for location in list(errors_map):
validation_error = errors_map.pop(location)
for error in validation_error.errors():
line_errors.append(
pdc.InitErrorDetails(
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Typing :: Typed",
]

[tool.poetry.dependencies]
python = ">=3.8"
lxml = {version = ">=4.9.0", optional = true}
pydantic = ">=2.6.0"
pydantic = ">=2.6.0, !=2.10.0b1"
pydantic-core = ">=2.15.0"

furo = {version = "^2022.12.7", optional = true}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class TestModel(BaseXmlModel, tag='model'):
assert err.errors() == [
{
'input': 'a',
'loc': ('submodel', 0, 'data'),
'loc': ('submodel', 0, 'TestSubModel2', 'data'),
'msg': f'[line {fmt_sourceline(3)}]: Input should be a valid number, unable to parse string as a number',
'type': 'float_parsing',
'ctx': {
Expand All @@ -208,7 +208,7 @@ class TestModel(BaseXmlModel, tag='model'):
},
{
'input': 'b',
'loc': ('submodel', 1, 'data'),
'loc': ('submodel', 1, 'TestSubModel1', 'data'),
'msg': f'[line {fmt_sourceline(4)}]: Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
'ctx': {
Expand Down
Loading