From 3d93a385851aa3903ecec3cfa26c6ab7b8c80161 Mon Sep 17 00:00:00 2001 From: Lin Jiang Date: Mon, 8 May 2023 11:21:41 +0800 Subject: [PATCH] [Lang] Remove the support for 'is' (#7930) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: # ### Brief Summary ### 🤖 Generated by Copilot at 661bbce Remove `is` and `is not` operators from Taichi scope and update test case. This change prevents confusion and errors when comparing Taichi tensors and scalars. ### Walkthrough ### 🤖 Generated by Copilot at 661bbce * Remove support for `is` and `is not` operators in Taichi scope and raise syntax error instead ([link](https://github.com/taichi-dev/taichi/pull/7930/files?diff=unified&w=0#diff-3e22417ffade4af0564893b98dc5101d714b8ba6fd4423ab5bc5129e360fee8fL1026-L1027), [link](https://github.com/taichi-dev/taichi/pull/7930/files?diff=unified&w=0#diff-3e22417ffade4af0564893b98dc5101d714b8ba6fd4423ab5bc5129e360fee8fL1034-R1038), [link](https://github.com/taichi-dev/taichi/pull/7930/files?diff=unified&w=0#diff-8981be068a363e39524dc2e29d28d4c13a097d0037fc3a1176b249ce5bf35ef8L102-R103)) --- python/taichi/lang/ast/ast_transformer.py | 17 ++++---------- tests/python/test_ast_refactor.py | 11 --------- tests/python/test_compare.py | 27 ----------------------- tests/python/test_deprecation.py | 7 ++---- 4 files changed, 6 insertions(+), 56 deletions(-) diff --git a/python/taichi/lang/ast/ast_transformer.py b/python/taichi/lang/ast/ast_transformer.py index 79a2bb8cf7297..ef405ce94582a 100644 --- a/python/taichi/lang/ast/ast_transformer.py +++ b/python/taichi/lang/ast/ast_transformer.py @@ -1011,28 +1011,19 @@ def build_Compare(ctx, node): ops_static = { ast.In: lambda l, r: l in r, ast.NotIn: lambda l, r: l not in r, - ast.Is: lambda l, r: l is r, - ast.IsNot: lambda l, r: l is not r, } if ctx.is_in_static_scope(): ops = {**ops, **ops_static} operands = [node.left.ptr] + [comparator.ptr for comparator in node.comparators] val = True for i, node_op in enumerate(node.ops): + if isinstance(node_op, (ast.Is, ast.IsNot)): + name = "is" if isinstance(node_op, ast.Is) else "is not" + raise TaichiSyntaxError(f'Operator "{name}" in Taichi scope is not supported.') l = operands[i] r = operands[i + 1] op = ops.get(type(node_op)) - if isinstance(node_op, (ast.Is, ast.IsNot)): - name = "is" if isinstance(node_op, ast.Is) else "is not" - warnings.warn_explicit( - f'Operator "{name}" in Taichi scope is deprecated, ' - f"and it will be removed in Taichi v1.6.0. " - f"Please avoid using it.", - DeprecationWarning, - ctx.file, - node.lineno + ctx.lineno_offset, - module="taichi", - ) + if op is None: if type(node_op) in ops_static: raise TaichiSyntaxError(f'"{type(node_op).__name__}" is only supported inside `ti.static`.') diff --git a/tests/python/test_ast_refactor.py b/tests/python/test_ast_refactor.py index 669c419dd724a..deab2b0310c44 100644 --- a/tests/python/test_ast_refactor.py +++ b/tests/python/test_ast_refactor.py @@ -164,17 +164,6 @@ def foo(a: ti.template()): assert a[i] == b[i] -@test_utils.test() -def test_compare_fail(): - with pytest.raises(ti.TaichiCompilationError, match='"Is" is only supported inside `ti.static`.'): - - @ti.kernel - def foo(): - None is None - - foo() - - @test_utils.test() def test_single_compare(): @ti.kernel diff --git a/tests/python/test_compare.py b/tests/python/test_compare.py index 487e88c70f7ee..84a335bc4c054 100644 --- a/tests/python/test_compare.py +++ b/tests/python/test_compare.py @@ -171,33 +171,6 @@ def foo(a: ti.template()) -> ti.i32: foo(ti.i32) -@test_utils.test() -def test_static_is(): - @ti.kernel - def is_f32(tp: ti.template()) -> ti.i32: - return ti.static(tp is ti.f32) - - @ti.kernel - def is_not_f32(tp: ti.template()) -> ti.i32: - return ti.static(tp is not ti.f32) - - assert is_f32(ti.f32) == 1 - assert is_f32(ti.i32) == 0 - assert is_not_f32(ti.f32) == 0 - assert is_not_f32(ti.i32) == 1 - - -@test_utils.test() -def test_non_static_is(): - with pytest.raises(ti.TaichiCompilationError, match='"Is" is only supported inside `ti.static`.'): - - @ti.kernel - def is_f32(tp: ti.template()) -> ti.i32: - return tp is ti.f32 - - is_f32(ti.f32) - - @test_utils.test(default_ip=ti.i64, require=ti.extension.data64) def test_compare_ret_type(): # The purpose of this test is to make sure a comparison returns i32 diff --git a/tests/python/test_deprecation.py b/tests/python/test_deprecation.py index f1feeaa4f54aa..a46ea7ca2b798 100644 --- a/tests/python/test_deprecation.py +++ b/tests/python/test_deprecation.py @@ -69,11 +69,8 @@ def test_deprecate_rwtexture_ndim(): @test_utils.test() -def test_deprecate_is_is_not(): - with pytest.warns( - DeprecationWarning, - match='Operator "is" in Taichi scope is deprecated, ' "and it will be removed in Taichi v1.6.0.", - ): +def test_remove_is_is_not(): + with pytest.raises(ti.TaichiSyntaxError, match='Operator "is" in Taichi scope is not supported'): @ti.kernel def func():