You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import pytest
from cached_property import cached_property
import time
from timeit import timeit
class PersonTwo:
def __init__(self, name: str) -> None:
self._name = name
@cached_property
def name(self):
print("Sleeping for 2 seconds...")
time.sleep(2)
return self._name # storing `*mut pyo3_ffi::PyObject` to track this variable
@name.setter
def name(self, new_name):
time.sleep(2)
print("Sleeping for 2 seconds...")
if not isinstance(new_name, str):
raise ValueError
self._name = new_name
def test___set__method_working(person_two_james, capfd):
assert person_two_james.name == 'James'
person_two_james.name = 'Thomas'
out, err = capfd.readouterr()
print(out)
# caches immediately
assert timeit(lambda: person_two_james.name, number=10) < 1
print(person_two_james.name)
assert person_two_james.name == 'Thomas' # test fails here
The test test___set__method_working failed at assert person_two_james.name == 'Thomas' as person_two_james.name='James' instead of 'Thomas'. In __set__, how should I write for the case that fset is Option::<Py<PyAny>>::None? I.e. how do I exchange the values behind two raw pointers *mut pyo3_ffi::PyObject?
This discussion was converted from issue #3006 on March 04, 2023 22:33.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am trying to write a descriptor,
cached_property
with a.setter
method.Here are the testing codes:
The test
test___set__method_working
failed atassert person_two_james.name == 'Thomas'
asperson_two_james.name
='James'
instead of'Thomas'
. In__set__
, how should I write for the case thatfset
isOption::<Py<PyAny>>::None
? I.e. how do I exchange the values behind two raw pointers*mut pyo3_ffi::PyObject
?Beta Was this translation helpful? Give feedback.
All reactions