Skip to content

Commit

Permalink
Add all ruff checks we use in anndata (#2761)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Nov 23, 2023
1 parent 75cb4e7 commit bc349b9
Show file tree
Hide file tree
Showing 175 changed files with 2,540 additions and 1,881 deletions.
2 changes: 1 addition & 1 deletion docs/api/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Any transformation of the data matrix that is not *preprocessing*. In contrast t
:nosignatures:
:toctree: ../generated/
tl.pca
pp.pca
tl.tsne
tl.umap
tl.draw_graph
Expand Down
11 changes: 7 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from __future__ import annotations

import sys
from pathlib import Path
from datetime import datetime
from typing import Any
from pathlib import Path
from typing import TYPE_CHECKING

import matplotlib # noqa
from sphinx.application import Sphinx
from packaging.version import parse as parse_version

# Don’t use tkinter agg when importing scanpy → … → matplotlib
Expand All @@ -15,6 +15,9 @@
sys.path[:0] = [str(HERE.parent), str(HERE / "extensions")]
import scanpy # noqa

if TYPE_CHECKING:
from sphinx.application import Sphinx


# -- General configuration ------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions docs/extensions/cite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from docutils import nodes, utils

if TYPE_CHECKING:
from typing import Any
from collections.abc import Mapping, Sequence
from typing import Any

from sphinx.application import Sphinx
from docutils.parsers.rst.states import Inliner
from sphinx.application import Sphinx


def cite_role(
Expand Down
8 changes: 6 additions & 2 deletions docs/extensions/debug_docstrings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Just do the following to see the rst of a function:
# rm -f _build/doctrees/api/scanpy.<what_you_want>.doctree; DEBUG=1 make html
# rm ./_build/doctrees/api/generated/scanpy.<what you want>.doctree; DEBUG=1 make html
from __future__ import annotations

import os
from typing import TYPE_CHECKING

from sphinx.application import Sphinx
import sphinx.ext.napoleon

if TYPE_CHECKING:
from sphinx.application import Sphinx

_pd_orig = sphinx.ext.napoleon._process_docstring

Expand Down
11 changes: 7 additions & 4 deletions docs/extensions/function_images.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Images for plot functions"""
from __future__ import annotations

from pathlib import Path
from typing import List, Any
from typing import TYPE_CHECKING, Any

from sphinx.application import Sphinx
from sphinx.ext.autodoc import Options
if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.ext.autodoc import Options


def insert_function_images(
app: Sphinx, what: str, name: str, obj: Any, options: Options, lines: List[str]
app: Sphinx, what: str, name: str, obj: Any, options: Options, lines: list[str]
):
path = app.config.api_dir / f"{name}.png"
if what != "function" or not path.is_file():
Expand Down
10 changes: 6 additions & 4 deletions docs/extensions/git_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from __future__ import annotations

from functools import lru_cache

import re
import subprocess
from sphinx.application import Sphinx
from sphinx.config import Config
from functools import lru_cache
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.config import Config


def git(*args: str) -> str:
Expand Down
7 changes: 6 additions & 1 deletion docs/extensions/has_attr_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from __future__ import annotations

from inspect import get_annotations
from typing import TYPE_CHECKING

from jinja2.defaults import DEFAULT_NAMESPACE
from jinja2.utils import import_string
from sphinx.application import Sphinx

if TYPE_CHECKING:
from sphinx.application import Sphinx


def has_member(obj_path: str, attr: str) -> bool:
Expand Down
6 changes: 5 additions & 1 deletion docs/extensions/param_police.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

from sphinx.application import Sphinx
from sphinx.ext.napoleon import NumpyDocstring

if TYPE_CHECKING:
from sphinx.application import Sphinx

_format_docutils_params_orig = NumpyDocstring._format_docutils_params
param_warnings = {}
Expand Down
7 changes: 6 additions & 1 deletion docs/extensions/typed_returns.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING

from sphinx.application import Sphinx
from sphinx.ext.napoleon import NumpyDocstring

if TYPE_CHECKING:
from sphinx.application import Sphinx


def process_return(lines):
for line in lines:
Expand Down
2 changes: 1 addition & 1 deletion docs/release-notes/1.4.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Please install `scanpy==1.4.5.post3` instead of `scanpy==1.4.5`.
- webpage overhaul, ecosystem page, release notes, tutorials overhaul {pr}`960` {pr}`966` {smaller}`A Wolf`

```{warning}
- changed default `solver` in {func}`~scanpy.tl.pca` from `auto` to `arpack`
- changed default `solver` in {func}`~scanpy.pp.pca` from `auto` to `arpack`
- changed default `use_raw` in {func}`~scanpy.tl.score_genes` from `False` to `None`
```

Expand Down
26 changes: 16 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ test-full = [
"scanpy[dask-ml]",
]
doc = [
"sphinx>=5",
"sphinx>=6",
"sphinx-book-theme>=1.0.1",
"scanpydoc>=0.9.5",
"sphinx-autodoc-typehints",
"myst-parser",
"myst-nb",
"sphinx-autodoc-typehints>=1.23.0",
"myst-parser>=2",
"myst-nb>=1",
"sphinx-design",
"sphinxext-opengraph", # for nice cards when sharing on social
"sphinx-copybutton",
"nbsphinx",
"nbsphinx>=0.9",
"ipython>=7.20", # for nbsphinx code highlighting
"matplotlib!=3.6.1",
# TODO: remove necessity for being able to import doc-linked classes
Expand Down Expand Up @@ -177,14 +177,17 @@ source = [".", "**/site-packages"]

[tool.ruff]
select = [
"F", # Pyflakes
"E", # Pycodestyle errors
"W", # Pycodestyle warnings
"E", # Error detected by Pycodestyle
"F", # Errors detected by Pyflakes
"W", # Warning detected by Pycodestyle
"UP", # pyupgrade
"I", # isort
"TCH", # manage type checking blocks
"TID251", # Banned imports
"ICN", # Follow import conventions
"PTH", # Pathlib instead of os.path
]
ignore = [
# module imported but unused -> required for Scanpys API
"F401",
# line too long -> we accept long comment lines; black gets rid of long code lines
"E501",
# module level import not at top of file -> required to circumvent circular imports for Scanpys API
Expand All @@ -197,6 +200,9 @@ ignore = [
[tool.ruff.per-file-ignores]
# Do not assign a lambda expression, use a def
"scanpy/tools/_rank_genes_groups.py" = ["E731"]
[tool.ruff.isort]
known-first-party = ["scanpy"]
required-imports = ["from __future__ import annotations"]
[tool.ruff.flake8-tidy-imports.banned-api]
"pytest.importorskip".msg = "Use the “@needs” decorator/mark instead"
"pandas.api.types.is_categorical_dtype".msg = "Use isinstance(s.dtype, CategoricalDtype) instead"
Expand Down
53 changes: 44 additions & 9 deletions scanpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Single-Cell Analysis in Python."""
from __future__ import annotations

try: # See https://github.com/maresb/hatch-vcs-footgun-example
from setuptools_scm import get_version
Expand All @@ -20,25 +21,26 @@

# the actual API
# (start with settings as several tools are using it)
from ._settings import settings, Verbosity
from . import tools as tl
from . import preprocessing as pp
from . import plotting as pl
from . import datasets, logging, queries, external, get, metrics, experimental

from anndata import AnnData, concat
from anndata import (
read_h5ad,
AnnData,
concat,
read_csv,
read_excel,
read_h5ad,
read_hdf,
read_loom,
read_mtx,
read_text,
read_umi_tools,
)
from .readwrite import read, read_10x_h5, read_10x_mtx, write, read_visium

from . import datasets, experimental, external, get, logging, metrics, queries
from . import plotting as pl
from . import preprocessing as pp
from . import tools as tl
from ._settings import Verbosity, settings
from .neighbors import Neighbors
from .readwrite import read, read_10x_h5, read_10x_mtx, read_visium, write

set_figure_params = settings.set_figure_params

Expand All @@ -50,3 +52,36 @@

annotate_doc_types(sys.modules[__name__], "scanpy")
del sys, annotate_doc_types

__all__ = [
"__version__",
"AnnData",
"concat",
"read_csv",
"read_excel",
"read_h5ad",
"read_hdf",
"read_loom",
"read_mtx",
"read_text",
"read_umi_tools",
"read",
"read_10x_h5",
"read_10x_mtx",
"read_visium",
"write",
"datasets",
"experimental",
"external",
"get",
"logging",
"metrics",
"queries",
"pl",
"pp",
"tl",
"Verbosity",
"settings",
"Neighbors",
"set_figure_params",
]
2 changes: 2 additions & 0 deletions scanpy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from .cli import console_main

console_main()
1 change: 1 addition & 0 deletions scanpy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dataclasses import dataclass, field
from pathlib import Path

from packaging import version

try:
Expand Down
Loading

0 comments on commit bc349b9

Please sign in to comment.