Make unary comparison ops evaluate their argument

This commit is contained in:
Kodi Arfer 2018-02-27 16:33:21 -08:00
parent 9f0911161d
commit 7fcc7ac4b6
3 changed files with 17 additions and 1 deletions

View File

@ -3,6 +3,12 @@
Unreleased 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 Bug Fixes
------------------------------ ------------------------------
* Fix `(return)` so it works correctly to exit a Python 2 generator * Fix `(return)` so it works correctly to exit a Python 2 generator

View File

@ -1574,7 +1574,8 @@ class HyASTCompiler(object):
@checkargs(min=1) @checkargs(min=1)
def compile_compare_op_expression(self, expression): def compile_compare_op_expression(self, expression):
if len(expression) == 2: 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) return self._compile_compare_op_expression(expression)
@builds("!=", "is_not") @builds("!=", "is_not")

View File

@ -219,7 +219,15 @@
(op-and-shadow-test [= is] (op-and-shadow-test [= is]
(forbid (f)) (forbid (f))
(assert (is (f "hello") True)) (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) (defclass C)
(setv x (get {"is" (C) "=" 0} f-name)) (setv x (get {"is" (C) "=" 0} f-name))
(setv y (get {"is" (C) "=" 1} f-name)) (setv y (get {"is" (C) "=" 1} f-name))
@ -229,6 +237,7 @@
(assert (is (f y x) False)) (assert (is (f y x) False))
(assert (is (f x x x x x) True)) (assert (is (f x x x x x) True))
(assert (is (f x x x y x) False)) (assert (is (f x x x y x) False))
(setv n None) (setv n None)
(assert (is (f n None) True)) (assert (is (f n None) True))
(assert (is (f n "b") False))) (assert (is (f n "b") False)))