Skip to content

Commit

Permalink
don't use awkward stuff in _methods.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp committed Jan 24, 2024
1 parent 4ff6275 commit 6afedf3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
101 changes: 47 additions & 54 deletions src/vector/_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4130,60 +4130,53 @@ def _handler_of(*objects: VectorProtocol) -> VectorProtocol:

assert handler is not None

# if there is a 2D vector in objects
if _check_instance(any, objects, Vector2D):
# if all the objects are not from the same backend
# choose the 2D object of the backend with highest priority if it exists
# or demote the first encountered object of the backend with highest priority to 2D
if (
not _check_instance(all, objects, vector.VectorObject)
and not _check_instance(all, objects, vector.VectorNumpy)
and not _check_instance(all, objects, vector.VectorAwkward)
):
new_type = type(handler.to_Vector2D())
flag = 0
# if there is a 2D object of the backend with highest priority
# make it the new handler
for obj in objects:
if type(obj) == new_type:
handler = obj
flag = 1
# else, demote the dimension of the object of the backend with highest priority
if flag == 0:
handler = handler.to_Vector2D()
# if all objects are from the same backend
# use the 2D one as the handler
else:
for obj in objects:
if isinstance(obj, Vector2D):
handler = obj
# if there is no 2D vector but a 3D vector in objects
elif _check_instance(any, objects, Vector3D):
# if all the objects are not from the same backend
# choose the 3D object of the backend with highest priority if it exists
# or demote the first encountered object of the backend with highest priority to 3D
if (
not _check_instance(all, objects, vector.VectorObject)
and not _check_instance(all, objects, vector.VectorNumpy)
and not _check_instance(all, objects, vector.VectorAwkward)
):
new_type = type(handler.to_Vector3D())
flag = 0
# if there is a 3D object of the backend with highest priority
# make it the new handler
for obj in objects:
if type(obj) == new_type:
handler = obj
flag = 1
# else, demote the dimension of the object of the backend with highest priority
if flag == 0:
handler = handler.to_Vector3D()
# if all objects are from the same backend
# use the 3D one as the handler
else:
for obj in objects:
if isinstance(obj, Vector3D):
handler = obj
if _check_instance(all, objects, Vector):
# if there is a 2D vector in objects
if _check_instance(any, objects, Vector2D):
# if all the objects are not from the same backend
# choose the 2D object of the backend with highest priority if it exists
# or demote the first encountered object of the backend with highest priority to 2D
if len({_handler_priority.index(obj.__module__) for obj in objects}) != 1:
new_type = type(handler.to_Vector2D())
flag = 0
# if there is a 2D object of the backend with highest priority
# make it the new handler
for obj in objects:
if type(obj) == new_type:
handler = obj
flag = 1
# else, demote the dimension of the object of the backend with highest priority
if flag == 0:
handler = handler.to_Vector2D()
# if all objects are from the same backend
# use the 2D one as the handler
else:
for obj in objects:
if isinstance(obj, Vector2D):
handler = obj
# if there is no 2D vector but a 3D vector in objects
elif _check_instance(any, objects, Vector3D):
# if all the objects are not from the same backend
# choose the 3D object of the backend with highest priority if it exists
# or demote the first encountered object of the backend with highest priority to 3D
if len({_handler_priority.index(obj.__module__) for obj in objects}) != 1:
new_type = type(handler.to_Vector3D())
flag = 0
# if there is a 3D object of the backend with highest priority
# make it the new handler
for obj in objects:
if type(obj) == new_type:
handler = obj
flag = 1
# else, demote the dimension of the object of the backend with highest priority
if flag == 0:
handler = handler.to_Vector3D()
# if all objects are from the same backend
# use the 3D one as the handler
else:
for obj in objects:
if isinstance(obj, Vector3D):
handler = obj

return handler

Expand Down
2 changes: 1 addition & 1 deletion tests/backends/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def test_demotion():
assert all(v2 + v3 == p_v2_v3)
assert all(v3 + v2 == p_v2_v3)

v2 = vector.zip(
v2 = vector.array(
{
"x": [10.0, 20.0, 30.0],
"y": [-10.0, 20.0, 30.0],
Expand Down
4 changes: 4 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

from __future__ import annotations

import pytest

import vector
from vector import VectorObject2D, VectorObject3D, VectorObject4D

awkward = pytest.importorskip("awkward")


def test_handler_of():
object_a = VectorObject4D.from_xyzt(0.0, 0.0, 0.0, 0.0)
Expand Down

0 comments on commit 6afedf3

Please sign in to comment.