From 7fcc7ac4b663d44344af6123ac71c13c7a71dbf6 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 27 Feb 2018 16:33:21 -0800 Subject: [PATCH] Make unary comparison ops evaluate their argument --- NEWS.rst | 6 ++++++ hy/compiler.py | 3 ++- tests/native_tests/operators.hy | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/NEWS.rst b/NEWS.rst index beb22ed..a1b503f 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -3,6 +3,12 @@ Unreleased ============================== +Other Breaking Changes +------------------------------ +* Non-shadow unary `=`, `is`, `<`, etc. now evaluate their argument + instead of ignoring it. This change increases consistency a bit + and makes accidental unary uses easier to notice. + Bug Fixes ------------------------------ * Fix `(return)` so it works correctly to exit a Python 2 generator diff --git a/hy/compiler.py b/hy/compiler.py index e6a888e..1132b65 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1574,7 +1574,8 @@ class HyASTCompiler(object): @checkargs(min=1) def compile_compare_op_expression(self, expression): if len(expression) == 2: - return asty.Name(expression, id="True", ctx=ast.Load()) + return (self.compile(expression[1]) + + asty.Name(expression, id="True", ctx=ast.Load())) return self._compile_compare_op_expression(expression) @builds("!=", "is_not") diff --git a/tests/native_tests/operators.hy b/tests/native_tests/operators.hy index 721ee6c..a32d5ab 100644 --- a/tests/native_tests/operators.hy +++ b/tests/native_tests/operators.hy @@ -219,7 +219,15 @@ (op-and-shadow-test [= is] (forbid (f)) + (assert (is (f "hello") True)) + + ; Unary comparison operators, despite always returning True, + ; should evaluate their argument. + (setv p "a") + (assert (is (f (do (setv p "b") "hello")) True)) + (assert (= p "b")) + (defclass C) (setv x (get {"is" (C) "=" 0} f-name)) (setv y (get {"is" (C) "=" 1} f-name)) @@ -229,6 +237,7 @@ (assert (is (f y x) False)) (assert (is (f x x x x x) True)) (assert (is (f x x x y x) False)) + (setv n None) (assert (is (f n None) True)) (assert (is (f n "b") False)))