Skip to content

Commit 80b4486

Browse files
gaogaotiantianHyukjinKwon
authored andcommitted
[SPARK-54769][PYTHON] Remove dead code in conversion.py
### What changes were proposed in this pull request? Dead code is removed and replaced with assertions. ### Why are the changes needed? There are two kinds of dead code in this PR: 1. The else case. We already confirmed that we need a converter for this `dataType`, `else` case should not ever happen, unless we made a mistake in our code. 2. If `ArrayType` has an `elementType` that does not need a converter, the `ArrayType` itself should not need a converter and we should never ask for one. assertion has clear semantics that this should never happen, but also provides a reasonable and reproducible fallback behavior when unexpected happens. We can catch our code failure faster. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? CI ### Was this patch authored or co-authored using generative AI tooling? No Closes #53536 from gaogaotiantian/remove-dead-code. Authored-by: Tian Gao <gaogaotiantian@hotmail.com> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
1 parent 2549a58 commit 80b4486

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

python/pyspark/sql/conversion.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,8 @@ def convert_other(value: Any) -> Any:
436436
return value
437437

438438
return convert_other
439-
else:
440-
if none_on_identity:
441-
return None
442-
else:
443-
return lambda value: value
439+
else: # pragma: no cover
440+
assert False, f"Need converter for {dataType} but failed to find one."
444441

445442
@staticmethod
446443
def convert(data: Sequence[Any], schema: StructType, use_large_var_types: bool) -> "pa.Table":
@@ -613,23 +610,16 @@ def convert_struct(value: Any) -> Any:
613610
dataType.elementType, none_on_identity=True, binary_as_bytes=binary_as_bytes
614611
)
615612

616-
if element_conv is None:
617-
618-
def convert_array(value: Any) -> Any:
619-
if value is None:
620-
return None
621-
else:
622-
assert isinstance(value, list)
623-
return value
624-
625-
else:
613+
assert (
614+
element_conv is not None
615+
), f"_need_converter() returned True for ArrayType of {dataType.elementType}"
626616

627-
def convert_array(value: Any) -> Any:
628-
if value is None:
629-
return None
630-
else:
631-
assert isinstance(value, list)
632-
return [element_conv(v) for v in value]
617+
def convert_array(value: Any) -> Any:
618+
if value is None:
619+
return None
620+
else:
621+
assert isinstance(value, list)
622+
return [element_conv(v) for v in value]
633623

634624
return convert_array
635625

@@ -793,11 +783,8 @@ def convert_geometry(value: Any) -> Any:
793783

794784
return convert_geometry
795785

796-
else:
797-
if none_on_identity:
798-
return None
799-
else:
800-
return lambda value: value
786+
else: # pragma: no cover
787+
assert False, f"Need converter for {dataType} but failed to find one."
801788

802789
@overload
803790
@staticmethod

0 commit comments

Comments
 (0)