Skip to content

Commit

Permalink
refactor: Use functools.cached_property instead
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jmgate committed Jun 10, 2024
1 parent e4a8b29 commit 1b4fe58
Showing 1 changed file with 2 additions and 29 deletions.
31 changes: 2 additions & 29 deletions staged_script/staged_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,6 @@
rich.traceback.install()


def lazy_property(func: Callable) -> property:
"""
A decorator to make it such that a property is lazily evaluated.
When the property is first accessed, the object will not yet have a
corresponding attribute, so the value will be computed by executing
:arg:`func`. Any time the property is accessed thereafter, the
value will just be retrieved from the object's corresponding
attribute.
Args:
func: The function used to compute the value of the property.
Returns:
The lazy property decorator.
"""
attr_name = f"_lazy_{func.__name__}"

@property # type: ignore[misc]
def _lazy_property(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, func(self))
return getattr(self, attr_name)

return _lazy_property # type: ignore[return-value]


class StageDuration(NamedTuple):
"""
The duration of a stage.
Expand Down Expand Up @@ -250,7 +223,7 @@ def _validate_stage_name(stage_name: str) -> None:
# Parse the command line arguments.
#

@lazy_property
@functools.cached_property
def parser(self) -> ArgumentParser:
"""
The base argument parser.
Expand All @@ -261,7 +234,7 @@ def parser(self) -> ArgumentParser:
.. code-block:: python
@lazy_property
@functools.cached_property
def parser(self) -> ArgumentParser:
ap = super().parser
ap.description = '''INSERT DESCRIPTION HERE'''
Expand Down

0 comments on commit 1b4fe58

Please sign in to comment.