Skip to content

Commit

Permalink
ruff
Browse files Browse the repository at this point in the history
Signed-off-by: Teo <[email protected]>
  • Loading branch information
teocns committed Dec 29, 2024
1 parent d53ed80 commit 8cee6fe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .singleton import conditional_singleton
from .helpers import cached_property


@conditional_singleton
class Client(metaclass=MetaClient):
def __init__(self):
Expand Down
3 changes: 2 additions & 1 deletion agentops/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class cached_property:
property cached on the instance.
See: https://github.com/AgentOps-AI/agentops/issues/612
"""

def __init__(self, func):
self.func = func
self.__doc__ = func.__doc__
Expand All @@ -191,4 +192,4 @@ def __get__(self, instance, cls=None):
return self
value = self.func(instance)
setattr(instance, self.func.__name__, value)
return value
return value
27 changes: 15 additions & 12 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,67 @@
import pytest
from agentops.helpers import cached_property


def test_cached_property():
class TestClass:
def __init__(self):
self.compute_count = 0

@cached_property
def expensive_computation(self):
self.compute_count += 1
return 42

# Create instance
obj = TestClass()

# First access should compute the value
assert obj.expensive_computation == 42
assert obj.compute_count == 1

# Second access should use cached value
assert obj.expensive_computation == 42
assert obj.compute_count == 1 # Count shouldn't increase

# Third access should still use cached value
assert obj.expensive_computation == 42
assert obj.compute_count == 1 # Count shouldn't increase


def test_cached_property_different_instances():
class TestClass:
def __init__(self):
self.compute_count = 0

@cached_property
def expensive_computation(self):
self.compute_count += 1
return id(self) # Return unique id for each instance

# Create two different instances
obj1 = TestClass()
obj2 = TestClass()

# Each instance should compute its own value
val1 = obj1.expensive_computation
val2 = obj2.expensive_computation

assert val1 != val2 # Values should be different
assert obj1.compute_count == 1
assert obj2.compute_count == 1

# Accessing again should use cached values
assert obj1.expensive_computation == val1
assert obj2.expensive_computation == val2
assert obj1.compute_count == 1 # Counts shouldn't increase
assert obj2.compute_count == 1


def test_cached_property_class_access():
class TestClass:
@cached_property
def expensive_computation(self):
return 42

# Accessing via class should return the descriptor
assert isinstance(TestClass.expensive_computation, cached_property)
assert isinstance(TestClass.expensive_computation, cached_property)

0 comments on commit 8cee6fe

Please sign in to comment.