From 2ff76589b4d5f56cf3b4e85172a20e793747743f Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Mon, 6 Jan 2025 12:43:38 +0300 Subject: [PATCH] REF: Make scale.final_limit a cached property --- plotnine/scales/scale.py | 3 ++- plotnine/scales/scale_continuous.py | 3 ++- plotnine/scales/scale_discrete.py | 3 ++- plotnine/scales/scale_xy.py | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/plotnine/scales/scale.py b/plotnine/scales/scale.py index dfdf96b47..bd8e58a2a 100644 --- a/plotnine/scales/scale.py +++ b/plotnine/scales/scale.py @@ -3,6 +3,7 @@ from abc import ABC from copy import copy, deepcopy from dataclasses import dataclass, field +from functools import cached_property from typing import TYPE_CHECKING, Generic, cast import numpy as np @@ -271,7 +272,7 @@ def is_empty(self) -> bool: return True return self._range.is_empty() and self.limits is None - @property + @cached_property def final_limits(self) -> Any: raise NotImplementedError diff --git a/plotnine/scales/scale_continuous.py b/plotnine/scales/scale_continuous.py index 713b17b7f..0a2ec8ff1 100644 --- a/plotnine/scales/scale_continuous.py +++ b/plotnine/scales/scale_continuous.py @@ -2,6 +2,7 @@ from contextlib import suppress from dataclasses import dataclass +from functools import cached_property from typing import TYPE_CHECKING, Sequence from warnings import warn @@ -169,7 +170,7 @@ def _make_trans(self) -> trans: return t - @property + @cached_property def final_limits(self) -> tuple[float, float]: if self.is_empty(): return (0, 1) diff --git a/plotnine/scales/scale_discrete.py b/plotnine/scales/scale_discrete.py index 64080a116..f14c96af8 100644 --- a/plotnine/scales/scale_discrete.py +++ b/plotnine/scales/scale_discrete.py @@ -1,6 +1,7 @@ from __future__ import annotations from dataclasses import dataclass +from functools import cached_property from typing import TYPE_CHECKING, Any, Literal, Sequence import numpy as np @@ -75,7 +76,7 @@ def __post_init__(self): super().__post_init__() self._range = RangeDiscrete() - @property + @cached_property def final_limits(self) -> Sequence[str]: if self.is_empty(): return ("0", "1") diff --git a/plotnine/scales/scale_xy.py b/plotnine/scales/scale_xy.py index 73815726b..6ff3b7d4f 100644 --- a/plotnine/scales/scale_xy.py +++ b/plotnine/scales/scale_xy.py @@ -1,6 +1,7 @@ from __future__ import annotations from dataclasses import dataclass +from functools import cached_property from itertools import chain from typing import TYPE_CHECKING @@ -88,7 +89,7 @@ def map(self, x, limits=None): return list(seq) return list(x) - @property + @cached_property def final_limits(self): if self.is_empty(): return (0, 1)