5353 from os import PathLike
5454 from typing import Any , Literal
5555
56+ from zarr .storage import StoreLike
57+
5658 from ..compat import Index1D
5759 from ..typing import XDataType
5860 from .aligned_mapping import AxisArraysView , LayersView , PairwiseArraysView
@@ -216,7 +218,7 @@ def __init__(
216218 raw : Mapping [str , Any ] | None = None ,
217219 dtype : np .dtype | type | str | None = None ,
218220 shape : tuple [int , int ] | None = None ,
219- filename : PathLike | None = None ,
221+ filename : PathLike [ str ] | str | None = None ,
220222 filemode : Literal ["r" , "r+" ] | None = None ,
221223 asview : bool = False ,
222224 obsp : np .ndarray | Mapping [str , Sequence [Any ]] | None = None ,
@@ -960,7 +962,7 @@ def filename(self) -> Path | None:
960962 return self .file .filename
961963
962964 @filename .setter
963- def filename (self , filename : PathLike | None ):
965+ def filename (self , filename : PathLike [ str ] | str | None ):
964966 # convert early for later comparison
965967 filename = None if filename is None else Path (filename )
966968 # change from backing-mode back to full loading into memory
@@ -1439,7 +1441,7 @@ def to_memory(self, *, copy: bool = False) -> AnnData:
14391441
14401442 return AnnData (** new )
14411443
1442- def copy (self , filename : PathLike | None = None ) -> AnnData :
1444+ def copy (self , filename : PathLike [ str ] | str | None = None ) -> AnnData :
14431445 """Full copy, optionally on disk."""
14441446 if not self .isbacked :
14451447 if self .is_view and self ._has_X ():
@@ -1800,9 +1802,12 @@ def _check_dimensions(self, key=None):
18001802 )
18011803 raise ValueError (msg )
18021804
1805+ @old_positionals ("compression" , "compression_opts" , "as_dense" )
18031806 def write_h5ad (
18041807 self ,
1805- filename : PathLike | None = None ,
1808+ filename : PathLike [str ] | str | None = None ,
1809+ * ,
1810+ convert_strings_to_categoricals : bool = True ,
18061811 compression : Literal ["gzip" , "lzf" ] | None = None ,
18071812 compression_opts : int | Any = None ,
18081813 as_dense : Sequence [str ] = (),
@@ -1826,6 +1831,8 @@ def write_h5ad(
18261831 ----------
18271832 filename
18281833 Filename of data file. Defaults to backing file.
1834+ convert_strings_to_categoricals
1835+ Convert string columns to categorical.
18291836 compression
18301837 For [`lzf`, `gzip`], see the h5py :ref:`dataset_compression`.
18311838
@@ -1880,6 +1887,7 @@ def write_h5ad(
18801887 write_h5ad (
18811888 Path (filename ),
18821889 self ,
1890+ convert_strings_to_categoricals = convert_strings_to_categoricals ,
18831891 compression = compression ,
18841892 compression_opts = compression_opts ,
18851893 as_dense = as_dense ,
@@ -1891,7 +1899,9 @@ def write_h5ad(
18911899 write = write_h5ad # a shortcut and backwards compat
18921900
18931901 @old_positionals ("skip_data" , "sep" )
1894- def write_csvs (self , dirname : PathLike , * , skip_data : bool = True , sep : str = "," ):
1902+ def write_csvs (
1903+ self , dirname : PathLike [str ] | str , * , skip_data : bool = True , sep : str = ","
1904+ ):
18951905 """\
18961906 Write annotation to `.csv` files.
18971907
@@ -1912,7 +1922,9 @@ def write_csvs(self, dirname: PathLike, *, skip_data: bool = True, sep: str = ",
19121922 write_csvs (dirname , self , skip_data = skip_data , sep = sep )
19131923
19141924 @old_positionals ("write_obsm_varm" )
1915- def write_loom (self , filename : PathLike , * , write_obsm_varm : bool = False ):
1925+ def write_loom (
1926+ self , filename : PathLike [str ] | str , * , write_obsm_varm : bool = False
1927+ ):
19161928 """\
19171929 Write `.loom`-formatted hdf5 file.
19181930
@@ -1925,10 +1937,13 @@ def write_loom(self, filename: PathLike, *, write_obsm_varm: bool = False):
19251937
19261938 write_loom (filename , self , write_obsm_varm = write_obsm_varm )
19271939
1940+ @old_positionals ("chunks" )
19281941 def write_zarr (
19291942 self ,
1930- store : MutableMapping | PathLike ,
1943+ store : StoreLike ,
1944+ * ,
19311945 chunks : tuple [int , ...] | None = None ,
1946+ convert_strings_to_categoricals : bool = True ,
19321947 ):
19331948 """\
19341949 Write a hierarchical Zarr array store.
@@ -1939,6 +1954,8 @@ def write_zarr(
19391954 The filename, a :class:`~typing.MutableMapping`, or a Zarr storage class.
19401955 chunks
19411956 Chunk shape.
1957+ convert_strings_to_categoricals
1958+ Convert string columns to categorical.
19421959 """
19431960 from ..io import write_zarr
19441961
@@ -1949,7 +1966,12 @@ def write_zarr(
19491966 "Please pass `write_zarr(adata)` instead."
19501967 )
19511968 raise ValueError (msg )
1952- write_zarr (store , self , chunks = chunks )
1969+ write_zarr (
1970+ store ,
1971+ self ,
1972+ chunks = chunks ,
1973+ convert_strings_to_categoricals = convert_strings_to_categoricals ,
1974+ )
19531975
19541976 def chunked_X (self , chunk_size : int | None = None ):
19551977 """\
@@ -2090,10 +2112,10 @@ def _infer_shape_for_axis(
20902112 return elem .shape [0 ]
20912113 for elem , id in zip ([layers , xxxm , xxxp ], ["layers" , "xxxm" , "xxxp" ]):
20922114 if elem is not None :
2093- elem = cast (Mapping , elem )
2115+ elem = cast (" Mapping" , elem )
20942116 for sub_elem in elem .values ():
20952117 if hasattr (sub_elem , "shape" ):
2096- size = cast (int , sub_elem .shape [axis if id == "layers" else 0 ])
2118+ size = cast (" int" , sub_elem .shape [axis if id == "layers" else 0 ])
20972119 return size
20982120 return None
20992121
0 commit comments