Skip to content

Commit f9fd5ea

Browse files
add type hints
1 parent 3aa510f commit f9fd5ea

File tree

2 files changed

+13
-35
lines changed

2 files changed

+13
-35
lines changed

tests/profiling/collector/test_asyncio.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,30 @@ def teardown_method(self):
5151
except Exception as e:
5252
print("Error while deleting file: ", e)
5353

54-
def test_subclassing_wrapped_lock(self):
55-
"""Test that subclassing asyncio.Lock works when profiling is active.
54+
def test_subclassing_wrapped_lock(self) -> None:
55+
"""Test that subclassing of a wrapped lock type when profiling is active."""
56+
from typing import Optional
5657

57-
Regression test for a bug reported by neo4j users where their library defines:
58-
class AsyncRLock(asyncio.Lock): ...
59-
60-
With profiling enabled, this would fail with:
61-
TypeError: _LockAllocatorWrapper.__init__() takes 2 positional arguments but 4 were given
62-
63-
The fix implements __mro_entries__ on _LockAllocatorWrapper (PEP 560).
64-
"""
6558
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper
6659

6760
with collector_asyncio.AsyncioLockCollector(capture_pct=100):
68-
# Verify the wrapper is in place
6961
assert isinstance(asyncio.Lock, _LockAllocatorWrapper)
7062

7163
# This should NOT raise TypeError
72-
class AsyncRLock(asyncio.Lock):
73-
"""A custom lock that extends asyncio.Lock (like neo4j does)."""
74-
64+
class CustomLock(asyncio.Lock): # type: ignore[misc]
7565
def __init__(self) -> None:
7666
super().__init__()
77-
self._owner = None
78-
self._count = 0
67+
self._owner: Optional[int] = None
68+
self._count: int = 0
7969

80-
# Verify the subclass can be instantiated
81-
custom_lock = AsyncRLock()
70+
# Verify subclassing and functionality
71+
custom_lock: CustomLock = CustomLock()
8272
assert hasattr(custom_lock, "_owner")
8373
assert hasattr(custom_lock, "_count")
8474
assert custom_lock._owner is None
8575
assert custom_lock._count == 0
8676

87-
# Verify async lock functionality still works
88-
async def test_async_lock():
77+
async def test_async_lock() -> None:
8978
await custom_lock.acquire()
9079
assert custom_lock.locked()
9180
custom_lock.release()

tests/profiling/collector/test_threading.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -565,34 +565,23 @@ def __init__(self, lock_class: LockTypeClass) -> None:
565565
Foobar(self.lock_class)
566566

567567
def test_subclassing_wrapped_lock(self) -> None:
568-
"""Test that subclassing a wrapped lock type works correctly.
569-
570-
Regression test for a bug where third-party libraries (e.g. neo4j) that subclass
571-
asyncio.Lock would fail with:
572-
TypeError: _LockAllocatorWrapper.__init__() takes 2 positional arguments but 4 were given
573-
574-
The fix implements __mro_entries__ on _LockAllocatorWrapper (PEP 560).
575-
"""
568+
"""Test that subclassing of a wrapped lock type when profiling is active."""
576569
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper
577570

578571
with self.collector_class():
579-
# Verify the wrapper is in place
580572
assert isinstance(self.lock_class, _LockAllocatorWrapper)
581573

582574
# This should NOT raise TypeError
583575
class CustomLock(self.lock_class): # type: ignore[misc]
584-
"""A custom lock that extends the wrapped lock type."""
585-
586576
def __init__(self) -> None:
587577
super().__init__()
588-
self.custom_attr = "test"
578+
self.custom_attr: str = "test"
589579

590-
# Verify the subclass works correctly
591-
custom_lock = CustomLock()
580+
# Verify subclassing and functionality
581+
custom_lock: CustomLock = CustomLock()
592582
assert hasattr(custom_lock, "custom_attr")
593583
assert custom_lock.custom_attr == "test"
594584

595-
# Verify lock functionality still works
596585
assert custom_lock.acquire()
597586
custom_lock.release()
598587

0 commit comments

Comments
 (0)