Skip to content

Commit f6a9509

Browse files
chore(types): Type hint Utils module (#1043)
* Add types to Generic sequence implementations * Fix type hints for python 3.9 and add changelog entry * Correct type hints * Add typing to instances of Maps * Use typehints on CachedMaps * Tighten types * Restore type var for rebasing * Update and prevent problems * Correct type hints * Correct changelog version
1 parent 3dcdc60 commit f6a9509

File tree

12 files changed

+98
-65
lines changed

12 files changed

+98
-65
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ Write the date in place of the "Unreleased" in the case a new version is release
33

44
# Changelog
55

6+
## v0.1.0-b36 (Unreleased)
7+
8+
### Changed
9+
10+
- Typehint utils collection implementations
611

712
## v0.1.0-b35 (2025-08-20)
813

@@ -86,7 +91,6 @@ continuous deployment processes.
8691
- Typehinted database access methods
8792
- Explicit type conversion in SQL adapter when appending to an existing table.
8893

89-
9094
## v0.1.0-b30 (2025-07-18)
9195

9296
### Changed

tiled/_tests/test_caches.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_one_shot_cached_map():
88

99
counter = 0
1010

11-
def f():
11+
def f() -> int:
1212
nonlocal counter
1313
counter += 1
1414
return 5
@@ -36,7 +36,7 @@ def test_caching_map():
3636

3737
counter = 0
3838

39-
def f():
39+
def f() -> int:
4040
nonlocal counter
4141
counter += 1
4242
return 5

tiled/_tests/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ def text(self, s):
172172

173173
def test_oneshotcachedmap_repr_lazy_and_evaluated():
174174
# Value factories
175-
def factory1():
175+
def factory1() -> int:
176176
return 42
177177

178-
def factory2():
178+
def factory2() -> str:
179179
return "foo"
180180

181181
# All values are lazy initially
@@ -200,10 +200,10 @@ def factory2():
200200

201201
def test_cachingmap_repr_lazy_and_evaluated():
202202
# Value factories
203-
def factory1():
203+
def factory1() -> int:
204204
return 123
205205

206-
def factory2():
206+
def factory2() -> str:
207207
return "bar"
208208

209209
mapping = {"x": factory1, "y": factory2}

tiled/catalog/adapter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@
109109
StructureFamily.table: PARQUET_MIMETYPE,
110110
StructureFamily.sparse: SPARSE_BLOCKS_PARQUET_MIMETYPE,
111111
}
112-
STORAGE_ADAPTERS_BY_MIMETYPE = OneShotCachedMap(
112+
113+
# TODO: make type[Adapter] after #1047
114+
STORAGE_ADAPTERS_BY_MIMETYPE = OneShotCachedMap[str, type](
113115
{
114116
ZARR_MIMETYPE: lambda: importlib.import_module(
115117
"...adapters.zarr", __name__

tiled/client/base.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from copy import copy, deepcopy
33
from dataclasses import asdict
44
from pathlib import Path
5+
from typing import Dict, List, Union
56
from urllib.parse import parse_qs, urlparse
67

78
import json_merge_patch
@@ -17,6 +18,10 @@
1718
from .metadata_update import apply_update_patch
1819
from .utils import MSGPACK_MIME_TYPE, handle_error, retry_context
1920

21+
# TODO: Duplicated from tiled.type_aliases to prevent importing numpy
22+
# After #1407 replace AnyAdapter with the BaseClass and remove this redefinition
23+
JSON_ITEM = Union[str, int, float, bool, Dict[str, "JSON_ITEM"], List["JSON_ITEM"]]
24+
2025

2126
class MetadataRevisions:
2227
def __init__(self, context, link):
@@ -229,7 +234,7 @@ def item(self):
229234
return self._item
230235

231236
@property
232-
def metadata(self):
237+
def metadata(self) -> DictView[str, JSON_ITEM]:
233238
"Metadata about this data source."
234239
# Ensure this is immutable (at the top level) to help the user avoid
235240
# getting the wrong impression that editing this would update anything
@@ -262,12 +267,12 @@ def metadata_copy(self):
262267
] # returning as list of mutable items
263268

264269
@property
265-
def specs(self):
270+
def specs(self) -> ListView[Spec]:
266271
"List of specifications describing the structure of the metadata and/or data."
267272
return ListView([Spec(**spec) for spec in self._item["attributes"]["specs"]])
268273

269274
@property
270-
def access_blob(self):
275+
def access_blob(self) -> DictView[str, JSON_ITEM]:
271276
"Authorization information about this node, in blob form"
272277
access_blob = self._item["attributes"]["access_blob"]
273278
if access_blob is None:

tiled/client/container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Container(BaseClient, collections.abc.Mapping, IndexersMixin):
4444
STRUCTURE_CLIENTS_FROM_ENTRYPOINTS = None
4545

4646
@classmethod
47-
def _discover_entrypoints(cls, entrypoint_name):
47+
def _discover_entrypoints(cls, entrypoint_name) -> OneShotCachedMap[str, Any]:
4848
return OneShotCachedMap(
4949
{
5050
name: entrypoint.load

tiled/client/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import urllib.parse
77
import warnings
88
from pathlib import Path
9-
from typing import List
9+
from typing import List, Literal
1010
from urllib.parse import parse_qs, urlparse
1111

1212
import httpx
@@ -473,7 +473,7 @@ def from_app(
473473
return context
474474

475475
@property
476-
def tokens(self):
476+
def tokens(self) -> DictView[Literal["access_token", "refresh_token"], str]:
477477
"A view of the current access and refresh tokens."
478478
return DictView(self.http_client.auth.tokens)
479479

tiled/media_type_registration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self):
5151
for ext, media_type in self.DEFAULT_ALIASES.items():
5252
self.register_alias(ext, media_type)
5353

54-
def media_types(self, structure_family):
54+
def media_types(self, structure_family) -> DictView[str, str]:
5555
"""
5656
List the supported media types for a given structure family.
5757
"""

tiled/mimetypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
ZARR_MIMETYPE = "application/x-zarr"
1313
AWKWARD_BUFFERS_MIMETYPE = "application/x-awkward-buffers"
1414
TILED_SQL_TABLE_MIMETYPE = "application/x-tiled-sql-table"
15-
DEFAULT_ADAPTERS_BY_MIMETYPE = OneShotCachedMap(
15+
# TODO: make type[Adapter] after #1047
16+
DEFAULT_ADAPTERS_BY_MIMETYPE = OneShotCachedMap[str, type](
1617
{
1718
"image/tiff": lambda: importlib.import_module(
1819
"..adapters.tiff", __name__

tiled/query_registration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def __init__(self):
2727
self._query_type_to_name = {}
2828

2929
@property
30-
def name_to_query_type(self):
30+
def name_to_query_type(self) -> DictView[Any, Any]:
3131
return DictView(self._name_to_query_type_type)
3232

3333
@property
34-
def query_type_to_name(self):
34+
def query_type_to_name(self) -> DictView[Any, Any]:
3535
return DictView(self._query_type_to_name)
3636

3737
def register(self, name=None, overwrite=False, must_revalidate=True):

0 commit comments

Comments
 (0)