Skip to content

Commit cad5214

Browse files
committed
Include Field/SparkField examples in Field metadata
1 parent cc4ea55 commit cad5214

File tree

3 files changed

+102
-57
lines changed

3 files changed

+102
-57
lines changed

src/sparkdantic/model.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,6 @@ def create_json_spark_schema(
206206
annotation_or_return_type = _get_annotation_or_return_type(info)
207207
field_type = _get_union_type_arg(annotation_or_return_type)
208208

209-
description = getattr(info, 'description', None)
210-
comment = {'comment': description} if description else {}
211-
212209
spark_type: Union[str, Dict[str, Any]]
213210

214211
try:
@@ -248,7 +245,7 @@ def create_json_spark_schema(
248245
'name': name,
249246
'type': spark_type,
250247
'nullable': nullable,
251-
'metadata': comment,
248+
'metadata': _json_field_metadata(info),
252249
}
253250
fields.append(struct_field)
254251
return {
@@ -627,3 +624,14 @@ def json_schema_to_ddl(json_schema: Dict[str, Any]) -> str:
627624
field_ddls.append(field_ddl)
628625

629626
return ','.join(field_ddls)
627+
628+
629+
def _json_field_metadata(info: ComputedFieldInfo) -> dict[str, str]:
630+
description = getattr(info, 'description', None)
631+
metadata = {'comment': description} if description else {}
632+
633+
examples = getattr(info, 'examples', None)
634+
if examples:
635+
metadata['examples'] = examples
636+
637+
return metadata

tests/test_field_descriptions.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/test_field_metadata.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from pyspark.sql.types import StringType, StructField, StructType
2+
3+
from sparkdantic import SparkField, SparkModel
4+
5+
6+
class DescriptionModel(SparkModel):
7+
field_with_description: str = SparkField(description='This is a test description.')
8+
field_with_examples: str = SparkField(examples=['test'])
9+
field_with_description_and_examples: str = SparkField(
10+
description='testing description', examples=['testing example']
11+
)
12+
13+
field_without_metadata: str = SparkField()
14+
15+
16+
def test_spark_schema_contains_field_metadata():
17+
expected_schema = StructType(
18+
[
19+
StructField(
20+
'field_with_description',
21+
StringType(),
22+
False,
23+
metadata={'comment': 'This is a test description.'},
24+
),
25+
StructField(
26+
'field_with_examples',
27+
StringType(),
28+
False,
29+
metadata={'examples': ['test']},
30+
),
31+
StructField(
32+
'field_with_description_and_examples',
33+
StringType(),
34+
False,
35+
metadata={
36+
'comment': 'testing description',
37+
'examples': ['testing example'],
38+
},
39+
),
40+
StructField(
41+
'field_without_metadata',
42+
StringType(),
43+
False,
44+
metadata={},
45+
),
46+
]
47+
)
48+
49+
actual_schema = DescriptionModel.model_spark_schema()
50+
print('hmmm')
51+
print(actual_schema)
52+
assert actual_schema == expected_schema
53+
54+
55+
def test_spark_schema_json_contains_field_metadata():
56+
expected_json_schema = {
57+
'type': 'struct',
58+
'fields': [
59+
{
60+
'name': 'field_with_description',
61+
'type': 'string',
62+
'nullable': False,
63+
'metadata': {'comment': 'This is a test description.'},
64+
},
65+
{
66+
'name': 'field_with_examples',
67+
'type': 'string',
68+
'nullable': False,
69+
'metadata': {'examples': ['test']},
70+
},
71+
{
72+
'name': 'field_with_description_and_examples',
73+
'type': 'string',
74+
'nullable': False,
75+
'metadata': {
76+
'comment': 'testing description',
77+
'examples': ['testing example'],
78+
},
79+
},
80+
{
81+
'name': 'field_without_metadata',
82+
'type': 'string',
83+
'nullable': False,
84+
'metadata': {},
85+
},
86+
],
87+
}
88+
89+
actual_json_schema = DescriptionModel.model_json_spark_schema()
90+
assert actual_json_schema == expected_json_schema

0 commit comments

Comments
 (0)