diff --git a/imperative/python/src/tensor.cpp b/imperative/python/src/tensor.cpp index 323616143215f13cc1b4c12e90373e85c705f469..1dc16c6f5dc5a6c987e63518ae76108fa857afa6 100644 --- a/imperative/python/src/tensor.cpp +++ b/imperative/python/src/tensor.cpp @@ -101,7 +101,10 @@ PyObject* py_apply( HostTensorND ht(target_cn); ht = npy::np2tensor(args[i], npy::Meth::copy_into(&ht), target_dtype); record_py_backtrace(); - if (PyArray_Check(args[i]) || PyList_Check(args[i])) { // non scaler + //! operand in elemwise can't be None + if (args[i] == Py_None) { + throw py::type_error("the operand is None and is not supported."); + } else if (PyArray_Check(args[i]) || PyList_Check(args[i])) { // non scaler // py_tuple is not allowed here because of tracing return imperative::apply( CreateTensor(CreateTensor::Const, target_cn, ht.layout()), diff --git a/imperative/python/test/unit/core/test_tensor_wrapper.py b/imperative/python/test/unit/core/test_tensor_wrapper.py index fce911cf8a666736d84e5b782dc9889a96b2e9af..f350942b8223b91e5dcbb653055e4ca9653d8008 100644 --- a/imperative/python/test/unit/core/test_tensor_wrapper.py +++ b/imperative/python/test/unit/core/test_tensor_wrapper.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import copy +import unittest import numpy as np import pytest @@ -235,3 +236,11 @@ def test_tensor_construct_tensor(): assert Tensor(x.to("xpu0:2"), device="xpu0:1").device == "xpu0:1" assert Tensor(x.to("xpu0:2")).device == x.to("xpu0:2").device _full_sync() + + +class TestElemwiseNone(unittest.TestCase): + def test_elemementwise_and_with_none(self): + with self.assertRaises(TypeError) as context: + a = Tensor(1.0) + b = a + None + assert str(context.exception) == "the operand is None and is not supported."