Skip to content

Commit ae01dd4

Browse files
committed
Replace custom cached_property with stdlib
Since Python 3.8 there's functools.cached_property, which works the same way, but also passes type information through.
1 parent e5016a0 commit ae01dd4

File tree

9 files changed

+1592
-1588
lines changed

9 files changed

+1592
-1588
lines changed

datacube/drivers/postgis/_fields.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from collections.abc import Callable
1111
from datetime import date, datetime, timezone
1212
from decimal import Decimal
13+
from functools import cached_property
1314
from typing import Any, TypeAlias
1415

1516
from sqlalchemy import and_, cast, func
@@ -25,7 +26,7 @@
2526
from datacube.drivers.postgis._schema import Dataset, search_field_index_map
2627
from datacube.model import Range
2728
from datacube.model.fields import Expression, Field
28-
from datacube.utils import cached_property, get_doc_offset
29+
from datacube.utils import get_doc_offset
2930
from datacube.utils.dates import tz_as_utc
3031

3132
DatasetJoinArgs = tuple[FromClause] | tuple[FromClause, ColumnExpressionArgument]

datacube/index/abstract/_index.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
from abc import ABC, abstractmethod
77
from collections.abc import Iterable, Mapping, Sequence
8+
from functools import cached_property
89
from urllib.parse import ParseResult, urlparse
910

1011
from deprecat import deprecat
@@ -13,7 +14,7 @@
1314
from datacube.cfg import ODCEnvironment, ODCOptionHandler
1415
from datacube.migration import ODC2DeprecationWarning
1516
from datacube.model import Field, MetadataType
16-
from datacube.utils import cached_property, report_to_user
17+
from datacube.utils import report_to_user
1718
from datacube.utils.generic import thread_local_cache
1819

1920
from ._datasets import AbstractDatasetResource

datacube/index/abstract/_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
# Copyright (c) 2015-2025 ODC Contributors
44
# SPDX-License-Identifier: Apache-2.0
55
from collections.abc import Iterable, Sequence
6+
from functools import cached_property
67
from typing import NamedTuple
78
from uuid import UUID
89

910
from deprecat import deprecat
1011

1112
from datacube.migration import ODC2DeprecationWarning
1213
from datacube.model import Dataset, Product
13-
from datacube.utils import cached_property
1414
from datacube.utils.documents import JsonDict
1515

1616

datacube/model/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from collections import OrderedDict
1313
from collections.abc import Iterable, Iterator, Mapping, Sequence
1414
from datetime import datetime
15+
from functools import cached_property
1516
from pathlib import Path
1617
from typing import Any
1718
from urllib.parse import urlparse
@@ -22,7 +23,6 @@
2223

2324
from datacube.utils import (
2425
DocReader,
25-
cached_property,
2626
parse_time,
2727
schema_validated,
2828
uri_to_local_path,

datacube/utils/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
unsqueeze_data_array,
3131
unsqueeze_dataset,
3232
)
33-
from .py import cached_property, ignore_exceptions_if, import_function
33+
from .py import ignore_exceptions_if, import_function
3434
from .serialise import jsonify_document
3535
from .uris import get_part_from_uri, is_url, is_vsipath, mk_part_uri, uri_to_local_path
3636

@@ -41,7 +41,6 @@
4141
"NoDatesSafeLoader",
4242
"SimpleDocNav",
4343
"_readable_offset",
44-
"cached_property",
4544
"check_write_path",
4645
"gen_password",
4746
"get_doc_offset",

datacube/utils/documents.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,8 @@ class UnknownMetadataType(InvalidDocException):
282282
pass
283283

284284

285-
def get_doc_offset(offset: list[str | int], document: dict, default=None):
285+
def get_doc_offset(offset: list[str | int], document: dict[str, Any], default=None):
286286
"""
287-
:type offset: list[str]
288-
:type document: dict
289287
290288
"""
291289
return toolz.get_in(offset, document, default=default)

datacube/utils/py.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,6 @@ def ignore_exceptions_if(ignore_errors, errors: tuple[type[Exception]] | None =
4343
yield
4444

4545

46-
class cached_property: # pylint: disable=invalid-name # noqa: N801
47-
"""
48-
A property that is only computed once per instance and then replaces
49-
itself with an ordinary attribute. Deleting the attribute resets the
50-
property.
51-
52-
Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
53-
"""
54-
55-
def __init__(self, func) -> None:
56-
self.__doc__ = func.__doc__
57-
self.func = func
58-
59-
def __get__(self, obj, cls):
60-
if obj is None:
61-
return self
62-
value = obj.__dict__[self.func.__name__] = self.func(obj)
63-
return value
64-
65-
6646
def sorted_items(d, key=None, reverse: bool = False):
6747
"""Given a dictionary `d` return items: (k1, v1), (k2, v2)... sorted in
6848
ascending order according to key.

docs/about/whats_new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Next Version
3737
- Remove executable permission :pull:`1846`
3838
- Sort imports with Ruff :pull:`1858`
3939
- Fix documentation parameter names :pull:`1857`
40+
- Replace internal `@cached_property` with one from `functools` :pull:`1866`
4041

4142
v1.9.3 (15th April 2025)
4243
========================

uv.lock

Lines changed: 1583 additions & 1559 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)