From b93d4b63a74c35d60b007695c2bbcd8b68263f60 Mon Sep 17 00:00:00 2001 From: Alan Fleming Date: Sun, 9 Mar 2025 19:23:09 +1100 Subject: [PATCH 1/6] Update __new__ method to use Self type for improved type hinting --- traitlets/traitlets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index a9d02c72..a2fab688 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -1290,7 +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: + def __new__(*args: t.Any, **kwargs: t.Any) -> Self: # type:ignore[misc, type-var] # Pass cls as args[0] to allow "cls" as keyword argument cls = args[0] args = args[1:] @@ -1303,7 +1303,7 @@ def __new__(*args: t.Any, **kwargs: t.Any) -> t.Any: else: inst = new_meth(cls, *args, **kwargs) inst.setup_instance(*args, **kwargs) - return inst + return inst # type:ignore[no-any-return] def setup_instance(*args: t.Any, **kwargs: t.Any) -> None: """ From 60819d7284847278728aee8934470a24df17f4f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Mar 2025 08:23:45 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- traitlets/traitlets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index a2fab688..7310f5c2 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -1290,7 +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) -> Self: # type:ignore[misc, type-var] + def __new__(*args: t.Any, **kwargs: t.Any) -> Self: # type:ignore[misc, type-var] # Pass cls as args[0] to allow "cls" as keyword argument cls = args[0] args = args[1:] @@ -1303,7 +1303,7 @@ def __new__(*args: t.Any, **kwargs: t.Any) -> Self: # type:ignore[misc, type-var else: inst = new_meth(cls, *args, **kwargs) inst.setup_instance(*args, **kwargs) - return inst # type:ignore[no-any-return] + return inst # type:ignore[no-any-return] def setup_instance(*args: t.Any, **kwargs: t.Any) -> None: """ From f492fde924391db86bbc48fedecf1fb0710711e4 Mon Sep 17 00:00:00 2001 From: Alan Fleming Date: Sun, 9 Mar 2025 21:54:00 +1100 Subject: [PATCH 3/6] Refactor __new__ method in HasDescriptors to accept cls as a keyword argument --- traitlets/traitlets.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index 7310f5c2..bdad3b33 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -1290,10 +1290,8 @@ 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) -> Self: # type:ignore[misc, type-var] - # 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. @@ -1303,7 +1301,7 @@ def __new__(*args: t.Any, **kwargs: t.Any) -> Self: # type:ignore[misc, type-va else: inst = new_meth(cls, *args, **kwargs) inst.setup_instance(*args, **kwargs) - return inst # type:ignore[no-any-return] + return inst def setup_instance(*args: t.Any, **kwargs: t.Any) -> None: """ From ea16a89df88b224334a8b4a14b07f01f5fa31985 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Mar 2025 10:54:20 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- traitlets/traitlets.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index bdad3b33..3b212099 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -1291,8 +1291,6 @@ class HasDescriptors(metaclass=MetaHasDescriptors): """The base class for all classes that have descriptors.""" 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__ From 1f78e2cdb3562cd6a47841d34a43d1b87e16c89b Mon Sep 17 00:00:00 2001 From: Alan Fleming Date: Mon, 10 Mar 2025 09:30:54 +1100 Subject: [PATCH 5/6] Refactor setup_instance method to accept self as a positional-only argument --- traitlets/traitlets.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index 3b212099..38841b6c 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -1301,13 +1301,10 @@ def __new__(cls, /, *args: t.Any, **kwargs: t.Any) -> Self: 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__ @@ -1329,11 +1326,8 @@ 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. From 8cf29469c60dd19149c148c4708e3966b8be7c17 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Mar 2025 22:32:51 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- traitlets/traitlets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index 38841b6c..0d248e95 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -1327,7 +1327,6 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): _all_trait_default_generators: dict[str, t.Any] 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.