FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: tensor equals type raises exception by JohannesMessner · Pull Request #1739 · docarray/docarray · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 6 additions & 4 deletions docarray/typing/tensor/abstract_tensor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class _ParametrizedMeta(type):

def _equals_special_case(cls, other):
is_type = isinstance(other, type)
is_tensor = is_type and AbstractTensor in other.mro()
same_parents = is_tensor and cls.mro()[1:] == other.mro()[1:]
is_tensor = is_type and AbstractTensor in other.__mro__
same_parents = is_tensor and cls.__mro__[1:] == other.__mro__[1:]

subclass_target_shape = getattr(other, '__docarray_target_shape__', False)
self_target_shape = getattr(cls, '__docarray_target_shape__', False)
Expand Down Expand Up @@ -92,9 +92,11 @@ def __instancecheck__(cls, instance):
return False
return any(
safe_issubclass(candidate, _cls.__unparametrizedcls__)
for candidate in type(instance).mro()
for candidate in type(instance).__mro__
)
return any(issubclass(candidate, cls) for candidate in type(instance).mro())
return any(
issubclass(candidate, cls) for candidate in type(instance).__mro__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

us safe version of is_subclass please

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

i am not sure if we want that here. This implements the "normal" isinstance() call, so i don't think it should work with non-class inputs. For example, the following also raises an exception:

isinstance(3, List[int])
TypeError: Subscripted generics cannot be used with class and instance checks

So I would not catch such things here

)
return super().__instancecheck__(instance)

def __eq__(cls, other):
Expand Down
5 changes: 5 additions & 0 deletions tests/units/typing/tensor/test_tensor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ class MyTensorDoc(BaseDoc):
assert isinstance(doc.tensor, TensorFlowTensor)
assert isinstance(doc.tensor.tensor, tf.Tensor)
assert tnp.allclose(doc.tensor.tensor, tf.zeros((1000, 2)))


def test_equals_type():
# see https://github.com/docarray/docarray/pull/1739
assert not (TorchTensor == type)

Back | FazBrowse Home | New Git URL