Skip to content

Commit

Permalink
[python] Convert from Optional[Foo] to Foo | None
Browse files Browse the repository at this point in the history
  • Loading branch information
mojaveazure committed Dec 17, 2024
1 parent 1fa2666 commit 5a411de
Show file tree
Hide file tree
Showing 23 changed files with 270 additions and 236 deletions.
27 changes: 13 additions & 14 deletions apis/python/src/tiledbsoma/_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
Callable,
ClassVar,
Dict,
Optional,
Tuple,
Type,
TypeVar,
Expand Down Expand Up @@ -67,9 +66,9 @@ def create(
cls,
uri: str,
*,
platform_config: Optional[options.PlatformConfig] = None,
context: Optional[SOMATileDBContext] = None,
tiledb_timestamp: Optional[OpenTimestamp] = None,
platform_config: options.PlatformConfig | None = None,
context: SOMATileDBContext | None = None,
tiledb_timestamp: OpenTimestamp | None = None,
) -> Self:
"""Creates and opens a new SOMA collection in storage.
Expand Down Expand Up @@ -147,8 +146,8 @@ def add_new_collection(
key: str,
kind: None = None,
*,
uri: Optional[str] = ...,
platform_config: Optional[options.PlatformConfig] = ...,
uri: str | None = ...,
platform_config: options.PlatformConfig | None = ...,
) -> "Collection[AnySOMAObject]": ...

@overload
Expand All @@ -157,17 +156,17 @@ def add_new_collection(
key: str,
kind: Type[_Coll],
*,
uri: Optional[str] = ...,
platform_config: Optional[options.PlatformConfig] = ...,
uri: str | None = ...,
platform_config: options.PlatformConfig | None = ...,
) -> _Coll: ...

def add_new_collection(
self,
key: str,
kind: Optional[Type[CollectionBase]] = None, # type: ignore[type-arg]
kind: Type[CollectionBase] | None = None, # type: ignore[type-arg]
*,
uri: Optional[str] = None,
platform_config: Optional[options.PlatformConfig] = None,
uri: str | None = None,
platform_config: options.PlatformConfig | None = None,
) -> AnyTileDBCollection:
"""Adds a new sub-collection to this collection.
Expand Down Expand Up @@ -226,7 +225,7 @@ def add_new_collection(
DataFrame.create, exclude=("context", "tiledb_timestamp")
)
def add_new_dataframe(
self, key: str, *, uri: Optional[str] = None, **kwargs: Any
self, key: str, *, uri: str | None = None, **kwargs: Any
) -> DataFrame:
"""Adds a new DataFrame to this collection.
Expand Down Expand Up @@ -269,7 +268,7 @@ def add_new_dataframe(

@_funcs.forwards_kwargs_to(NDArray.create, exclude=("context", "tiledb_timestamp"))
def _add_new_ndarray(
self, cls: Type[_NDArr], key: str, *, uri: Optional[str] = None, **kwargs: Any
self, cls: Type[_NDArr], key: str, *, uri: str | None = None, **kwargs: Any
) -> _NDArr:
"""Internal implementation of common NDArray-adding operations."""
return self._add_new_element(
Expand Down Expand Up @@ -361,7 +360,7 @@ def _add_new_element(
key: str,
kind: Type[_TDBO],
factory: Callable[[str], _TDBO],
user_uri: Optional[str],
user_uri: str | None,
) -> _TDBO:
"""Handles the common parts of adding new elements.
Expand Down
12 changes: 7 additions & 5 deletions apis/python/src/tiledbsoma/_common_nd_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

"""Common code shared by both NDArray implementations."""

from typing import Optional, Sequence, Tuple, Union, cast
from __future__ import annotations

from typing import Sequence, Tuple, Union, cast

import pyarrow as pa
import somacore
Expand All @@ -32,9 +34,9 @@ def create(
*,
type: pa.DataType,
shape: Sequence[Union[int, None]],
platform_config: Optional[options.PlatformConfig] = None,
context: Optional[SOMATileDBContext] = None,
tiledb_timestamp: Optional[OpenTimestamp] = None,
platform_config: options.PlatformConfig | None = None,
context: SOMATileDBContext | None = None,
tiledb_timestamp: OpenTimestamp | None = None,
) -> Self:
"""Creates a SOMA ``NDArray`` at the given URI.
Expand Down Expand Up @@ -154,7 +156,7 @@ def tiledbsoma_has_upgraded_shape(self) -> bool:
def _dim_capacity_and_extent(
cls,
dim_name: str,
dim_shape: Optional[int],
dim_shape: int | None,
ndim: int,
create_options: TileDBCreateOptions,
) -> Tuple[int, int]:
Expand Down
26 changes: 14 additions & 12 deletions apis/python/src/tiledbsoma/_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"""
Implementation of a SOMA DataFrame
"""

from __future__ import annotations

import inspect
from typing import (
Any,
Dict,
List,
Optional,
Sequence,
Tuple,
Union,
Expand Down Expand Up @@ -151,10 +153,10 @@ def create(
*,
schema: pa.Schema,
index_column_names: Sequence[str] = (SOMA_JOINID,),
domain: Optional[Domain] = None,
platform_config: Optional[options.PlatformConfig] = None,
context: Optional[SOMATileDBContext] = None,
tiledb_timestamp: Optional[OpenTimestamp] = None,
domain: Domain | None = None,
platform_config: options.PlatformConfig | None = None,
context: SOMATileDBContext | None = None,
tiledb_timestamp: OpenTimestamp | None = None,
) -> "DataFrame":
"""Creates the data structure on disk/S3/cloud.
Expand Down Expand Up @@ -407,7 +409,7 @@ def count(self) -> int:
return cast(DataFrameWrapper, self._handle).count

@property
def _maybe_soma_joinid_shape(self) -> Optional[int]:
def _maybe_soma_joinid_shape(self) -> int | None:
"""An internal helper method that returns the shape
value along the ``soma_joinid`` index column, if the ``DataFrame
has one, else ``None``.
Expand All @@ -419,7 +421,7 @@ def _maybe_soma_joinid_shape(self) -> Optional[int]:
return self._handle.maybe_soma_joinid_shape

@property
def _maybe_soma_joinid_maxshape(self) -> Optional[int]:
def _maybe_soma_joinid_maxshape(self) -> int | None:
"""An internal helper method that returns the maxshape
value along the ``soma_joinid`` index column, if the ``DataFrame
has one, else ``None``.
Expand Down Expand Up @@ -657,13 +659,13 @@ def __len__(self) -> int:
def read(
self,
coords: options.SparseDFCoords = (),
column_names: Optional[Sequence[str]] = None,
column_names: Sequence[str] | None = None,
*,
result_order: options.ResultOrderStr = options.ResultOrder.AUTO,
value_filter: Optional[str] = None,
value_filter: str | None = None,
batch_size: options.BatchSize = _UNBATCHED,
partitions: Optional[options.ReadPartitions] = None,
platform_config: Optional[options.PlatformConfig] = None,
partitions: options.ReadPartitions | None = None,
platform_config: options.PlatformConfig | None = None,
) -> TableReadIter:
"""Reads a user-defined subset of data, addressed by the dataframe indexing columns,
optionally filtered, and return results as one or more `Arrow tables <https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`_.
Expand Down Expand Up @@ -732,7 +734,7 @@ def read(
)

def write(
self, values: pa.Table, platform_config: Optional[options.PlatformConfig] = None
self, values: pa.Table, platform_config: options.PlatformConfig | None = None
) -> Self:
"""Writes an `Arrow table <https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`_
to the persistent object. As duplicate index values are not allowed, index values already
Expand Down
18 changes: 10 additions & 8 deletions apis/python/src/tiledbsoma/_dense_nd_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
Implementation of SOMA DenseNDArray.
"""

from typing import List, Optional, Sequence, Tuple, Union
from __future__ import annotations

from typing import List, Sequence, Tuple, Union

import numpy as np
import pyarrow as pa
Expand Down Expand Up @@ -93,9 +95,9 @@ def create(
*,
type: pa.DataType,
shape: Sequence[Union[int, None]],
platform_config: Optional[options.PlatformConfig] = None,
context: Optional[SOMATileDBContext] = None,
tiledb_timestamp: Optional[OpenTimestamp] = None,
platform_config: options.PlatformConfig | None = None,
context: SOMATileDBContext | None = None,
tiledb_timestamp: OpenTimestamp | None = None,
) -> Self:
context = _validate_soma_tiledb_context(context)

Expand Down Expand Up @@ -173,8 +175,8 @@ def read(
coords: options.DenseNDCoords = (),
*,
result_order: options.ResultOrderStr = somacore.ResultOrder.ROW_MAJOR,
partitions: Optional[options.ReadPartitions] = None,
platform_config: Optional[options.PlatformConfig] = None,
partitions: options.ReadPartitions | None = None,
platform_config: options.PlatformConfig | None = None,
) -> pa.Tensor:
"""Reads a user-defined dense slice of the array and return as an Arrow ``Tensor``.
Expand Down Expand Up @@ -262,7 +264,7 @@ def write(
coords: options.DenseNDCoords,
values: pa.Tensor,
*,
platform_config: Optional[options.PlatformConfig] = None,
platform_config: options.PlatformConfig | None = None,
) -> Self:
"""Writes a subarray, defined by ``coords`` and ``values``. Will overwrite existing
values in the array.
Expand Down Expand Up @@ -326,7 +328,7 @@ def write(
def _dim_capacity_and_extent(
cls,
dim_name: str,
dim_shape: Optional[int],
dim_shape: int | None,
ndim: int,
create_options: TileDBCreateOptions,
) -> Tuple[int, int]:
Expand Down
6 changes: 4 additions & 2 deletions apis/python/src/tiledbsoma/_eager_iter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from concurrent import futures
from typing import Iterator, Optional, TypeVar
from typing import Iterator, TypeVar

_T = TypeVar("_T")

Expand All @@ -8,7 +10,7 @@ class EagerIterator(Iterator[_T]):
def __init__(
self,
iterator: Iterator[_T],
pool: Optional[futures.Executor] = None,
pool: futures.Executor | None = None,
):
super().__init__()
self.iterator = iterator
Expand Down
8 changes: 5 additions & 3 deletions apis/python/src/tiledbsoma/_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

"""Implementation of a SOMA Experiment.
"""

from __future__ import annotations

import functools
from typing import Optional

from somacore import experiment, query

Expand Down Expand Up @@ -81,8 +83,8 @@ def axis_query( # type: ignore
self,
measurement_name: str,
*,
obs_query: Optional[query.AxisQuery] = None,
var_query: Optional[query.AxisQuery] = None,
obs_query: query.AxisQuery | None = None,
var_query: query.AxisQuery | None = None,
) -> ExperimentAxisQuery:
"""Creates an axis query over this experiment.
Lifecycle: Maturing.
Expand Down
21 changes: 11 additions & 10 deletions apis/python/src/tiledbsoma/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
Collection.
"""

from __future__ import annotations

from typing import (
Callable,
Dict,
Optional,
Type,
TypeVar,
Union,
Expand Down Expand Up @@ -55,9 +56,9 @@ def open(
uri: str,
mode: options.OpenMode = ...,
*,
soma_type: Optional[str] = None,
context: Optional[SOMATileDBContext] = None,
tiledb_timestamp: Optional[OpenTimestamp] = None,
soma_type: str | None = None,
context: SOMATileDBContext | None = None,
tiledb_timestamp: OpenTimestamp | None = None,
) -> AnySOMAObject: ...


Expand All @@ -67,8 +68,8 @@ def open(
mode: options.OpenMode,
*,
soma_type: Type[_Obj],
context: Optional[SOMATileDBContext] = None,
tiledb_timestamp: Optional[OpenTimestamp] = None,
context: SOMATileDBContext | None = None,
tiledb_timestamp: OpenTimestamp | None = None,
) -> _Obj: ...


Expand All @@ -78,8 +79,8 @@ def open(
mode: options.OpenMode = "r",
*,
soma_type: Union[Type[SOMAObject], str, None] = None, # type: ignore[type-arg]
context: Optional[SOMATileDBContext] = None,
tiledb_timestamp: Optional[OpenTimestamp] = None,
context: SOMATileDBContext | None = None,
tiledb_timestamp: OpenTimestamp | None = None,
) -> AnySOMAObject:
"""Opens a TileDB SOMA object.
Expand Down Expand Up @@ -145,12 +146,12 @@ def open(

def _open_internal(
opener: Callable[
[str, options.OpenMode, SOMATileDBContext, Optional[OpenTimestamp]], _Wrapper
[str, options.OpenMode, SOMATileDBContext, OpenTimestamp | None], _Wrapper
],
uri: str,
mode: options.OpenMode,
context: SOMATileDBContext,
timestamp: Optional[OpenTimestamp],
timestamp: OpenTimestamp | None,
) -> SOMAObject[_Wrapper]:
"""Lower-level open function for internal use only."""
handle = opener(uri, mode, context, timestamp)
Expand Down
Loading

0 comments on commit 5a411de

Please sign in to comment.