Skip to content

Commit 58dbac9

Browse files
committed
fix: add class construction with handle if no other ctors are available
1 parent 52fa385 commit 58dbac9

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

lib/plugify/plugin.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,31 +294,41 @@ def __init__(self, *args, **kwargs):
294294

295295
# Check if this is handle + ownership construction
296296
# Pattern: ClassName(handle_value, Ownership.OWNED/BORROWED)
297-
if len(args) >= 2 and isinstance(args[1], Ownership):
297+
if len(args) == 2 and isinstance(args[1], Ownership):
298298
self._handle = args[0]
299299
self._owned = args[1]
300300
return
301301

302+
# Check if this is handle-only construction (assume OWNED by default)
303+
# Pattern: ClassName(handle_value)
304+
if len(args) == 1 and len(constructors) == 0:
305+
self._handle = args[0]
306+
self._owned = Ownership.OWNED
307+
return
308+
302309
# Constructor call mode
303310
if len(constructors) == 0:
304-
raise ValueError(f"{class_name} requires handle and ownership for construction")
311+
raise ValueError(
312+
f"{className} has no constructors. "
313+
f"Use: {className}(handle, Ownership.OWNED/BORROWED) or "
314+
f"{className}(handle) to wrap an existing handle."
315+
)
305316

306317
# Try constructors
307-
last_error = None
308-
for constructor in constructors:
318+
errors = []
319+
for i, constructor in enumerate(constructors):
309320
try:
310321
self._handle = constructor(*args, **kwargs)
311322
self._owned = Ownership.OWNED
312323
return # Success!
313324
except Exception as e:
314-
last_error = e
315-
continue
325+
errors.append(f"Constructor {i}: {e}")
316326

317327
# If we get here, all constructors failed
318-
if last_error:
319-
raise last_error
320-
else:
321-
raise ValueError(f"No constructor matched the arguments for {class_name}")
328+
raise ValueError(
329+
f"No constructor matched the arguments for {className}.\n"
330+
f"Tried {len(constructors)} constructor(s):\n" + "\n".join(errors)
331+
)
322332

323333
cls.__init__ = __init__
324334

0 commit comments

Comments
 (0)