Skip to content

Commit 2a05ffb

Browse files
author
gabriel
committed
accpet only None in Union
1 parent 16f20bf commit 2a05ffb

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

dataframely/collection/_base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,17 @@ def _derive_member_info(
244244
attr, annotation_args[0], annotation_args[1]
245245
)
246246
elif origin == typing.Union:
247-
# Happy path: optional member
247+
# Happy path: optional member (e.g. dy.LazyFrame[Schema] | None)
248248
union_args = get_args(type_annotation)
249249
if len(union_args) != 2:
250250
raise AnnotationImplementationError(attr, type_annotation)
251-
if not any(get_origin(arg) is None for arg in union_args):
251+
# Check that exactly one arg is None (type(None) is NoneType)
252+
if not any(arg is type(None) for arg in union_args):
252253
raise AnnotationImplementationError(attr, type_annotation)
253254

254-
not_none_args = [arg for arg in union_args if get_origin(arg) is not None]
255-
if len(not_none_args) == 0:
255+
# Get the non-None type
256+
not_none_args = [arg for arg in union_args if arg is not type(None)]
257+
if len(not_none_args) != 1:
256258
raise AnnotationImplementationError(attr, type_annotation)
257259

258260
frame_origin = get_origin(not_none_args[0])

tests/collection/test_implementation.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_annotation_only_none_failure() -> None:
113113

114114

115115
def test_annotation_invalid_type_failure() -> None:
116-
"""First argument of union must be a LazyFrame."""
116+
"""First argument of union must be a LazyFrame or DataFrame."""
117117
with pytest.raises(AnnotationImplementationError):
118118
create_collection_raw(
119119
"test",
@@ -123,6 +123,39 @@ def test_annotation_invalid_type_failure() -> None:
123123
)
124124

125125

126+
def test_annotation_invalid_generic_type_in_union() -> None:
127+
"""Union with generic type that's not LazyFrame/DataFrame should fail."""
128+
with pytest.raises(AnnotationImplementationError):
129+
create_collection_raw(
130+
"test",
131+
{
132+
"first": list[int] | None,
133+
},
134+
)
135+
136+
137+
def test_annotation_union_frame_with_non_none_type() -> None:
138+
"""Union of DataFrame with non-None type should fail."""
139+
with pytest.raises(AnnotationImplementationError):
140+
create_collection_raw(
141+
"test",
142+
{
143+
"first": dy.DataFrame[MyTestSchema] | int,
144+
},
145+
)
146+
147+
148+
def test_annotation_invalid_standalone_generic() -> None:
149+
"""Standalone generic type that's not LazyFrame/DataFrame should fail."""
150+
with pytest.raises(AnnotationImplementationError):
151+
create_collection_raw(
152+
"test",
153+
{
154+
"first": list[int],
155+
},
156+
)
157+
158+
126159
def test_explicit_annotation_type_failure_no_frame_type() -> None:
127160
"""First argument of the annotated union must be a LazyFrame."""
128161
with pytest.raises(AnnotationImplementationError):

0 commit comments

Comments
 (0)