From cdf4efcedb44b6bfd4497953749529b4d0fcd82b Mon Sep 17 00:00:00 2001 From: yashaka Date: Mon, 22 Jul 2024 23:33:49 +0300 Subject: [PATCH] [#505] REFACTOR: set actual entity on object... ... not descriptor (even via lru_cache) --- CHANGELOG.md | 2 -- selene/support/_pom.py | 24 ++++++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69379866..78f6e54f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -168,8 +168,6 @@ check vscode pylance, mypy, jetbrains qodana... ### DOING: draft Element descriptors POC? -#### TODO: decide on lru_cache vs set attr on instance... - ### Deprecated conditions - `be.present` in favor of `be.present_in_dom` diff --git a/selene/support/_pom.py b/selene/support/_pom.py index eb5b5db6..df08b7f5 100644 --- a/selene/support/_pom.py +++ b/selene/support/_pom.py @@ -21,9 +21,7 @@ # SOFTWARE. from __future__ import annotations -from functools import lru_cache - -from typing_extensions import Tuple, cast +from typing_extensions import Tuple import selene @@ -148,12 +146,17 @@ def all(self, selector: str | Tuple[str, str]) -> all_: def __set_name__(self, owner, name): self._name = name # TODO: use it - # TODO: should not we set attr on instance instead of lru_cache? :D - # set first time, then reuse :D - # current impl looks like cheating :D - @lru_cache def __get__(self, instance, owner): - return self._as_context(instance) + # # this if is not needed, because unless we define __set__ on the descriptor + # # on dot access, after we setattr on instance, python will first look in __dict__ + # # and only then in __get__ (of a non-data-descriptor + # # – the one without __set__ or __del__) + # if hasattr(instance, self._name): + # return getattr(instance, self._name) + # else: + as_context = self._as_context(instance) + setattr(instance, self._name, as_context) + return as_context # --- LocationContext --- # prev impl. was completely wrong, cause store "as_context" snapshot on self @@ -226,9 +229,10 @@ def within_browser(self): def __set_name__(self, owner, name): self._name = name # TODO: use it - @lru_cache def __get__(self, instance, owner): - return self._as_context(instance) + as_context = self._as_context(instance) + setattr(instance, self._name, as_context) + return as_context # --- FilteringContext --- # # todo: do we need it?