Skip to content

Commit

Permalink
[#505] REFACTOR: set actual entity on object...
Browse files Browse the repository at this point in the history
... not descriptor (even via lru_cache)
  • Loading branch information
yashaka committed Jul 22, 2024
1 parent a339a5e commit cdf4efc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
24 changes: 14 additions & 10 deletions selene/support/_pom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit cdf4efc

Please sign in to comment.