Skip to content

Commit

Permalink
Backport PR scverse#3064: Import types only in if TYPE_CHECKING blo…
Browse files Browse the repository at this point in the history
…cks (scverse#3067)

Co-authored-by: Philipp A <flying-sheep@web.de>
  • Loading branch information
meeseeksmachine and flying-sheep authored May 17, 2024
1 parent 698313b commit c6d8cc7
Show file tree
Hide file tree
Showing 73 changed files with 377 additions and 222 deletions.
4 changes: 3 additions & 1 deletion benchmarks/benchmarks/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from functools import cache
from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING

import numpy as np
import pooch
Expand All @@ -11,6 +11,8 @@
import scanpy as sc

if TYPE_CHECKING:
from typing import Literal

from anndata import AnnData

Dataset = Literal["pbmc68k_reduced", "pbmc3k", "bmmc", "lung93k"]
Expand Down
4 changes: 3 additions & 1 deletion docs/extensions/function_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from __future__ import annotations

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

if TYPE_CHECKING:
from typing import Any

from sphinx.application import Sphinx
from sphinx.ext.autodoc import Options

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,6 @@ required-imports = ["from __future__ import annotations"]
"pytest.importorskip".msg = "Use the “@needs” decorator/mark instead"
"pandas.api.types.is_categorical_dtype".msg = "Use isinstance(s.dtype, CategoricalDtype) instead"
"pandas.value_counts".msg = "Use pd.Series(a).value_counts() instead"
[tool.ruff.lint.flake8-type-checking]
exempt-modules = []
strict = true
18 changes: 9 additions & 9 deletions scanpy/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
from logging import getLevelName
from pathlib import Path
from time import time
from typing import TYPE_CHECKING, Any, Literal, TextIO, Union
from typing import TYPE_CHECKING

from . import logging
from ._compat import old_positionals
from .logging import _RootLogger, _set_log_file, _set_log_level

if TYPE_CHECKING:
from collections.abc import Generator, Iterable
from typing import Any, Literal, TextIO, Union

# Collected from the print_* functions in matplotlib.backends
_Format = Union[
Literal["png", "jpg", "tif", "tiff"],
Literal["pdf", "ps", "eps", "svg", "svgz", "pgf"],
Literal["raw", "rgba"],
]

_VERBOSITY_TO_LOGLEVEL = {
"error": "ERROR",
Expand All @@ -28,14 +36,6 @@
_VERBOSITY_TO_LOGLEVEL[v] = level


# Collected from the print_* functions in matplotlib.backends
_Format = Union[
Literal["png", "jpg", "tif", "tiff"],
Literal["pdf", "ps", "eps", "svg", "svgz", "pgf"],
Literal["raw", "rgba"],
]


class Verbosity(IntEnum):
"""Logging verbosity levels."""

Expand Down
34 changes: 14 additions & 20 deletions scanpy/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,11 @@
from operator import mul, truediv
from textwrap import dedent
from types import MethodType, ModuleType
from typing import (
TYPE_CHECKING,
Any,
Callable,
Literal,
TypeVar,
Union,
overload,
)
from typing import TYPE_CHECKING, overload
from weakref import WeakSet

import numpy as np
from anndata import AnnData
from anndata import __version__ as anndata_version
from numpy.typing import NDArray
from packaging.version import Version
from scipy import sparse
from sklearn.utils import check_random_state
Expand All @@ -46,8 +36,14 @@
if TYPE_CHECKING:
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Callable, Literal, TypeVar, Union

from numpy.typing import DTypeLike
from anndata import AnnData
from numpy.typing import DTypeLike, NDArray

# e.g. https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html
# maybe in the future random.Generator
AnyRandom = Union[int, np.random.RandomState, None]


class Empty(Enum):
Expand All @@ -59,10 +55,6 @@ def __repr__(self) -> str:

_empty = Empty.token

# e.g. https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html
# maybe in the future random.Generator
AnyRandom = Union[int, np.random.RandomState, None]


class RNGIgraph:
"""
Expand Down Expand Up @@ -535,9 +527,10 @@ def update_params(
# --------------------------------------------------------------------------------


_SparseMatrix = Union[sparse.csr_matrix, sparse.csc_matrix]
_MemoryArray = Union[NDArray, _SparseMatrix]
_SupportedArray = Union[_MemoryArray, DaskArray]
if TYPE_CHECKING:
_SparseMatrix = Union[sparse.csr_matrix, sparse.csc_matrix]
_MemoryArray = Union[NDArray, _SparseMatrix]
_SupportedArray = Union[_MemoryArray, DaskArray]


@singledispatch
Expand All @@ -561,7 +554,8 @@ def _elem_mul_dask(x: DaskArray, y: DaskArray) -> DaskArray:
return da.map_blocks(elem_mul, x, y)


Scaling_T = TypeVar("Scaling_T", DaskArray, np.ndarray)
if TYPE_CHECKING:
Scaling_T = TypeVar("Scaling_T", DaskArray, np.ndarray)


def broadcast_axis(divisor: Scaling_T, axis: Literal[0, 1]) -> Scaling_T:
Expand Down
4 changes: 3 additions & 1 deletion scanpy/_utils/compute/is_constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Callable
from functools import partial, singledispatch, wraps
from numbers import Integral
from typing import TYPE_CHECKING, Literal, TypeVar, overload
from typing import TYPE_CHECKING, TypeVar, overload

import numpy as np
from numba import njit
Expand All @@ -12,6 +12,8 @@
from ..._compat import DaskArray

if TYPE_CHECKING:
from typing import Literal

from numpy.typing import NDArray

C = TypeVar("C", bound=Callable)
Expand Down
6 changes: 4 additions & 2 deletions scanpy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from functools import lru_cache, partial
from pathlib import Path
from shutil import which
from subprocess import CompletedProcess, run
from typing import TYPE_CHECKING, Any
from subprocess import run
from typing import TYPE_CHECKING

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


class _DelegatingSubparsersAction(_SubParsersAction):
Expand Down
4 changes: 3 additions & 1 deletion scanpy/datasets/_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING

import anndata as ad
import numpy as np
Expand All @@ -16,6 +16,8 @@
from ._utils import check_datasetdir_exists, filter_oldformatwarning

if TYPE_CHECKING:
from typing import Literal

from .._utils import AnyRandom

HERE = Path(__file__).parent
Expand Down
5 changes: 4 additions & 1 deletion scanpy/datasets/_ebi_expression_atlas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import BinaryIO
from typing import TYPE_CHECKING
from urllib.error import HTTPError
from urllib.request import urlopen
from zipfile import ZipFile
Expand All @@ -15,6 +15,9 @@
from ..readwrite import _download
from ._utils import check_datasetdir_exists

if TYPE_CHECKING:
from typing import BinaryIO


def _filter_boring(dataframe: pd.DataFrame) -> pd.DataFrame:
unique_vals = dataframe.apply(lambda x: len(x.unique()))
Expand Down
4 changes: 3 additions & 1 deletion scanpy/experimental/pp/_highly_variable_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings
from functools import partial
from math import sqrt
from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING

import numba as nb
import numpy as np
Expand All @@ -27,6 +27,8 @@
from scanpy.preprocessing._utils import _get_mean_var

if TYPE_CHECKING:
from typing import Literal

from numpy.typing import NDArray


Expand Down
6 changes: 4 additions & 2 deletions scanpy/experimental/pp/_normalization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from types import MappingProxyType
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING
from warnings import warn

import numpy as np
Expand All @@ -10,7 +10,6 @@

from ... import logging as logg
from ..._utils import (
Empty,
_doc_params,
_empty,
check_nonnegative_integers,
Expand All @@ -31,6 +30,9 @@

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

from ..._utils import Empty


def _pearson_residuals(X, theta, clip, check_values, copy: bool = False):
Expand Down
3 changes: 2 additions & 1 deletion scanpy/external/pl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -23,6 +23,7 @@

if TYPE_CHECKING:
from collections.abc import Collection
from typing import Any


__all__ = [
Expand Down
4 changes: 3 additions & 1 deletion scanpy/external/pp/_bbknn.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Callable
from typing import TYPE_CHECKING

from ..._compat import old_positionals
from ..._utils._doctests import doctest_needs

if TYPE_CHECKING:
from typing import Callable

from anndata import AnnData
from sklearn.metrics import DistanceMetric

Expand Down
5 changes: 3 additions & 2 deletions scanpy/external/pp/_dca.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from __future__ import annotations

from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING

from ..._compat import old_positionals

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

from anndata import AnnData

from ..._utils import AnyRandom

_AEType = Literal["zinb-conddisp", "zinb", "nb-conddisp", "nb"]
_AEType = Literal["zinb-conddisp", "zinb", "nb-conddisp", "nb"]


@old_positionals(
Expand Down
3 changes: 2 additions & 1 deletion scanpy/external/pp/_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING

from packaging.version import Version

Expand All @@ -14,6 +14,7 @@

if TYPE_CHECKING:
from collections.abc import Sequence
from typing import Literal

from anndata import AnnData

Expand Down
3 changes: 2 additions & 1 deletion scanpy/external/pp/_mnn_correct.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING

from ..._settings import settings

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

import numpy as np
import pandas as pd
Expand Down
4 changes: 3 additions & 1 deletion scanpy/external/tl/_phate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING

from ... import logging as logg
from ..._compat import old_positionals
from ..._settings import settings
from ..._utils._doctests import doctest_needs

if TYPE_CHECKING:
from typing import Literal

from anndata import AnnData

from ..._utils import AnyRandom
Expand Down
4 changes: 3 additions & 1 deletion scanpy/external/tl/_phenograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING

import pandas as pd
from anndata import AnnData
Expand All @@ -15,6 +15,8 @@
from ..._utils._doctests import doctest_needs

if TYPE_CHECKING:
from typing import Any, Literal

import numpy as np
from scipy.sparse import spmatrix

Expand Down
Loading

0 comments on commit c6d8cc7

Please sign in to comment.