Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor Author

@fleming79 fleming79 Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# This is needed because object.__new__ only accepts
# the cls argument.
new_meth = super(HasDescriptors, cls).__new__
Expand All @@ -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__
Expand All @@ -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.
Expand Down
Loading