Skip to content

Commit 6d6d442

Browse files
tobixenclaude
andcommitted
fix: async _handle_reverse_relations — was calling get_relatives() without await
_async_add_object_finish called _handle_reverse_relations(fix=True), which internally called self.get_relatives() — returning a coroutine in async mode instead of the dict, causing TypeError: 'coroutine' object is not iterable. Added _async_set_reverse_relation, _async_verify_reverse_relation, and _async_handle_reverse_relations; updated _async_add_object_finish to await the new async variant. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cf6dfa7 commit 6d6d442

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

caldav/calendarobjectresource.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,14 @@ def _set_reverse_relation(self, other, reltype):
448448
return
449449
other.set_relation(self, reverse_reltype, other)
450450

451+
async def _async_set_reverse_relation(self, other, reltype):
452+
"""Async version of _set_reverse_relation."""
453+
reverse_reltype = self.RELTYPE_REVERSE_MAP.get(reltype)
454+
if not reverse_reltype:
455+
logging.error("Reltype %s not supported in object uid %s" % (reltype, self.id))
456+
return
457+
await other.set_relation(self, reverse_reltype, other)
458+
451459
def _verify_reverse_relation(self, other, reltype) -> tuple:
452460
revreltype = self.RELTYPE_REVERSE_MAP[reltype]
453461
## TODO: special case FIRST/NEXT needs special handling
@@ -461,6 +469,36 @@ def _verify_reverse_relation(self, other, reltype) -> tuple:
461469
return (other, revreltype)
462470
return False
463471

472+
async def _async_verify_reverse_relation(self, other, reltype) -> tuple:
473+
"""Async version of _verify_reverse_relation."""
474+
revreltype = self.RELTYPE_REVERSE_MAP[reltype]
475+
other_relations = await other.get_relatives(fetch_objects=False, reltypes={revreltype})
476+
my_uid = self._get_uid_cheap() or str(self.icalendar_component["uid"])
477+
if my_uid not in other_relations[revreltype]:
478+
return (other, revreltype)
479+
return False
480+
481+
async def _async_handle_reverse_relations(
482+
self, verify: bool = False, fix: bool = False, pdb: bool = False
483+
) -> list:
484+
"""Async version of _handle_reverse_relations for async clients."""
485+
ret = []
486+
assert verify or fix
487+
relations = await self.get_relatives()
488+
for reltype in relations:
489+
for other in relations[reltype]:
490+
if verify:
491+
foobar = await self._async_verify_reverse_relation(other, reltype)
492+
if foobar:
493+
ret.append(foobar)
494+
if pdb:
495+
breakpoint()
496+
if fix:
497+
await self._async_set_reverse_relation(other, reltype)
498+
elif fix:
499+
await self._async_set_reverse_relation(other, reltype)
500+
return ret
501+
464502
def _handle_reverse_relations(
465503
self, verify: bool = False, fix: bool = False, pdb: bool = False
466504
) -> list:

caldav/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ async def _async_add_object_finish(self, o, no_overwrite=False, no_create=False)
848848
"""Async helper for add_object(): awaits save() then handles reverse relations."""
849849
o = await o.save(no_overwrite=no_overwrite, no_create=no_create)
850850
if o.url is not None:
851-
o._handle_reverse_relations(fix=True)
851+
await o._async_handle_reverse_relations(fix=True)
852852
return o
853853

854854
def add_event(self, *largs, **kwargs) -> "Event":

0 commit comments

Comments
 (0)