Repository navigation
Conversation
|
Hi! I'm the It looks like you correctly set up a CI job that uses the autofix.ci GitHub Action, but the autofix.ci GitHub App has not been installed for this repository. This means that autofix.ci unfortunately does not have the permissions to fix this pull request. If you are the repository owner, please install the app and then restart the CI workflow! 😃 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #218 +/- ##
==========================================
- Coverage 91.28% 90.19% -1.09%
==========================================
Files 12 12
Lines 1847 1856 +9
==========================================
- Hits 1686 1674 -12
- Misses 161 182 +21
🚀 New features to boost your workflow:
|
Zethson
left a comment
There was a problem hiding this comment.
This targets the anndata main branch (hence the failing mypy and RTD in CI).
https://github.com/scverse/pertpy/blob/main/pyproject.toml#L155 should also work for you, right?
similarly, accessors were not meant to be subclassed and are not typed with that in mind.
I think that's feedback for @flying-sheep and @ilan-gold. Maybe there are quick improvements. Otherwise, no big deal.
accesses to AnnData.X or AnnData.layers. These are currently typed as Any:
Same for this.
Generally, I feel like this PR is absolutely a step into the right direction.
| stacklevel=1, | ||
| ) | ||
| d = {} | ||
| d: dict[str, Any] = {} |
There was a problem hiding this comment.
Nit: Can this Any be specified more closely? Should be possible, right?
There was a problem hiding this comment.
It doesn't seem so, at least not without blowing up the code by a lot. I can do d: dict[str, RWAble], but then mypy throws
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "AnnData | Mapping[str, AnnData | MuData] | MuData | None" [arg-type]
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Mapping[str, str] | None" [arg-type]
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Literal[0, 'obs', 1, 'var', -1, 'both']" [arg-type]
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "DataFrame | Series[Any] | Index[Any] | Mapping[str, Iterable[Any]] | None" [arg-type]
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "ndarray[tuple[Any, ...], dtype[Any]] | Mapping[str, Sequence[Any]] | None" [arg-type]
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Mapping[str, Any] | None" [arg-type]
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Mapping[str, ndarray[tuple[int], dtype[unsignedinteger[Any]]]] | None" [arg-type]
src/mudata/_core/io.py:432: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "bool" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "AnnData | Mapping[str, AnnData | MuData] | MuData | None" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Mapping[str, str] | None" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Literal[0, 'obs', 1, 'var', -1, 'both']" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "DataFrame | Series[Any] | Index[Any] | Mapping[str, Iterable[Any]] | None" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "ndarray[tuple[Any, ...], dtype[Any]] | Mapping[str, Sequence[Any]] | None" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Mapping[str, Any] | None" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "Mapping[str, ndarray[tuple[int], dtype[unsignedinteger[Any]]]] | None" [arg-type]
src/mudata/_core/io.py:471: error: Argument 1 to "MuData" has incompatible type "**dict[str, Storable | AnnData | ExtensionArray]"; expected "bool" [arg-type]
So either explicitly test / cast all the dict elements, or it stays like this.
| f = zarr.open(store, mode="r") | ||
| d = {} | ||
| f = zarr.open_group(store, mode="r") | ||
| d: dict[str, Any] = {} |
Yeah, I'll add something like this when this gets closer to merging, the point for now was to just get feedback from you and @ilan-gold .
That's why I also tagged @ilan-gold here. |
as the docs explicitly state, they are meant to be subclassed, or what do you mean? how can the typing be improved?
The the only ways to remove the Any are
option 1 would work immediately, option 2 is a little work. |
The most common issue that mypy complaines about is that the signature of Another thing that mypy complains about is Simiarly, the to/from_json methods have different return types for me, which mypy again complains about. Here I'm not sure if anything can be done at all.
So that means everyone type checking their anndata use needs to have all opt deps (or their stubs) installed? To me that seems like it sort of defeats the point of making them optional in the first place. |
|
Thanks! Yeah you and Ilan already added MuData as generic options there, seems like it needs more work. Regarding my comment above: I made cupy-stubs because of this, see scverse/anndata#2716
I wish it was different. It’s insane how easy it is to have |
warnings are because type hints target anndata main, should go away when 0.14 is released
| file = zarr.open(store, mode="w", zarr_format=zarr_format) | ||
| file = zarr.open_group(store, mode="w", zarr_format=zarr_format) | ||
| except TypeError: | ||
| # zarr_format is not supported in this version of zarr |
There was a problem hiding this comment.
Does MuData still need to support zarr-python<3? anndata doesn't https://github.com/scverse/anndata/blob/0.13.4/pyproject.toml#L47 (not for this PR, just a comment)
There was a problem hiding this comment.
mudata supports anndata>=0.11, so yes, we still need Zarr 2 support
| if backed and (isinstance(filename, IOBase) or _is_openfile(filename)): | ||
| warn("Backed mode is not supported if `filename` is an open file. Switching off backed mode.", stacklevel=1) | ||
| backed = False |
There was a problem hiding this comment.
Why should we? This is not something unrecoverable, we can still read the file.
| mods[m] = _read_mod_extras(gmods[m], read_elem(gmods[m])) | ||
| if not isinstance(mod := gmods[m], zarr.Group): | ||
| raise ValueError(f"Modality {m} must be a group.") | ||
| mods[m] = _read_mod_extras(mod, cast(AnnData, read_elem(gmods[m]))) |
There was a problem hiding this comment.
Do our read_{zarr,h5ad} methods not handle groups? I think they do. That might allow you to avoid the cast
There was a problem hiding this comment.
read_h5ad does not. read_zarr does, but it just reads everything that's there and passes that as kwargs to the anndata constructor, which now conflicts with the treedata extras (TypeError: AnnData.__init__() got an unexpected keyword argument 'extra').
| return f"A.mod[{self.mod}]" | ||
|
|
||
| def to_json(self, ref: R) -> list[str | int | None]: | ||
| def to_json(self, ref: R) -> list[str | list[str | int | None]]: # type: ignore[override] |
There was a problem hiding this comment.
So this is something we should resolve upstream?
There was a problem hiding this comment.
I don't think the to/from_json issues can be resolved at all (it's a different schema for MuData), but the other two accessor issues I mentioned are probably best addressed upstream, if we want to resolve them.
There was a problem hiding this comment.
Having looked into the other points, I don't see a simple way to make .get() generic in its data argument. Ideally, I'd like to do something like
class MetaAcc[D: AnnData | MuData, R: AdRef[str | None, D])(RefAcc[R, str | None, D]):
def get(self, data: D)but apparently passthrough of type vars to nested generics is not supported in Python. So the only solution that I can see is introducing yet another typevar throughout the class hierarchy and duplicating the types everywhere.
I'm not super invested in static typing, so I'm also fine just leaving the type: ignore comments in place.
/edit: python/typing#548 seems related
|
If there are no more comments, I'd like to merge this rather soon-ish. |
|
@ilia-kats ship it! This can always be iterated on |
Static typing and mypy pre-commit hooks. This targets the anndata main branch (hence the failing mypy and RTD in CI). There are a lot of
type: ignorecomments at the moment, the vast majority of them belongs to one of the following three cases:AnnDatabut work just fine withMuData(e.g.AlignedMappingand related)AnnData.XorAnnData.layers. These are currently typed asAny:compat. I don't know how to best solve this. The only option I can think of is to construct the type unions only with the available packages and only do the mock types when building docs. But I don't know if mypy will be able to handle the conditional logic, and this has the downside that code written by someone without opt deps may not typecheck with the opt deps.