Skip to content

Commit ce20815

Browse files
committed
Use Callable type hints for InstanceHP - requries fix to Traitlets : ipython/traitlets#918.
1 parent e9511b8 commit ce20815

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

src/menubox/instance.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def class_init(self, cls, name):
9898
raise TypeError(msg)
9999
return super().class_init(cls, name)
100100

101-
def __init__(self, klass: type[T] | str, *args, **kwgs: Any) -> None:
101+
def __init__(self, klass: Callable[P, T] | str, *args: P.args, **kwgs: P.kwargs) -> None:
102102
"""InstanceHP is an Instance type class to spawn the instance of
103103
`klass(*args, **kwargs)` in the `HasParent` (parent) object.
104104
@@ -428,7 +428,7 @@ def configure(
428428

429429

430430
def instanceHP_wrapper(
431-
klass: type[T] | str,
431+
klass: Callable[P, T] | str,
432432
/,
433433
*,
434434
defaults: None | dict[str, Any] = None,
@@ -438,7 +438,7 @@ def instanceHP_wrapper(
438438
allow_none: bool | NO_DEFAULT_TYPE = NO_DEFAULT,
439439
load_default: bool | NO_DEFAULT_TYPE = NO_DEFAULT,
440440
**kwargs: Unpack[IHPSettings],
441-
):
441+
) -> Callable[P, InstanceHP[T]]:
442442
"""
443443
A decorator style function to produce InstanceHP trait for klass.
444444
@@ -483,9 +483,7 @@ class Widget_Box(Menubox):
483483
defaults_ = merge({}, defaults) if defaults else {}
484484
tags = dict(tags) if tags else {}
485485

486-
# TODO : Requires py 3.12+ https://typing.readthedocs.io/en/latest/spec/constructors.html#converting-a-constructor-to-callable
487-
# Hopefully this will restore documentation to the Constructor.
488-
def instanceHP_factory(*args, **kwgs) -> InstanceHP[T]:
486+
def instanceHP_factory(*args: P.args, **kwgs: P.kwargs) -> InstanceHP[T]:
489487
"""Returns an InstanceHP[klass] trait.
490488
491489
Use this to add a trait to new subclass of HasParent.
@@ -494,8 +492,9 @@ def instanceHP_factory(*args, **kwgs) -> InstanceHP[T]:
494492
495493
Follow the link (ctrl + click): function-> klass to see the class definition and what *args and **kwargs are available.
496494
"""
497-
kw = merge({}, defaults_, kwgs, strategy=strategy) if defaults_ else kwgs
498-
instance = InstanceHP(klass, *args, **kw)
495+
if defaults_:
496+
kwgs = merge({}, defaults_, kwgs, strategy=strategy) # type: ignore
497+
instance = InstanceHP(klass, *args, **kwgs)
499498
instance.configure(load_default=load_default, read_only=read_only, allow_none=allow_none, **kwargs)
500499
if tags:
501500
instance.tag(**tags)

0 commit comments

Comments
 (0)