Skip to content

Commit 8478dbd

Browse files
committed
ci(mypy): add mypy check and adjust code for types
1 parent 37cf141 commit 8478dbd

File tree

9 files changed

+135
-19
lines changed

9 files changed

+135
-19
lines changed

.github/workflows/integration-test.yml

+22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ jobs:
5656
uses: pre-commit/[email protected]
5757
with:
5858
extra_args: --all-files
59+
python-type-checks:
60+
# This job is used to check Python types
61+
name: Python type checks
62+
# Avoid fail-fast to retain output
63+
strategy:
64+
fail-fast: false
65+
runs-on: ubuntu-22.04
66+
if: github.event_name != 'schedule'
67+
steps:
68+
- name: Checkout repo
69+
uses: actions/checkout@v4
70+
- name: Setup python, and check pre-commit cache
71+
uses: ./.github/actions/setup-env
72+
with:
73+
python-version: ${{ env.TARGET_PYTHON_VERSION }}
74+
cache-pre-commit: false
75+
cache-venv: true
76+
setup-poetry: true
77+
install-deps: true
78+
- name: Run mypy
79+
run: |
80+
poetry run mypy .
5981
integration-test:
6082
name: Pytest (Python ${{ matrix.python-version }} on ${{ matrix.os }})
6183
# Runs pytest on all tested versions of python and OSes

poetry.lock

+66-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pycytominer/aggregate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def aggregate(
121121
population_df = population_df.drop([columns_to_drop], axis="columns")
122122

123123
if output_file is not None:
124-
output(
124+
return output(
125125
df=population_df,
126126
output_filename=output_file,
127127
output_type=output_type,

pycytominer/cyto_utils/DeepProfiler_processing.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import pandas as pd
88
import warnings
99

10-
from pycytominer import aggregate, normalize
11-
from pycytominer.cyto_utils import (
10+
# use mypy ignores below to avoid duplicate import warnings
11+
from pycytominer import aggregate, normalize # type: ignore[no-redef]
12+
from pycytominer.cyto_utils import ( # type: ignore[no-redef]
1213
load_npz_features,
1314
load_npz_locations,
1415
infer_cp_features,

pycytominer/cyto_utils/cells.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def merge_single_cells(
714714
"""
715715

716716
# Load the single cell dataframe by merging on the specific linking columns
717-
sc_df = ""
717+
left_compartment_loaded = False
718718
linking_check_cols = []
719719
merge_suffix_rename = []
720720
for left_compartment in self.compartment_linking_cols:
@@ -737,7 +737,7 @@ def merge_single_cells(
737737
left_compartment
738738
]
739739

740-
if isinstance(sc_df, str):
740+
if not left_compartment_loaded:
741741
sc_df = self.load_compartment(compartment=left_compartment)
742742

743743
if compute_subsample:
@@ -752,6 +752,8 @@ def merge_single_cells(
752752
sc_df, how="left", on=subset_logic_df.columns.tolist()
753753
).reindex(sc_df.columns, axis="columns")
754754

755+
left_compartment_loaded = True
756+
755757
sc_df = sc_df.merge(
756758
self.load_compartment(compartment=right_compartment),
757759
left_on=[*self.merge_cols, left_link_col],
@@ -804,11 +806,13 @@ def merge_single_cells(
804806

805807
normalize_args["features"] = features
806808

807-
sc_df = normalize(profiles=sc_df, **normalize_args)
809+
# ignore mypy warnings below as these reference root package imports
810+
sc_df = normalize(profiles=sc_df, **normalize_args) # type: ignore[operator]
808811

809812
# In case platemap metadata is provided, use pycytominer.annotate for metadata
810813
if platemap is not None:
811-
sc_df = annotate(
814+
# ignore mypy warnings below as these reference root package imports
815+
sc_df = annotate( # type: ignore[operator]
812816
profiles=sc_df, platemap=platemap, output_file=None, **kwargs
813817
)
814818

pycytominer/cyto_utils/collate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def collate(
131131
with sqlite3.connect(cache_backend_file, isolation_level=None) as connection:
132132
cursor = connection.cursor()
133133
if column:
134-
if print:
134+
if printtoscreen:
135135
print(f"Adding a Metadata_Plate column based on column {column}")
136136
cursor.execute("ALTER TABLE Image ADD COLUMN Metadata_Plate TEXT;")
137137
cursor.execute(f"UPDATE image SET Metadata_Plate ={column};")

pycytominer/cyto_utils/output.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
def output(
1313
df: pd.DataFrame,
1414
output_filename: str,
15-
output_type: str = "csv",
15+
output_type: Optional[str] = "csv",
1616
sep: str = ",",
1717
float_format: Optional[str] = None,
18-
compression_options: Union[str, Dict] = {"method": "gzip", "mtime": 1},
18+
compression_options: Optional[Union[str, Dict]] = {"method": "gzip", "mtime": 1},
1919
**kwargs,
2020
):
2121
"""Given an output file and compression options, write file to disk
@@ -79,6 +79,10 @@ def output(
7979
)
8080
"""
8181

82+
# ensure a default output type
83+
if output_type is None:
84+
output_type = "csv"
85+
8286
if output_type == "csv":
8387
compression_options = set_compression_method(compression=compression_options)
8488

@@ -98,7 +102,7 @@ def output(
98102
return output_filename
99103

100104

101-
def set_compression_method(compression: Union[str, Dict]):
105+
def set_compression_method(compression: Optional[Union[str, Dict]]):
102106
"""Set the compression options
103107
104108
Parameters

pyproject.toml

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pytest-cov = "^4.1.0"
7373
pre-commit = ">=3.3.2"
7474
commitizen = "^3.12.0"
7575
ruff = "^0.3.4"
76+
mypy = "^1.11.2"
7677

7778
[tool.poetry.group.docs]
7879
optional = true
@@ -177,6 +178,18 @@ preview = true
177178
[tool.pytest.ini_options]
178179
testpaths = "tests"
179180

181+
[tool.mypy]
182+
# ignores optionally added type packages
183+
ignore_missing_imports = true
184+
# ignores redefinition of variable labels to new types
185+
allow_redefinition = true
186+
exclude = [
187+
# ignore notebook-based walkthroughs
188+
"walkthroughs",
189+
# ignore tests dir
190+
"tests"
191+
]
192+
180193
[build-system]
181194
requires = ["poetry-core>=1.7.0", "poetry-dynamic-versioning>=1.1.0"]
182195
build-backend = "poetry_dynamic_versioning.backend"

tests/test_cyto_utils/test_output.py

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ def test_output_default():
5656
result, DATA_DF, check_names=False, check_exact=False, atol=1e-3
5757
)
5858

59+
# test with an output_type of None
60+
output_result = output(
61+
df=DATA_DF,
62+
output_filename=output_filename,
63+
compression_options=TEST_COMPRESSION_OPTIONS,
64+
float_format=None,
65+
output_type=None,
66+
)
67+
result = pd.read_csv(output_result)
68+
69+
pd.testing.assert_frame_equal(
70+
result, DATA_DF, check_names=False, check_exact=False, atol=1e-3
71+
)
72+
5973

6074
def test_output_tsv():
6175
# Test input filename of writing a tab separated file

0 commit comments

Comments
 (0)