From 3b6d1d13b0d2a4a88edef00be291862057fa6a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinclert=20P=C3=A9rez?= Date: Thu, 29 Jul 2021 13:12:01 +0200 Subject: [PATCH 1/7] Add _handler_of func. MRO classes logic --- src/vector/_methods.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/vector/_methods.py b/src/vector/_methods.py index 46007f85..e27ad749 100644 --- a/src/vector/_methods.py +++ b/src/vector/_methods.py @@ -5,6 +5,7 @@ import typing +from contextlib import suppress from vector._typeutils import ( BoolCollection, ScalarCollection, @@ -2533,6 +2534,20 @@ def _from_signature( ] +def _get_handler_index(obj: VectorProtocol) -> int: + """ + Returns the index of the first valid handler checking the list of parent classes + Returns -1 if no handler is found + """ + index = -1 + for cls in type(obj).__mro__: + with suppress(ValueError): + index = _handler_priority.index(cls.__module__) + break + + return index + + def _handler_of(*objects: VectorProtocol) -> VectorProtocol: """ Determines which vector should wrap the output of a dispatched function. @@ -2547,9 +2562,7 @@ def _handler_of(*objects: VectorProtocol) -> VectorProtocol: if isinstance(obj, Vector): if handler is None: handler = obj - elif _handler_priority.index( - type(obj).__module__ - ) > _handler_priority.index(type(handler).__module__): + elif _get_handler_index(obj) > _get_handler_index(handler): handler = obj assert handler is not None From 3b234e6f102874e91c8401796d064e82d37729ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinclert=20P=C3=A9rez?= Date: Thu, 29 Jul 2021 13:55:57 +0200 Subject: [PATCH 2/7] Add _handler_of func. test --- tests/test_methods.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/test_methods.py diff --git a/tests/test_methods.py b/tests/test_methods.py new file mode 100644 index 00000000..17a9dd16 --- /dev/null +++ b/tests/test_methods.py @@ -0,0 +1,17 @@ +# Copyright (c) 2019-2021, Jonas Eschle, Jim Pivarski, Eduardo Rodrigues, and Henry Schreiner. +# +# Distributed under the 3-clause BSD license, see accompanying file LICENSE +# or https://github.com/scikit-hep/vector for details. + +import vector + + +class CustomVector(vector.VectorObject4D): + pass + + +def test_handler_of(): + object_a = CustomVector.from_xyzt(0.0, 0.0, 0.0, 0.0) + object_b = CustomVector.from_xyzt(1.0, 1.0, 1.0, 1.0) + protocol = vector._methods._handler_of(object_a, object_b) + assert protocol == object_a From 33544808e346a17bbaf3b300648abc59524d5295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinclert=20P=C3=A9rez?= Date: Thu, 29 Jul 2021 13:49:47 +0200 Subject: [PATCH 3/7] Invert _handler_of func. logic --- src/vector/_methods.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/vector/_methods.py b/src/vector/_methods.py index e27ad749..641b0e90 100644 --- a/src/vector/_methods.py +++ b/src/vector/_methods.py @@ -2559,11 +2559,12 @@ def _handler_of(*objects: VectorProtocol) -> VectorProtocol: """ handler = None for obj in objects: - if isinstance(obj, Vector): - if handler is None: - handler = obj - elif _get_handler_index(obj) > _get_handler_index(handler): - handler = obj + if not isinstance(obj, Vector): + continue + if handler is None: + handler = obj + elif _get_handler_index(obj) > _get_handler_index(handler): + handler = obj assert handler is not None return handler From e088e9a53e764cbd2136a5e289c93e028aba3016 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:04:55 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/vector/_methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vector/_methods.py b/src/vector/_methods.py index 641b0e90..a5d00f08 100644 --- a/src/vector/_methods.py +++ b/src/vector/_methods.py @@ -4,8 +4,8 @@ # or https://github.com/scikit-hep/vector for details. import typing - from contextlib import suppress + from vector._typeutils import ( BoolCollection, ScalarCollection, From 9c60f5fa5e724a7b59b2fdaf8d5fe24acc18b1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinclert=20P=C3=A9rez?= Date: Thu, 29 Jul 2021 16:15:53 +0200 Subject: [PATCH 5/7] Tune _get_handler_index func. with Jim comment --- src/vector/_methods.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/vector/_methods.py b/src/vector/_methods.py index a5d00f08..cc0f9de1 100644 --- a/src/vector/_methods.py +++ b/src/vector/_methods.py @@ -2535,16 +2535,14 @@ def _from_signature( def _get_handler_index(obj: VectorProtocol) -> int: - """ - Returns the index of the first valid handler checking the list of parent classes - Returns -1 if no handler is found - """ + """Returns the index of the first valid handler checking the list of parent classes""" index = -1 for cls in type(obj).__mro__: with suppress(ValueError): index = _handler_priority.index(cls.__module__) break + assert index != -1 return index From 38f632cbe135a05e9a1073944d13b065954844d2 Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Thu, 29 Jul 2021 09:59:37 -0500 Subject: [PATCH 6/7] Replace `-1`/`break` logic with an early return. Co-authored-by: Henry Schreiner --- src/vector/_methods.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/vector/_methods.py b/src/vector/_methods.py index cc0f9de1..a22e1487 100644 --- a/src/vector/_methods.py +++ b/src/vector/_methods.py @@ -2536,14 +2536,10 @@ def _from_signature( def _get_handler_index(obj: VectorProtocol) -> int: """Returns the index of the first valid handler checking the list of parent classes""" - index = -1 for cls in type(obj).__mro__: with suppress(ValueError): - index = _handler_priority.index(cls.__module__) - break - - assert index != -1 - return index + return _handler_priority.index(cls.__module__) + raise AssertionError(f"Could not find a valid handler for {obj}! This should not happen.") def _handler_of(*objects: VectorProtocol) -> VectorProtocol: From 0dd5fb23375834ebd339ca463e909e0fe49c1822 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 29 Jul 2021 15:00:16 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/vector/_methods.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/vector/_methods.py b/src/vector/_methods.py index a22e1487..7c3bf7ab 100644 --- a/src/vector/_methods.py +++ b/src/vector/_methods.py @@ -2539,7 +2539,9 @@ def _get_handler_index(obj: VectorProtocol) -> int: for cls in type(obj).__mro__: with suppress(ValueError): return _handler_priority.index(cls.__module__) - raise AssertionError(f"Could not find a valid handler for {obj}! This should not happen.") + raise AssertionError( + f"Could not find a valid handler for {obj}! This should not happen." + ) def _handler_of(*objects: VectorProtocol) -> VectorProtocol: