diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index a9d02c72..0d248e95 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -1290,11 +1290,7 @@ def class_init(self, cls: type[HasTraits], name: str | None) -> None: class HasDescriptors(metaclass=MetaHasDescriptors): """The base class for all classes that have descriptors.""" - def __new__(*args: t.Any, **kwargs: t.Any) -> t.Any: - # Pass cls as args[0] to allow "cls" as keyword argument - cls = args[0] - args = args[1:] - + def __new__(cls, /, *args: t.Any, **kwargs: t.Any) -> Self: # This is needed because object.__new__ only accepts # the cls argument. new_meth = super(HasDescriptors, cls).__new__ @@ -1305,13 +1301,10 @@ def __new__(*args: t.Any, **kwargs: t.Any) -> t.Any: inst.setup_instance(*args, **kwargs) return inst - def setup_instance(*args: t.Any, **kwargs: t.Any) -> None: + def setup_instance(self, /, *args: t.Any, **kwargs: t.Any) -> None: """ This is called **before** self.__init__ is called. """ - # Pass self as args[0] to allow "self" as keyword argument - self = args[0] - args = args[1:] self._cross_validation_lock = False cls = self.__class__ @@ -1333,11 +1326,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): _traits: dict[str, t.Any] _all_trait_default_generators: dict[str, t.Any] - def setup_instance(*args: t.Any, **kwargs: t.Any) -> None: - # Pass self as args[0] to allow "self" as keyword argument - self = args[0] - args = args[1:] - + def setup_instance(self, /, *args: t.Any, **kwargs: t.Any) -> None: # although we'd prefer to set only the initial values not present # in kwargs, we will overwrite them in `__init__`, and simply making # a copy of a dict is faster than checking for each key.