Refactor/move typing to ty#223
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Summary of ChangesHello @noamteyssier, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on improving the type safety and clarity of the Python codebase by integrating the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Ignored Files
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request focuses on improving type safety across the codebase, primarily by adding typing.cast to guide the static type checker and addressing various typing-related issues. Most changes successfully enhance type correctness, such as removing unnecessary type: ignore comments and correcting return types. However, a new type: ignore added in src/cell_eval/utils.py masks a potential runtime bug, which I've detailed in a specific comment. Overall, it's a good step towards a more type-safe codebase, with one critical point to address.
| elif adata.isview: | ||
| frac, _ = np.modf(adata.X.toarray()) | ||
| frac, _ = np.modf(adata.X.toarray()) # type: ignore[unresolved-attribute] |
There was a problem hiding this comment.
Adding type: ignore here hides a potential AttributeError at runtime. The adata.isview check doesn't guarantee that adata.X is a sparse matrix with a .toarray() method. If adata is a view of an AnnData object with a dense X matrix, adata.X will be a numpy.ndarray and won't have .toarray(), which would cause a crash.
A more robust approach would be to check for sparsity directly using scipy.sparse.issparse() and handle dense and sparse cases accordingly. This would also be more efficient for sparse matrices than calling .toarray().
Consider refactoring this logic to check for sparsity first, which is more reliable than checking isview:
if adata.X is None:
raise ValueError("adata.X is None")
if sp.issparse(adata.X):
frac, _ = np.modf(adata.X.data)
else:
frac, _ = np.modf(adata.X)This simplified logic correctly handles both sparse and dense matrices, regardless of whether adata is a view.
No description provided.