Skip to content

Commit

Permalink
style(python): update type annotations for Python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
jbms committed Feb 21, 2025
1 parent 9f18e2d commit da5feab
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 66 deletions.
14 changes: 7 additions & 7 deletions python/neuroglancer/coordinate_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import re
from collections.abc import Sequence
from typing import Any, NamedTuple, Optional, Union
from typing import Any, NamedTuple

import numpy as np
import numpy.typing
Expand Down Expand Up @@ -166,7 +166,7 @@ class DimensionScale(NamedTuple):
unit: str = ""
"""Units of `.scale`."""

coordinate_array: Optional[CoordinateArray] = None
coordinate_array: CoordinateArray | None = None
"""Coordinate array for the dimension."""

@staticmethod
Expand Down Expand Up @@ -208,7 +208,7 @@ class CoordinateSpace:
Length is equal to `.rank`.
"""

coordinate_arrays: tuple[Optional[CoordinateArray], ...]
coordinate_arrays: tuple[CoordinateArray | None, ...]
"""Coordinate array for each dimension.
Length is equal to `.rank`.
Expand All @@ -217,10 +217,10 @@ class CoordinateSpace:
def __init__(
self,
json: Any = None,
names: Optional[Sequence[str]] = None,
scales: Optional[Sequence[float]] = None,
units: Optional[Union[str, Sequence[str]]] = None,
coordinate_arrays: Optional[Sequence[Optional[CoordinateArray]]] = None,
names: Sequence[str] | None = None,
scales: Sequence[float] | None = None,
units: str | Sequence[str] | None = None,
coordinate_arrays: Sequence[CoordinateArray | None] | None = None,
):
"""
Constructs a coordinate space.
Expand Down
3 changes: 1 addition & 2 deletions python/neuroglancer/equivalence_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import collections
import copy
from collections.abc import ItemsView, Iterator, KeysView
from typing import Optional


class EquivalenceMap:
Expand Down Expand Up @@ -91,7 +90,7 @@ def clear(self):
self._prev_next.clear()
self._min_values.clear()

def union(self, *elements: int) -> Optional[int]:
def union(self, *elements: int) -> int | None:
"""Unions the equivalence classes containing the specified elements."""
if self._readonly:
raise AttributeError
Expand Down
2 changes: 1 addition & 1 deletion python/neuroglancer/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def json_encoder_default(obj):
return float(obj)
elif isinstance(obj, np.ndarray):
return list(obj)
elif isinstance(obj, (set, frozenset)):
elif isinstance(obj, set | frozenset):
return list(obj)
raise TypeError

Expand Down
32 changes: 18 additions & 14 deletions python/neuroglancer/json_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@
import inspect
import numbers
import threading
from collections.abc import ItemsView, Iterable, Iterator, KeysView, ValuesView
from collections.abc import (
Callable,
ItemsView,
Iterable,
Iterator,
KeysView,
ValuesView,
)
from typing import (
Any,
Callable,
ClassVar,
Generic,
Literal,
Optional,
TypeVar,
Union,
cast,
overload,
)
Expand Down Expand Up @@ -250,7 +254,7 @@ def modified_validator(value, **kwargs):
modified_wrapper.supports_validation = modified_validator

if default_value is None:
_map_type_annotation(modified_wrapper, wrapper, lambda t: Optional[t])
_map_type_annotation(modified_wrapper, wrapper, lambda t: t | None)
else:
_map_type_annotation(modified_wrapper, wrapper, lambda t: t)

Expand Down Expand Up @@ -310,13 +314,13 @@ def items(self) -> ItemsView[K, V]:
return _MapItemsView(self)

@overload
def get(self, key: K) -> Optional[V]: ...
def get(self, key: K) -> V | None: ...

@overload
def get(self, key: K, default: V) -> V: ...

@overload
def get(self, key: K, default: T) -> Union[V, T]: ...
def get(self, key: K, default: T) -> V | T: ...

def get(self, key: K, default=None):
"""Returns the mapped value, or the specified default."""
Expand Down Expand Up @@ -450,7 +454,7 @@ class _Map(Map):


def typed_set(wrapped_type: Callable[[Any], T]):
def wrapper(x, _readonly=False) -> Union[set[T], frozenset[T]]:
def wrapper(x, _readonly=False) -> set[T] | frozenset[T]:
set_type = frozenset if _readonly else set
kwargs: dict[str, Any] = dict()
if hasattr(wrapped_type, "supports_readonly"):
Expand Down Expand Up @@ -490,7 +494,7 @@ class List(Generic[T]):
def __init__(self, json_data=None, _readonly=False):
if json_data is None:
json_data = []
if not isinstance(json_data, (list, tuple, np.ndarray)):
if not isinstance(json_data, list | tuple | np.ndarray):
raise ValueError
self._readonly = _readonly
validator = type(self)._validator
Expand All @@ -516,7 +520,7 @@ def __setitem__(self, key: int, value: T): ...
@overload
def __setitem__(self, key: slice, value: Iterable[T]): ...

def __setitem__(self, key: Union[int, slice], value: Union[T, Iterable[T]]):
def __setitem__(self, key: int | slice, value: T | Iterable[T]):
"""Assigns to the specified index or slice."""
if self._readonly:
raise AttributeError
Expand Down Expand Up @@ -575,18 +579,18 @@ class _List(List):


def number_or_string(value):
if not isinstance(value, numbers.Real) and not isinstance(value, str):
if not isinstance(value, numbers.Real | str):
raise TypeError
return value


_set_type_annotation(number_or_string, Union[numbers.Real, str])
_set_type_annotation(number_or_string, numbers.Real | str)


def bool_or_string(value):
if not isinstance(value, (bool, str)):
if not isinstance(value, bool | str):
raise TypeError
return value


_set_type_annotation(bool_or_string, Union[bool, str])
_set_type_annotation(bool_or_string, bool | str)
3 changes: 2 additions & 1 deletion python/neuroglancer/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.

import time
from typing import Callable, TypeVar
from collections.abc import Callable
from typing import TypeVar

T = TypeVar("T")

Expand Down
8 changes: 4 additions & 4 deletions python/neuroglancer/tool/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
import os
import threading
import time
from collections.abc import Iterator
from typing import Callable, NamedTuple, Optional
from collections.abc import Callable, Iterator
from typing import NamedTuple

import numpy as np
import PIL
Expand Down Expand Up @@ -293,8 +293,8 @@ def capture_screenshots_in_parallel(
request_iter: Iterator[CaptureScreenshotRequest],
refresh_browser_timeout: float,
num_to_prefetch: int,
total_requests: Optional[int] = None,
buffer_size: Optional[int] = None,
total_requests: int | None = None,
buffer_size: int | None = None,
):
if buffer_size is None:
if total_requests is None:
Expand Down
8 changes: 4 additions & 4 deletions python/neuroglancer/trackable_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def transform_state_function(new_state):

def set_state(
self,
new_state: typing.Union[typing.Any, State],
generation: typing.Optional[Generation] = None,
existing_generation: typing.Optional[Generation] = None,
new_state: typing.Any | State,
generation: Generation | None = None,
existing_generation: Generation | None = None,
) -> Generation:
"""
Sets a new value.
Expand Down Expand Up @@ -173,7 +173,7 @@ def txn(self, overwrite: bool = False, lock: bool = True):
if lock:
self._lock.acquire()
try:
existing_generation: typing.Optional[Generation]
existing_generation: Generation | None
new_state, existing_generation = self.state_and_generation
new_state = copy.deepcopy(new_state)
yield new_state
Expand Down
7 changes: 3 additions & 4 deletions python/neuroglancer/viewer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import re
import threading
from concurrent.futures import Future
from typing import Optional

import numpy as np

Expand Down Expand Up @@ -190,7 +189,7 @@ def volume_info(
self,
layer: str,
*,
dimensions: Optional[coordinate_space.CoordinateSpace] = None,
dimensions: coordinate_space.CoordinateSpace | None = None,
) -> "Future[ts.TensorStore]":
request_id = make_random_token()
future: Future[ts.TensorStore] = Future()
Expand All @@ -217,7 +216,7 @@ def volume(
self,
layer: str,
*,
dimensions: Optional[coordinate_space.CoordinateSpace] = None,
dimensions: coordinate_space.CoordinateSpace | None = None,
) -> "Future[ts.TensorStore]":
future: Future[ts.TensorStore] = Future()

Expand Down Expand Up @@ -377,7 +376,7 @@ def _transform_viewer_state(self, new_state):
new_state = new_state.to_json()

def encoder(x):
if isinstance(x, (local_volume.LocalVolume, skeleton.SkeletonSource)):
if isinstance(x, local_volume.LocalVolume | skeleton.SkeletonSource):
return self.volume_manager.register_volume(x)
return json_encoder_default(x)

Expand Down
8 changes: 3 additions & 5 deletions python/neuroglancer/viewer_config_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def export(obj):
@export
class SegmentIdMapEntry(typing.NamedTuple):
key: int
value: typing.Optional[int] = None
label: typing.Optional[str] = None
value: int | None = None
label: str | None = None


@export
Expand All @@ -71,9 +71,7 @@ def layer_selected_value(x):
return None


_set_type_annotation(
layer_selected_value, typing.Union[None, numbers.Number, SegmentIdMapEntry]
)
_set_type_annotation(layer_selected_value, None | numbers.Number | SegmentIdMapEntry)


@export
Expand Down
26 changes: 12 additions & 14 deletions python/neuroglancer/viewer_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ class CoordinateSpaceTransform(JsonObjectWrapper):


def data_source_url(x):
if isinstance(x, (local_volume.LocalVolume, skeleton.SkeletonSource)):
if isinstance(x, local_volume.LocalVolume | skeleton.SkeletonSource):
return x
if not isinstance(x, str):
raise TypeError
Expand All @@ -488,7 +488,7 @@ class LayerDataSource(JsonObjectWrapper):

def __init__(self, json_data=None, *args, **kwargs):
if isinstance(json_data, str) or isinstance(
json_data, (local_volume.LocalVolume, skeleton.SkeletonSource)
json_data, local_volume.LocalVolume | skeleton.SkeletonSource
):
json_data = {"url": json_data}
super().__init__(json_data, *args, **kwargs)
Expand All @@ -514,13 +514,11 @@ class LayerDataSources(_LayerDataSourcesBase):
def __init__(self, json_data=None, **kwargs):
if isinstance(
json_data,
(
LayerDataSource,
str,
local_volume.LocalVolume,
skeleton.SkeletonSource,
dict,
),
LayerDataSource
| str
| local_volume.LocalVolume
| skeleton.SkeletonSource
| dict,
):
json_data = [json_data]
elif isinstance(json_data, LayerDataSources):
Expand Down Expand Up @@ -576,7 +574,7 @@ def _shader_control_parameters(v, _readonly=False):

_set_type_annotation(
_shader_control_parameters,
typing.Union[numbers.Number, str, InvlerpParameters, TransferFunctionParameters],
numbers.Number | str | InvlerpParameters | TransferFunctionParameters,
)


Expand Down Expand Up @@ -751,10 +749,10 @@ def add(self, segment_id: int) -> None:
self.setdefault(segment_id, True)

@typing.overload
def get(self, segment_id: int) -> typing.Optional[bool]: ...
def get(self, segment_id: int) -> bool | None: ...

@typing.overload
def get(self, segment_id: int, default: T) -> typing.Union[bool, T]: ...
def get(self, segment_id: int, default: T) -> bool | T: ...

def get(self, segment_id: int, default=None):
"""Checks if a segment is visible.
Expand Down Expand Up @@ -825,7 +823,7 @@ def update( # type: ignore[override]
other: typing.Union[
"StarredSegments",
collections.abc.MutableMapping[int, bool],
typing.Iterable[typing.Union[int, str, tuple[int, bool]]],
typing.Iterable[int | str | tuple[int, bool]],
],
):
"""Merges in additional starred segments."""
Expand Down Expand Up @@ -1608,7 +1606,7 @@ def layout_specification(x, _readonly=False):
x = "4panel"
if isinstance(x, str):
x = {"type": str(x)}
if isinstance(x, (StackLayout, LayerGroupViewer, DataPanelLayout)):
if isinstance(x, StackLayout | LayerGroupViewer | DataPanelLayout):
return type(x)(x.to_json(), _readonly=_readonly)
if not isinstance(x, dict):
raise ValueError
Expand Down
Loading

0 comments on commit da5feab

Please sign in to comment.