msgspec-xml 0.3.0__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/PKG-INFO +1 -1
  2. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/msgspec_xml/__init__.py +1 -1
  3. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/msgspec_xml/introspection.py +6 -2
  4. msgspec_xml-0.3.1/tests/resources/test-05.xml +7 -0
  5. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/tests/test_roundtrip.py +15 -1
  6. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/.gitattributes +0 -0
  7. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/.gitignore +0 -0
  8. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/.pre-commit-config.yaml +0 -0
  9. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/LICENSE +0 -0
  10. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/README.md +0 -0
  11. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/msgspec_xml/decoder.py +0 -0
  12. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/msgspec_xml/encoder.py +0 -0
  13. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/msgspec_xml/metadata.py +0 -0
  14. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/pdm.toml +0 -0
  15. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/pylock.toml +0 -0
  16. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/pyproject.toml +0 -0
  17. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/renovate.json +0 -0
  18. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/tests/__init__.py +0 -0
  19. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/tests/resources/test-01.xml +0 -0
  20. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/tests/resources/test-02.xml +0 -0
  21. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/tests/resources/test-03.xml +0 -0
  22. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/tests/resources/test-04.xml +0 -0
  23. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/tests/test_metadata.py +0 -0
  24. {msgspec_xml-0.3.0 → msgspec_xml-0.3.1}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: msgspec-xml
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: XML Serialization/Deserialization using msgspec
5
5
  Project-URL: Homepage, https://pypi.org/project/msgspec-xml
6
6
  Project-URL: Issues, https://codefloe.com/buriedincode/msgspec-xml/issues
@@ -9,7 +9,7 @@ __all__ = [
9
9
  "decode",
10
10
  "encode",
11
11
  ]
12
- __version__ = "0.3.0"
12
+ __version__ = "0.3.1"
13
13
 
14
14
  from msgspec_xml.decoder import Decoder, decode
15
15
  from msgspec_xml.encoder import Encoder, encode
@@ -24,6 +24,7 @@ class FieldType(Struct, frozen=True, kw_only=True):
24
24
  type: Any
25
25
  xml: XML
26
26
  is_struct: bool = False
27
+ is_generic: bool = False
27
28
  item: Optional["FieldType"] = None
28
29
 
29
30
 
@@ -35,6 +36,9 @@ def inspect_annotation(annotation: Any) -> FieldType: # noqa: ANN401
35
36
  return FieldType(
36
37
  type=annotation, xml=xml, item=inspect_annotation(annotation=item_annotation)
37
38
  )
39
+ origin = get_origin(annotation)
40
+ if origin is not None and isinstance(origin, type) and issubclass(origin, Struct):
41
+ return FieldType(type=origin, xml=xml, is_struct=True, is_generic=True)
38
42
  return FieldType(type=annotation, xml=xml, is_struct=is_struct_type(annotation))
39
43
 
40
44
 
@@ -60,7 +64,7 @@ def attribute_name(field: FieldInfo, info: FieldType, namespaces: dict[str, str]
60
64
  def element_tag(field: FieldInfo, info: FieldType, namespaces: dict[str, str]) -> str:
61
65
  if info.xml.tag:
62
66
  name = info.xml.tag
63
- elif info.is_struct:
67
+ elif info.is_struct and not info.is_generic:
64
68
  name = class_tag(cls=info.type)
65
69
  else:
66
70
  name = field.encode_name
@@ -76,7 +80,7 @@ def item_tag(field: FieldInfo, info: FieldType, namespaces: dict[str, str]) -> s
76
80
  name = item.xml.tag
77
81
  elif info.xml.tag:
78
82
  name = info.xml.tag
79
- elif item.is_struct:
83
+ elif item.is_struct and not item.is_generic:
80
84
  name = class_tag(cls=item.type)
81
85
  else:
82
86
  name = field.encode_name
@@ -0,0 +1,7 @@
1
+ <Credit>
2
+ <Creator id="cr1">Michael Moreci</Creator>
3
+ <Roles>
4
+ <Role>Writer</Role>
5
+ <Role>Artist</Role>
6
+ </Roles>
7
+ </Credit>
@@ -1,6 +1,6 @@
1
1
  from decimal import Decimal
2
2
  from pathlib import Path
3
- from typing import Annotated
3
+ from typing import Annotated, Generic, TypeVar
4
4
 
5
5
  import pytest
6
6
  from msgspec import Struct
@@ -45,6 +45,19 @@ class Book(Struct, rename="pascal"):
45
45
  authors: Annotated[list[Author], XML(wrapper="Authors", tag="Author")]
46
46
 
47
47
 
48
+ T = TypeVar("T")
49
+
50
+
51
+ class Resource(Struct, Generic[T]):
52
+ value: Annotated[T, Value()]
53
+ id: Annotated[str | None, Attribute()] = None
54
+
55
+
56
+ class Credit(Struct, rename="pascal"):
57
+ creator: Annotated[Resource[str], Element()]
58
+ roles: Annotated[list[Resource[str]], Wrapper(wrapper="Roles", tag="Role")]
59
+
60
+
48
61
  def load_resource(name: str) -> str:
49
62
  return (RESOURCE_DIR / name).read_text(encoding="UTF-8")
50
63
 
@@ -56,6 +69,7 @@ def load_resource(name: str) -> str:
56
69
  ("test-02.xml", Greetings, True),
57
70
  ("test-03.xml", Product, False),
58
71
  ("test-04.xml", Book, True),
72
+ ("test-05.xml", Credit, False),
59
73
  ],
60
74
  )
61
75
  def test_roundtrip(resource: str, cls: type[Struct], show_declaration: bool) -> None:
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes