Skip to content

Commit 78a496f

Browse files
committed
address reviews
1 parent 29ef1a9 commit 78a496f

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ lint.per-file-ignores."src/cytodataframe/image.py" = [ "PLR2004" ]
116116
# ignore typing rules for tests
117117
lint.per-file-ignores."tests/*" = [ "ANN201", "PLR0913", "PLR2004", "SIM105" ]
118118

119-
# specify where version replacement is performed
119+
# configure Bandit security checks (exclude tests directory from scanning)
120120
[tool.bandit]
121121
exclude_dirs = [ "tests" ]
122122

src/cytodataframe/frame.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__( # noqa: PLR0913
124124
segmentation_file_regex: Optional[Dict[str, str]] = None,
125125
image_adjustment: Optional[Callable] = None,
126126
display_options: Optional[Dict[str, Any]] = None,
127-
*args: Tuple[Any, ...],
127+
*args: Any,
128128
**kwargs: Dict[str, Any],
129129
) -> None:
130130
"""
@@ -385,15 +385,16 @@ def _constructor(self: CytoDataFrame_type) -> Callable[..., CytoDataFrame_type]:
385385
def _build_result_cdf(
386386
self: CytoDataFrame_type,
387387
data: Union["CytoDataFrame", pd.DataFrame, pd.Series, Any],
388-
*args: Tuple[Any, ...],
388+
*args: Any,
389389
**kwargs: Dict[str, Any],
390390
) -> CytoDataFrame_type:
391391
"""Construct a result frame while preserving CytoDataFrame metadata."""
392392
if data is None and "data" in kwargs:
393393
data = kwargs.pop("data")
394394

395395
cdf = CytoDataFrame(
396-
data=data,
396+
data,
397+
*args,
397398
data_context_dir=self._custom_attrs["data_context_dir"],
398399
data_image_paths=self._custom_attrs["data_image_paths"],
399400
data_bounding_box=self._custom_attrs["data_bounding_box"],
@@ -403,7 +404,6 @@ def _build_result_cdf(
403404
segmentation_file_regex=self._custom_attrs["segmentation_file_regex"],
404405
image_adjustment=self._custom_attrs["image_adjustment"],
405406
display_options=self._custom_attrs["display_options"],
406-
*args,
407407
**kwargs,
408408
)
409409

@@ -422,7 +422,7 @@ def _return_cytodataframe(
422422
self: CytoDataFrame_type,
423423
method: Callable,
424424
method_name: str,
425-
*args: Tuple[Any, ...],
425+
*args: Any,
426426
**kwargs: Dict[str, Any],
427427
) -> Any:
428428
"""
@@ -434,7 +434,7 @@ def _return_cytodataframe(
434434
The method to be called and wrapped.
435435
method_name (str):
436436
The name of the method to be wrapped.
437-
*args (Tuple[Any, ...]):
437+
*args (Any):
438438
Positional arguments to be passed to the method.
439439
**kwargs (Dict[str, Any]):
440440
Keyword arguments to be passed to the method.
@@ -453,12 +453,14 @@ def _return_cytodataframe(
453453
else method(*args, **kwargs)
454454
)
455455

456-
if isinstance(result, pd.DataFrame):
457-
cdf = self._build_result_cdf(result)
458-
# If the method name is transpose we know that
459-
# the dataframe has been transposed.
460-
if method_name == "transpose" and not self._custom_attrs["is_transposed"]:
461-
cdf._custom_attrs["is_transposed"] = True
456+
if not isinstance(result, pd.DataFrame):
457+
return result
458+
459+
cdf = self._build_result_cdf(result)
460+
# If the method name is transpose we know that
461+
# the dataframe has been transposed.
462+
if method_name == "transpose" and not self._custom_attrs["is_transposed"]:
463+
cdf._custom_attrs["is_transposed"] = True
462464

463465
return cdf
464466

@@ -482,7 +484,7 @@ def _wrap_method(self: CytoDataFrame_type, method_name: str) -> Callable:
482484
the result is a CytoDataFrame.
483485
"""
484486

485-
def wrapper(*args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> Any:
487+
def wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Any:
486488
"""
487489
Wraps the specified method to ensure
488490
it returns a CytoDataFrame.
@@ -494,7 +496,7 @@ def wrapper(*args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> Any:
494496
custom attributes.
495497
496498
Args:
497-
*args (Tuple[Any, ...]):
499+
*args (Any):
498500
Positional arguments to be passed to the method.
499501
**kwargs (Dict[str, Any]):
500502
Keyword arguments to be passed to the method.

tests/test_frame.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,16 @@ def test_return_cytodataframe(cytotable_NF1_data_parquet_shrunken: str):
801801
assert isinstance(cdf.iloc[0:5:2], CytoDataFrame)
802802

803803

804+
def test_return_cytodataframe_passthroughs_non_dataframe_results() -> None:
805+
"""Ensure helper methods return scalar-like results without wrapping."""
806+
807+
cdf = CytoDataFrame(pd.DataFrame({"a": [1, 2, 3]}))
808+
809+
result = cdf._return_cytodataframe(lambda: 3, "dummy_method")
810+
811+
assert result == 3
812+
813+
804814
def test_iloc_slice_preserves_cytodataframe_html_formatting():
805815
"""Ensure ``iloc`` slices keep the CytoDataFrame notebook HTML renderer."""
806816

0 commit comments

Comments
 (0)