Skip to content

Commit 3d91282

Browse files
committed
refactor: Use functools.cached_property instead
This package was originally developed for Python 3.6, before `functools.cached_property` was introduced. That should be a drop-in replacement for the home-grown `lazy_property`, though, so now that we just support Python 3.8+, we should use it instead.
1 parent 0d3a5e3 commit 3d91282

File tree

2 files changed

+2
-31
lines changed

2 files changed

+2
-31
lines changed

staged_script/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
HelpFormatter,
1717
RetryStage,
1818
StageDuration,
19-
lazy_property,
2019
)
2120

2221
__all__ = [
2322
"StagedScript",
2423
"HelpFormatter",
2524
"RetryStage",
2625
"StageDuration",
27-
"lazy_property",
2826
]
2927
__version__ = "1.0.0"

staged_script/staged_script.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,6 @@
4141
rich.traceback.install()
4242

4343

44-
def lazy_property(func: Callable) -> property:
45-
"""
46-
A decorator to make it such that a property is lazily evaluated.
47-
48-
When the property is first accessed, the object will not yet have a
49-
corresponding attribute, so the value will be computed by executing
50-
:arg:`func`. Any time the property is accessed thereafter, the
51-
value will just be retrieved from the object's corresponding
52-
attribute.
53-
54-
Args:
55-
func: The function used to compute the value of the property.
56-
57-
Returns:
58-
The lazy property decorator.
59-
"""
60-
attr_name = f"_lazy_{func.__name__}"
61-
62-
@property # type: ignore[misc]
63-
def _lazy_property(self):
64-
if not hasattr(self, attr_name):
65-
setattr(self, attr_name, func(self))
66-
return getattr(self, attr_name)
67-
68-
return _lazy_property # type: ignore[return-value]
69-
70-
7144
class StageDuration(NamedTuple):
7245
"""
7346
The duration of a stage.
@@ -250,7 +223,7 @@ def _validate_stage_name(stage_name: str) -> None:
250223
# Parse the command line arguments.
251224
#
252225

253-
@lazy_property
226+
@functools.cached_property
254227
def parser(self) -> ArgumentParser:
255228
"""
256229
The base argument parser.
@@ -261,7 +234,7 @@ def parser(self) -> ArgumentParser:
261234
262235
.. code-block:: python
263236
264-
@lazy_property
237+
@functools.cached_property
265238
def parser(self) -> ArgumentParser:
266239
ap = super().parser
267240
ap.description = '''INSERT DESCRIPTION HERE'''

0 commit comments

Comments
 (0)