Skip to content

Commit eb7c5d5

Browse files
Fix mypy CI errors in sample_metadata.py (dtype typing for pd.read_csv)
1 parent 45babc6 commit eb7c5d5

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

malariagen_data/anoph/sample_metadata.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import io
22
from itertools import cycle
3-
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, Union
3+
from typing import (
4+
Any,
5+
Callable,
6+
Dict,
7+
List,
8+
Mapping,
9+
Optional,
10+
Sequence,
11+
Tuple,
12+
Union,
13+
Hashable,
14+
cast,
15+
)
416

517
import ipyleaflet # type: ignore
618
import numpy as np
@@ -148,9 +160,11 @@ def _parse_general_metadata(
148160
}
149161

150162
# Convert string dtypes to actual dtypes
151-
dtype = {col: dtype_map.get(dtype[col], str) for col in dtype}
163+
dtype_fixed: Mapping[Hashable, Union[str, np.dtype, type]] = {
164+
col: dtype_map.get(dtype[col], str) for col in dtype
165+
}
152166

153-
df = pd.read_csv(io.BytesIO(data), dtype=dtype, na_values="")
167+
df = pd.read_csv(io.BytesIO(data), dtype=dtype_fixed, na_values="")
154168

155169
# Ensure all column names are lower case.
156170
df.columns = [c.lower() for c in df.columns] # type: ignore
@@ -470,7 +484,12 @@ def _parse_aim_metadata(
470484
if isinstance(data, bytes):
471485
# Parse CSV data.
472486
df = pd.read_csv(
473-
io.BytesIO(data), dtype=self._aim_metadata_dtype, na_values=""
487+
io.BytesIO(data),
488+
dtype=cast(
489+
Mapping[Hashable, Union[str, type, np.dtype]],
490+
self._aim_metadata_dtype,
491+
),
492+
na_values="",
474493
)
475494

476495
# Ensure all column names are lower case.

0 commit comments

Comments
 (0)