Skip to content

Commit 1c0a40a

Browse files
Remove branching imports for Python <3.9 (#876)
* Drop conditional imports from Typing for non-supported versions * QA: define __all__ in __init__
1 parent 902f1e8 commit 1c0a40a

File tree

10 files changed

+55
-97
lines changed

10 files changed

+55
-97
lines changed

tiled/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
from ._version import __version__, __version_tuple__ # noqa: F401
1+
from ._version import __version__, __version_tuple__
2+
3+
__all__ = [
4+
"__version__",
5+
"__version_tuple__",
6+
]

tiled/adapters/awkward_directory_container.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import sys
1+
from collections.abc import Mapping
22
from pathlib import Path
33
from typing import Any, Iterator
44

5-
if sys.version_info < (3, 9):
6-
from typing_extensions import MutableMapping
75

8-
MappingType = MutableMapping
9-
else:
10-
import collections
11-
12-
MappingType = collections.abc.MutableMapping
13-
14-
15-
class DirectoryContainer(MappingType[str, bytes]):
6+
class DirectoryContainer(Mapping[str, bytes]):
167
""" """
178

189
def __init__(self, directory: Path, form: Any):

tiled/adapters/hdf5.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import collections.abc
21
import os
3-
import sys
42
import warnings
3+
from collections.abc import Mapping
54
from typing import Any, Iterator, List, Optional, Tuple, Union
65

76
import h5py
@@ -27,17 +26,7 @@ def from_dataset(dataset: NDArray[Any]) -> ArrayAdapter:
2726
return ArrayAdapter.from_array(dataset, metadata=getattr(dataset, "attrs", {}))
2827

2928

30-
if sys.version_info < (3, 9):
31-
from typing_extensions import Mapping
32-
33-
MappingType = Mapping
34-
else:
35-
import collections
36-
37-
MappingType = collections.abc.Mapping
38-
39-
40-
class HDF5Adapter(MappingType[str, Union["HDF5Adapter", ArrayAdapter]], IndexersMixin):
29+
class HDF5Adapter(Mapping[str, Union["HDF5Adapter", ArrayAdapter]], IndexersMixin):
4130
"""
4231
Read an HDF5 file or a group within one.
4332

tiled/adapters/mapping.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import collections.abc
21
import copy
32
import itertools
43
import operator
5-
import sys
64
from collections import Counter
75
from datetime import datetime, timedelta, timezone
86
from typing import (
@@ -20,6 +18,8 @@
2018
if TYPE_CHECKING:
2119
from fastapi import APIRouter
2220

21+
from collections.abc import Iterable, Mapping
22+
2323
from ..iterviews import ItemsView, KeysView, ValuesView
2424
from ..queries import (
2525
Comparison,
@@ -43,17 +43,8 @@
4343
from .protocols import AccessPolicy, AnyAdapter
4444
from .utils import IndexersMixin
4545

46-
if sys.version_info < (3, 9):
47-
from typing_extensions import Mapping
48-
49-
MappingType = Mapping
50-
else:
51-
import collections
52-
53-
MappingType = collections.abc.Mapping
54-
5546

56-
class MapAdapter(MappingType[str, AnyAdapter], IndexersMixin):
47+
class MapAdapter(Mapping[str, AnyAdapter], IndexersMixin):
5748
"""
5849
Adapt any mapping (dictionary-like object) to Tiled.
5950
"""
@@ -537,7 +528,7 @@ def walk_string_values(tree: MapAdapter, node: Optional[Any] = None) -> Iterator
537528
elif hasattr(value, "items"):
538529
for k, v in value.items():
539530
yield from walk_string_values(value, k)
540-
elif isinstance(value, collections.abc.Iterable):
531+
elif isinstance(value, Iterable):
541532
for item in value:
542533
if isinstance(item, str):
543534
yield item
@@ -706,7 +697,7 @@ def contains(query: Any, tree: MapAdapter) -> MapAdapter:
706697
matches = {}
707698
for key, value, term in iter_child_metadata(query.key, tree):
708699
if (
709-
isinstance(term, collections.abc.Iterable)
700+
isinstance(term, Iterable)
710701
and (not isinstance(term, str))
711702
and (query.value in term)
712703
):

tiled/adapters/protocols.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import collections.abc
2-
import sys
31
from abc import abstractmethod
2+
from collections.abc import Mapping
43
from typing import Any, Dict, List, Literal, Optional, Protocol, Tuple, Union
54

65
import dask.dataframe
@@ -35,17 +34,7 @@ def specs(self) -> List[Spec]:
3534
pass
3635

3736

38-
if sys.version_info < (3, 9):
39-
from typing_extensions import Mapping
40-
41-
MappingType = Mapping
42-
else:
43-
import collections
44-
45-
MappingType = collections.abc.Mapping
46-
47-
48-
class ContainerAdapter(MappingType[str, "AnyAdapter"], BaseAdapter):
37+
class ContainerAdapter(Mapping[str, "AnyAdapter"], BaseAdapter):
4938
structure_family: Literal[StructureFamily.container]
5039

5140
@abstractmethod

tiled/adapters/xarray.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import collections.abc
21
import itertools
3-
import sys
2+
from collections.abc import Mapping
43
from typing import Any, Iterator, List, Optional
54

65
import xarray
@@ -82,17 +81,7 @@ def inlined_contents_enabled(self, depth: int) -> bool:
8281
return True
8382

8483

85-
if sys.version_info < (3, 9):
86-
from typing_extensions import Mapping
87-
88-
MappingType = Mapping
89-
else:
90-
import collections
91-
92-
MappingType = collections.abc.Mapping
93-
94-
95-
class _DatasetMap(MappingType[str, Any]):
84+
class _DatasetMap(Mapping[str, Any]):
9685
def __init__(self, dataset: Any) -> None:
9786
"""
9887

tiled/adapters/zarr.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import builtins
2-
import collections.abc
32
import os
4-
import sys
3+
from collections.abc import Mapping
54
from typing import Any, Iterator, List, Optional, Tuple, Union
65

76
import zarr.core
@@ -205,18 +204,8 @@ async def patch(
205204
return new_shape_tuple, new_chunks_tuple
206205

207206

208-
if sys.version_info < (3, 9):
209-
from typing_extensions import Mapping
210-
211-
MappingType = Mapping
212-
else:
213-
import collections
214-
215-
MappingType = collections.abc.Mapping
216-
217-
218207
class ZarrGroupAdapter(
219-
MappingType[str, Union["ArrayAdapter", "ZarrGroupAdapter"]],
208+
Mapping[str, Union["ArrayAdapter", "ZarrGroupAdapter"]],
220209
IndexersMixin,
221210
):
222211
""" """

tiled/catalog/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
from .adapter import from_uri, in_memory # noqa: F401
1+
from .adapter import from_uri, in_memory
2+
3+
__all__ = [
4+
"from_uri",
5+
"in_memory",
6+
]

tiled/client/__init__.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
from ..utils import tree # noqa: F401
2-
from .constructors import from_context, from_profile, from_uri # noqa: F401
3-
from .container import ASCENDING, DESCENDING # noqa: F401
4-
from .context import Context # noqa: F401
5-
from .logger import hide_logs, record_history, show_logs # noqa: F401
6-
from .metadata_update import DELETE_KEY # noqa: F401
1+
from ..utils import tree
2+
from .constructors import from_context, from_profile, from_uri
3+
from .container import ASCENDING, DESCENDING
4+
from .context import Context
5+
from .logger import hide_logs, record_history, show_logs
6+
from .metadata_update import DELETE_KEY
7+
8+
__all__ = [
9+
"ASCENDING",
10+
"Context",
11+
"DESCENDING",
12+
"DELETE_KEY",
13+
"from_context",
14+
"from_profile",
15+
"from_uri",
16+
"hide_logs",
17+
"record_history",
18+
"show_logs",
19+
"tree",
20+
]

tiled/client/context.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,15 @@ def __init__(
236236
# where the context starts and stops and the wrapped ASGI app.
237237
# We are abusing it slightly to enable interactive use of the
238238
# TestClient.
239-
if sys.version_info < (3, 9):
240-
import atexit
241239

242-
atexit.register(client.__exit__)
243-
else:
244-
import threading
245-
246-
# The threading module has its own (internal) atexit
247-
# mechanism that runs at thread shutdown, prior to the atexit
248-
# mechanism that runs at interpreter shutdown.
249-
# We need to intervene at that layer to close the portal, or else
250-
# we will wait forever for a thread run by the portal to join().
251-
threading._register_atexit(client.__exit__)
240+
import threading
241+
242+
# The threading module has its own (internal) atexit
243+
# mechanism that runs at thread shutdown, prior to the atexit
244+
# mechanism that runs at interpreter shutdown.
245+
# We need to intervene at that layer to close the portal, or else
246+
# we will wait forever for a thread run by the portal to join().
247+
threading._register_atexit(client.__exit__)
252248

253249
self.http_client = client
254250
self._verify = verify

0 commit comments

Comments
 (0)