File: let.hy

package info (click to toggle)
hy 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,632 kB
  • sloc: python: 7,299; makefile: 38; sh: 27
file content (678 lines) | stat: -rw-r--r-- 15,823 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
(import types
        pytest)

(defn test-let-basic []
  (assert (= (let [a 0] a) 0))
  (setv a "a"
        b "b")
  (let [a "x"
        b "y"]
    (assert (= (+ a b)
               "xy"))
    (let [a "z"]
      (assert (= (+ a b)
                 "zy")))
    ;; let-shadowed variable doesn't get clobbered.
    (assert (= (+ a b)
               "xy")))
  (let [q "q"]
    (assert (= q "q")))
  (assert (= a "a"))
  (assert (= b "b"))
  (assert (in "a" (.keys (vars))))
  ;; scope of q is limited to let body
  (assert (not-in "q" (.keys (vars)))))


;; let should substitute within f-strings
;; related to https://github.com/hylang/hy/issues/1843
(defn test-let-fstring []
  (assert (= (let [a 0] a) 0))
  (setv a "a"
        b "b")
  (let [a "x"
        b "y"]
    (assert (= f"res: {(+ a b)}!"
               "res: xy!"))
    (let [a 4]
      (assert (= f"double f >{b :^{(+ a 1)}}<"
                 "double f >  y  <")))))


(defn test-let-sequence []
  ;; assignments happen in sequence, not parallel.
  (setv a "x"
        b "y"
        c "z")
  (let [a "a"
        b "b"
        ab (+ a b)]
    (assert (= ab "ab"))
    (let [c "c"
          abc (+ ab c)]
      (assert (= abc "abc")))))


(defn test-let-early []
  (setv a "a")
  (let [q (+ a "x")
        a 2  ; should not affect q
        b 3]
    (assert (= q "ax"))
    (let [q (* a b)
          a (+ a b)
          b (* a b)]
      (assert (= q 6))
      (assert (= a 5))
      (assert (= b 15))))
  (assert (= a "a")))


(defn test-let-special []
  ;; special forms in function position still work as normal
  (let [import 1]
    (assert (= #(import import)
               #(1 1)))))


(defn test-let-if-result []
  (let [af None]
    (setv af (if (> 5 3) (do 5 5) (do 3 3)))
    (assert (= af 5))))


(defn test-let-for []
  (let [x 99]
    (for [x (range 20)])
    (assert (= x 19))))


(defn test-let-generator []
  (let [x 99]
    (lfor x (range 20) :do x x)  ; force making a function
    (assert (= x 99))))


(defn test-let-comprehension-scope []
  ; https://github.com/hylang/hy/issues/2224

  (setv x 100)

  (let [x 10]
    (assert (=
      (lfor  x (range 5)  :if (> x 1)  x)
      [2 3 4]))
    (assert (= x 10)))

  (let [x 15]
    (assert (=
      (lfor  y (range 3)  :setv x (* y 2)  (+ y x))
      [0 3 6]))
    (assert (= x 15)))

  (let [x 20]
    (assert (=
      (lfor  z "abc"  :do (setv x (.upper z))  (+ z x))
      ["aA" "bB" "cC"]))
    (assert (= x "C")))

  (let [x 25
        l []]
    (for [x (range 5)  :if (> x 1)]
      (.append l x))
    (assert (= l [2 3 4]))
    (assert (= x 4)))

  (assert (= x 100)))


(defn test-let-quasiquote []
  (setv a-symbol 'a)
  (let [a "x"]
    (assert (= a "x"))
    (assert (= 'a a-symbol))
    (assert (= `a a-symbol))
    (assert (= (hy.as-model `(foo ~a))
               '(foo "x")))
    (assert (= (hy.as-model `(foo `(bar a ~a ~~a)))
               '(foo `(bar a ~a ~"x"))))
    (assert (= (hy.as-model `(foo ~@[a]))
               '(foo "x")))
    (assert (= (hy.as-model `(foo `(bar [a] ~@[a] ~@~(hy.models.List [a 'a `a]) ~~@[a])))
               '(foo `(bar [a] ~@[a] ~@["x" a a] ~"x"))))))


(defn test-let-except []
  (let [foo 42
        bar 33]
    (assert (= foo 42))
    (try
      (do
        (/ 1 0)
        (assert False))
      (except [foo Exception]
        ;; let bindings should work in except block
        (assert (= bar 33))
        ;; but exception bindings can shadow let bindings
        (assert (= (get (locals) "foo") foo))
        (assert (isinstance foo Exception))))
    ;; let binding did not get clobbered.
    (assert (= foo 42))))


(defn test-let-with []
  (let [foo 42]
    (assert (= foo 42))
    (with [foo (pytest.raises ZeroDivisionError)]
      (do
        (assert (!= foo 42))
        (/ 1 0)
        (assert False)))
    (assert (is (. foo type) ZeroDivisionError))))


(defn test-let-mutation []
  (setv foo 42)
  (setv error False)
  (let [foo 12
        bar 13]
    (assert (= foo 12))
    (setv foo 14)
    (assert (= foo 14))
    (del foo)
    ;; deleting a let binding should not affect others
    (assert (= bar 13))
    (try
      ;; foo=42 is still shadowed, but the let binding was deleted.
      (do
        foo
        (assert False))
      (except [le UnboundLocalError]
        (setv error le)))
    (setv foo 16)
    (assert (= foo 16))
    (setv [foo bar baz] [1 2 3])
    (assert (= foo 1))
    (assert (= bar 2))
    (assert (= baz 3)))
  (assert error)
  (assert (= foo 42))
  (assert (= baz 3)))


(defn test-let-break []
  (for [x (range 3)]
    (let [done (% x 2)]
      (when done (break))))
  (assert (= x 1)))


(defn test-let-continue []
  (let [foo []]
    (for [x (range 10)]
      (let [odd (% x 2)]
        (when odd (continue))
        (.append foo x)))
    (assert (= foo [0 2 4 6 8]))))


(defn test-let-yield []
  (defn grind []
    (yield 0)
    (let [a 1
          b 2]
      (yield a)
      (yield b)))
  (assert (= (tuple (grind))
             #(0 1 2))))


(defn test-let-return []
  (defn get-answer []
    (let [answer 42]
      (return answer)))
  (assert (= (get-answer)
             42)))


(defn test-let-import []
  (let [types 6]
    (assert (= types 6))
    ;; imports shadow let-bound names
    (import types)
    (assert (in "types" (vars)))
    (assert (isinstance types types.ModuleType)))
  ;; import happened in Python scope.
  (assert (in "types" (vars)))
  (assert (isinstance types types.ModuleType)))


(defn test-let-defn []
  (let [foo 42
        bar 99
        quux "quux"
        baz "baz"]
    (assert (= foo 42))
    ;; the name of the defn should be unaffected by the let
    (defn foo [bar]  ; let bindings do not apply in param list
      ;; let bindings apply inside fn body
      (nonlocal baz) ;; nonlocal should allow access to outer let bindings
      (setv x foo)
      (assert (isinstance x types.FunctionType))
      (assert (= (get (locals) "bar") bar))
      (setv y baz)
      (setv baz bar)
      (setv baz f"foo's {baz = }")
      ;; quux is local, so should shadow the let binding
      (setv quux "foo quux")
      (assert (= (get (locals) "quux") quux))
      quux)
    (assert (= quux "quux"))
    (assert (= foo (get (locals) "foo")))
    (assert (isinstance foo types.FunctionType))
    (assert (= baz "baz"))
    (assert (= (foo 2) "foo quux"))
    (assert (= baz "foo's baz = 2")))
  ;; defn happened in Python scope
  (assert (= foo (get (locals) "foo")))
  (assert (isinstance foo types.FunctionType))
  (assert (= (foo 2) "foo quux")))


(defn test-nested-assign []
  (let [fox 42]
    (defn bar []
      (let [unrelated 99]
        (setv fox 3))
      (assert (= (get (locals) "fox") fox))
      (assert (= fox 3)))
    (bar)
    (assert (= fox 42))))


(defn test-top-level-let-nonlocal []
  (hy.eval '(do
              (let [my-fuel 50]
                (defn propulse-me [distance]
                  (nonlocal my-fuel)
                  (-= my-fuel distance))
                (defn check-fuel []
                  my-fuel))
              (assert (= (check-fuel) 50))
              (propulse-me 3)
              (assert (= (check-fuel) 47)))
           :globals {}))


(defn test-let-nested-nonlocal []
  (let [fox 42]
    (defn bar []
      (let [unrelated 99]
        (defn baz []
          (nonlocal fox)
          (setv fox 2)))
      (setv fox 3)
      (assert (= fox 3))
      (baz)
      (assert (= fox 2)))
    (assert (= fox 42)))
  (bar))


(defn test-let-defclass []
  (let [Foo 42
        quux "quux"
        baz object]
    (assert (= Foo 42))
    ;; the name of the class should be unaffected by the let
    (defclass Foo [baz]  ; let bindings apply in inheritance list
      ;; let bindings apply inside class body
      (defn baz [self] Foo)
      ;; quux is local
      (setv quux "foo quux"))
    (assert (= quux "quux"))
    (assert (= Foo (get (locals) "Foo")))
    (assert (= Foo.quux "foo quux"))
    (assert (= (.baz (Foo)) Foo)))
  ;; defclass happened in Python scope
  (assert (= Foo (get (locals) "Foo")))
  (assert (= Foo.quux "foo quux"))
  (assert (= (.baz (Foo)) Foo)))


(defn test-let-dot []
  (setv foo (fn [])
        foo.a 42)
  (let [a 1
        b []
        bar (fn [])]
    (setv bar.a 13)
    (assert (= bar.a 13))
    (setv (. bar a) 14)
    (assert (= bar.a 14))
    (assert (= a 1))
    (assert (= b []))
    ;; method syntax not affected
    (.append b 2)
    (assert (= b [2]))
    ;; attrs access is not affected
    (assert (= foo.a 42))
    (assert (= (. foo a)
               42))
    ;; but indexing is
    (assert (= (. [1 2 3]
                  [a])
               2))))


(defn test-let-positional []
  (let [a 0
        b 1
        c 2]
    (defn foo [a b]
      #(a b c))
    (assert (= (foo 100 200)
               #(100 200 2)))
    (setv c 300)
    (assert (= (foo 1000 2000)
               #(1000 2000 300)))
    (assert (= a 0))
    (assert (= b 1))
    (assert (= c 300))))


(defn test-let-rest []
  (let [xs 6
        a 88
        c 64
        &rest 12]
    (defn foo [a b #* xs]
      (-= a 1)
      (setv xs (list xs))
      (.append xs 42)
      #(&rest a b c xs))
    (assert (= xs 6))
    (assert (= a 88))
    (assert (= (foo 1 2 3 4)
               #(12 0 2 64 [3 4 42])))
    (assert (= xs 6))
    (assert (= c 64))
    (assert (= a 88))))


(defn test-let-kwargs []
  (let [kws 6
        &kwargs 13]
    (defn foo [#** kws]
      #(&kwargs kws))
    (assert (= kws 6))
    (assert (= (foo :a 1)
               #(13 {"a" 1})))))


(defn test-let-optional []
  (let [a 1
        b 6
        d 2]
    (defn foo [[a a] [b None] [c d]]
      #(a b c))
    (assert (= (foo)
               #(1 None 2)))
    (assert (= (foo 10 20 30)
               #(10 20 30)))))


(defn test-let-closure []
  (let [count 0]
    (defn +count [[x 1]]
      (nonlocal count)
      (+= count x)
      count))
  ;; let bindings can still exist outside of a let body
  (assert (= 1 (+count)))
  (assert (= 2 (+count)))
  (assert (= 42 (+count 40))))


(defmacro triple [a]
  (setv g!a (hy.gensym a))
  `(do
     (setv ~g!a ~a)
     (+ ~g!a ~g!a ~g!a)))


(defmacro ap-triple []
  '(+ a a a))


(defn test-let-macros []
  (let [a 1
        b (triple a)
        c (ap-triple)]
    (assert (= (triple a)
               3))
    (assert (= (ap-triple)
               3))
    (assert (= b 3))
    (assert (= c 3))))


(defn test-let-rebind []
  (let [x "foo"
        y "bar"
        x (+ x y)
        y (+ y x)
        x (+ x x)]
    (assert (= x "foobarfoobar"))
    (assert (= y "barfoobar"))))


(defn test-let-unpacking []
  (let [[a b] [1 2]
        [lhead #* ltail] (range 3)
        #(thead #* ttail) (range 3)
        [nhead #* #(c #* nrest)] [0 1 2]]
    (assert (= a 1))
    (assert (= b 2))
    (assert (= lhead 0))
    (assert (= ltail [1 2]))
    (assert (= thead 0))
    (assert (= ttail [1 2]))
    (assert (= nhead 0))
    (assert (= c 1))
    (assert (= nrest [2]))))


(defn test-let-unpacking-rebind []
  (let [[a b] [:foo :bar]
        [a #* c] (range 3)
        [head #* tail] [a b c]]
    (assert (= a 0))
    (assert (= b :bar))
    (assert (= c [1 2]))
    (assert (= head 0))
    (assert (= tail [:bar [1 2]]))))


(defn test-no-extra-eval-of-function-args []
  ; https://github.com/hylang/hy/issues/2116
  (setv l [])
  (defn f []
    (.append l 1))
  (let [a 1]
    (assert (= a 1))
    (defn g [[b (f)]]
      5))
  (assert (= (g) 5))
  (assert (= l [1])))


(defn test-let-optional-2 []
  (let [a 1
        b 6
        d 2]
       (defn foo [* [a a] b [c d]]
         #(a b c))
       (assert (= (foo :b "b")
                  #(1 "b" 2)))
       (assert (= (foo :b 20 :a 10 :c 30)
                  #(10 20 30)))))


(defmacro eval-isolated [#* body]
  `(hy.eval '(do ~@body) :module (hy.I.types.ModuleType "<test>") :locals {}))


(defn test-let-bound-nonlocal []
  (with [err (pytest.raises SyntaxError)]
    (eval-isolated
      (let [foo 99]
        (nonlocal undefined))))
  (assert (in "no binding for nonlocal 'undefined'" err.value.msg))

  (with [err (pytest.raises SyntaxError)]
    (eval-isolated
      (defn bax []
        (let [other 99]
          (let [foo 99]
            (nonlocal undefined)
            other)))))
  (assert (in "no binding for nonlocal 'undefined'" err.value.msg))

  (with [err (pytest.raises SyntaxError)]
    (eval-isolated
      (defn bax []
        (let [other 99]
          (let [unrelated 99]
            (nonlocal unrelated)
            other)))))
  (assert (in "no binding for nonlocal 'unrelated'" err.value.msg))

  (with [err (pytest.raises SyntaxError)]
    (eval-isolated
      (defn outer []
        (let [fox 42]
          (defn bar []
            (let [unrelated 99]
              (nonlocal something-else)  ; error, nothing to bind to
              (setv fox 2))
            (setv fox 3))))))
  (assert (in "no binding for nonlocal 'something_else'" err.value.msg))

  (eval-isolated
    (defn outer []
      (defn bax []
        (let [other 99]
          (let [unrelated 99]
            ; should be elided, allowing this to compile
            (nonlocal other)
            other)))
      (bax))
    (assert (= 99 (outer))))

  (let [fox 42]
    (defn baz []
      (let [unrelated 99]
        (nonlocal fox)  ; should bind to outer let from within bar
        (setv fox 2))
      (setv fox 3))  ; `nonlocal` affects everything in the Python scope
    (assert (= fox 42))
    (baz)
    (assert (= fox 3)))

  (setv hound 43)
  (defn bay []
    (let [unrelated 99]
      (nonlocal hound)  ; this should bind to outer `hound` as expected
      (setv hound 2))
    (setv hound 3))  ; `nonlocal` affects everything in the Python scope
  (assert (= hound 43))
  (bay)
  (assert (= hound 3))

  (let [fox 42]
    (setv wolf 44)
    (defn bax []
      (let [other 99]
        (let [unrelated 99]
          ; only binds `fox` and `wolf`; `other` is elided (allowing this to compile)
          (nonlocal fox wolf other)
          (setv fox 1)
          (setv wolf 2))
        (setv fox 3))
      (setv wolf 4))
    (assert (= fox 42))
    (assert (= wolf 44))
    (bax)
    (assert (= fox 3))
    (assert (= wolf 4))))


(defn test-let-bound-global []

  (eval-isolated
    (defn outer []
      (let [fox 42]
        (defn bar []
          (let [something-else 33]
            (let [unrelated 99]
              (global something-else)
              (setv something-else 2))))
        (bar)))
    (outer)
    (assert (= something-else 2)))

  (eval-isolated
    (defn bax []
      (let [other 99]
        (let [unrelated 99]
          (global other)
          (setv other 2))
        (setv other 3)))  ; `global` affects everything in the Python scope
    (bax)
    (assert (= other 3)))

  (eval-isolated
    (let [fox 42]
      (defn baz []
        (let [unrelated 99]
          (global fox)  ; should bind outside any let scope
          (setv fox 2))
        (setv fox 3))  ; `global` affects everything in the Python scope
      (assert (= fox 42))
      (baz)
      (assert (= fox 42)))
    (assert (= fox 3)))

  (eval-isolated
    (setv hound 43)
    (defn bay []
      (let [unrelated 99]
        (global hound)  ; this should bind to global `hound` as expected
        (setv hound 2))
      (setv hound 3))  ; `global` affects everything in the Python scope
    (assert (= hound 43))
    (bay)
    (assert (= hound 3)))

  (eval-isolated
    (let [fox 42]
      (setv wolf 44)
      (defn bax []
        (let [other 99]
          (let [unrelated 99]
            (global fox wolf other)
            (setv fox 1)
            (setv wolf 2)
            (setv other 3))
          (setv fox 4))
        (setv wolf 5))
      (assert (= fox 42))
      (assert (= wolf 44))
      (bax)
      (assert (= fox 42))
      (assert (= wolf 5))
      (assert (= other 3)))
    (assert (= fox 4))
    (assert (= wolf 5))
    (assert (= other 3))))