Remove uses of defclass attribute lists

This commit is contained in:
Kodi Arfer 2019-07-02 12:02:03 -04:00
parent 8215281968
commit 308bedbebe
12 changed files with 63 additions and 64 deletions

View File

@ -6,11 +6,11 @@
(import [collections [defaultdict]] (import [collections [defaultdict]]
[hy [HyExpression HyList HyString]]) [hy [HyExpression HyList HyString]])
(defclass MultiDispatch [object] [ (defclass MultiDispatch [object]
_fns (defaultdict dict) (setv _fns (defaultdict dict))
__init__ (fn [self f] (defn __init__ [self f]
(setv self.f f) (setv self.f f)
(setv self.__doc__ f.__doc__) (setv self.__doc__ f.__doc__)
(unless (in f.__name__ (.keys (get self._fns f.__module__))) (unless (in f.__name__ (.keys (get self._fns f.__module__)))
@ -18,14 +18,14 @@
(setv values f.__code__.co_varnames) (setv values f.__code__.co_varnames)
(setv (get self._fns f.__module__ f.__name__ values) f)) (setv (get self._fns f.__module__ f.__name__ values) f))
fn? (fn [self v args kwargs] (defn fn? [self v args kwargs]
"Compare the given (checked fn) to the called fn" "Compare the given (checked fn) to the called fn"
(setv com (+ (list args) (list (.keys kwargs)))) (setv com (+ (list args) (list (.keys kwargs))))
(and (and
(= (len com) (len v)) (= (len com) (len v))
(.issubset (frozenset (.keys kwargs)) com))) (.issubset (frozenset (.keys kwargs)) com)))
__call__ (fn [self &rest args &kwargs kwargs] (defn __call__ [self &rest args &kwargs kwargs]
(setv func None) (setv func None)
(for [[i f] (.items (get self._fns self.f.__module__ self.f.__name__))] (for [[i f] (.items (get self._fns self.f.__module__ self.f.__name__))]
(when (.fn? self i args kwargs) (when (.fn? self i args kwargs)
@ -33,7 +33,7 @@
(break))) (break)))
(if func (if func
(func #* args #** kwargs) (func #* args #** kwargs)
(raise (TypeError "No matching functions with this signature"))))]) (raise (TypeError "No matching functions with this signature")))))
(defn multi-decorator [dispatch-fn] (defn multi-decorator [dispatch-fn]
(setv inner (fn [&rest args &kwargs kwargs] (setv inner (fn [&rest args &kwargs kwargs]

View File

@ -3,12 +3,12 @@
;; license. See the LICENSE. ;; license. See the LICENSE.
(defclass Sequence [] (defclass Sequence []
[--init-- (fn [self func] (defn --init-- [self func]
"initialize a new sequence with a function to compute values" "initialize a new sequence with a function to compute values"
(setv (. self func) func) (setv (. self func) func)
(setv (. self cache) []) (setv (. self cache) [])
(setv (. self high-water) -1)) (setv (. self high-water) -1))
--getitem-- (fn [self n] (defn --getitem-- [self n]
"get nth item of sequence" "get nth item of sequence"
(if (hasattr n "start") (if (hasattr n "start")
(gfor x (range n.start n.stop (or n.step 1)) (gfor x (range n.start n.stop (or n.step 1))
@ -23,7 +23,7 @@
(setv (. self high-water) (inc (. self high-water))) (setv (. self high-water) (inc (. self high-water)))
(.append (. self cache) (.func self (. self high-water)))) (.append (. self cache) (.func self (. self high-water))))
(get self n)))))) (get self n))))))
--iter-- (fn [self] (defn --iter-- [self]
"create iterator for this sequence" "create iterator for this sequence"
(setv index 0) (setv index 0)
(try (while True (try (while True
@ -31,7 +31,7 @@
(setv index (inc index))) (setv index (inc index)))
(except [IndexError] (except [IndexError]
(return)))) (return))))
--len-- (fn [self] (defn --len-- [self]
"length of the sequence, dangerous for infinite sequences" "length of the sequence, dangerous for infinite sequences"
(setv index (. self high-water)) (setv index (. self high-water))
(try (while True (try (while True
@ -39,17 +39,17 @@
(setv index (inc index))) (setv index (inc index)))
(except [IndexError] (except [IndexError]
(len (. self cache))))) (len (. self cache)))))
max-items-in-repr 10 (setv max-items-in-repr 10)
--str-- (fn [self] (defn --str-- [self]
"string representation of this sequence" "string representation of this sequence"
(setv items (list (take (inc self.max-items-in-repr) self))) (setv items (list (take (inc self.max-items-in-repr) self)))
(.format (if (> (len items) self.max-items-in-repr) (.format (if (> (len items) self.max-items-in-repr)
"[{0}, ...]" "[{0}, ...]"
"[{0}]") "[{0}]")
(.join ", " (map str items)))) (.join ", " (map str items))))
--repr-- (fn [self] (defn --repr-- [self]
"string representation of this sequence" "string representation of this sequence"
(.--str-- self))]) (.--str-- self)))
(defmacro seq [param &rest seq-code] (defmacro seq [param &rest seq-code]
`(Sequence (fn ~param (do ~@seq-code)))) `(Sequence (fn ~param (do ~@seq-code))))

View File

@ -158,8 +158,8 @@
(assert (= (hy-repr (C)) "cuddles")) (assert (= (hy-repr (C)) "cuddles"))
(defclass Container [object] (defclass Container [object]
[__init__ (fn [self value] (defn __init__ [self value]
(setv self.value value))]) (setv self.value value)))
(hy-repr-register Container :placeholder "(Container ...)" (fn [x] (hy-repr-register Container :placeholder "(Container ...)" (fn [x]
(+ "(Container " (hy-repr x.value) ")"))) (+ "(Container " (hy-repr x.value) ")")))
(setv container (Container 5)) (setv container (Container 5))
@ -170,5 +170,5 @@
(defn test-hy-repr-fallback [] (defn test-hy-repr-fallback []
(defclass D [object] (defclass D [object]
[__repr__ (fn [self] "cuddles")]) (defn __repr__ [self] "cuddles"))
(assert (= (hy-repr (D)) "cuddles"))) (assert (= (hy-repr (D)) "cuddles")))

View File

@ -300,7 +300,7 @@ result['y in globals'] = 'y' in globals()")
(assert-requires-num inc) (assert-requires-num inc)
(defclass X [object] (defclass X [object]
[__add__ (fn [self other] (.format "__add__ got {}" other))]) (defn __add__ [self other] (.format "__add__ got {}" other)))
(assert-equal (inc (X)) "__add__ got 1")) (assert-equal (inc (X)) "__add__ got 1"))
(defn test-instance [] (defn test-instance []

View File

@ -27,7 +27,7 @@
(defn test-defclass-attrs [] (defn test-defclass-attrs []
"NATIVE: test defclass attributes" "NATIVE: test defclass attributes"
(defclass A [] (defclass A []
[x 42]) (setv x 42))
(assert (= A.x 42)) (assert (= A.x 42))
(assert (= (getattr (A) "x") 42))) (assert (= (getattr (A) "x") 42)))
@ -35,9 +35,9 @@
(defn test-defclass-attrs-fn [] (defn test-defclass-attrs-fn []
"NATIVE: test defclass attributes with fn" "NATIVE: test defclass attributes with fn"
(defclass B [] (defclass B []
[x 42 (setv x 42)
y (fn [self value] (setv y (fn [self value]
(+ self.x value))]) (+ self.x value))))
(assert (= B.x 42)) (assert (= B.x 42))
(assert (= (.y (B) 5) 47)) (assert (= (.y (B) 5) 47))
(setv b (B)) (setv b (B))
@ -48,17 +48,17 @@
(defn test-defclass-dynamic-inheritance [] (defn test-defclass-dynamic-inheritance []
"NATIVE: test defclass with dynamic inheritance" "NATIVE: test defclass with dynamic inheritance"
(defclass A [((fn [] (if True list dict)))] (defclass A [((fn [] (if True list dict)))]
[x 42]) (setv x 42))
(assert (isinstance (A) list)) (assert (isinstance (A) list))
(defclass A [((fn [] (if False list dict)))] (defclass A [((fn [] (if False list dict)))]
[x 42]) (setv x 42))
(assert (isinstance (A) dict))) (assert (isinstance (A) dict)))
(defn test-defclass-no-fn-leak [] (defn test-defclass-no-fn-leak []
"NATIVE: test defclass attributes with fn" "NATIVE: test defclass attributes with fn"
(defclass A [] (defclass A []
[x (fn [] 1)]) (setv x (fn [] 1)))
(try (try
(do (do
(x) (x)
@ -68,13 +68,13 @@
(defn test-defclass-docstring [] (defn test-defclass-docstring []
"NATIVE: test defclass docstring" "NATIVE: test defclass docstring"
(defclass A [] (defclass A []
[--doc-- "doc string" (setv --doc-- "doc string")
x 1]) (setv x 1))
(setv a (A)) (setv a (A))
(assert (= a.__doc__ "doc string")) (assert (= a.__doc__ "doc string"))
(defclass B [] (defclass B []
"doc string" "doc string"
[x 1]) (setv x 1))
(setv b (B)) (setv b (B))
(assert (= b.x 1)) (assert (= b.x 1))
(assert (= b.__doc__ "doc string")) (assert (= b.__doc__ "doc string"))
@ -82,7 +82,7 @@
"begin a very long multi-line string to make "begin a very long multi-line string to make
sure that it comes out the way we hope sure that it comes out the way we hope
and can span 3 lines end." and can span 3 lines end."
[x 1]) (setv x 1))
(setv mL (MultiLine)) (setv mL (MultiLine))
(assert (= mL.x 1)) (assert (= mL.x 1))
(assert (in "begin" mL.__doc__)) (assert (in "begin" mL.__doc__))
@ -100,8 +100,8 @@
"NATIVE: test defclass syntax with properties and methods and side-effects" "NATIVE: test defclass syntax with properties and methods and side-effects"
(setv foo 1) (setv foo 1)
(defclass A [] (defclass A []
[x 1 (setv x 1)
y 2] (setv y 2)
(global foo) (global foo)
(setv foo 2) (setv foo 2)
(defn greet [self] (defn greet [self]
@ -117,7 +117,7 @@
(defn test-defclass-implicit-none-for-init [] (defn test-defclass-implicit-none-for-init []
"NATIVE: test that defclass adds an implicit None to --init--" "NATIVE: test that defclass adds an implicit None to --init--"
(defclass A [] (defclass A []
[--init-- (fn [self] (setv self.x 1) 42)]) (setv --init-- (fn [self] (setv self.x 1) 42)))
(defclass B [] (defclass B []
(defn --init-- [self] (defn --init-- [self]
(setv self.x 2) (setv self.x 2)

View File

@ -129,8 +129,8 @@
(assert (none? (setv (get {} "x") 42))) (assert (none? (setv (get {} "x") 42)))
(setv l []) (setv l [])
(defclass Foo [object] (defclass Foo [object]
[__setattr__ (fn [self attr val] (defn __setattr__ [self attr val]
(.append l [attr val]))]) (.append l [attr val])))
(setv x (Foo)) (setv x (Foo))
(assert (none? (setv x.eggs "ham"))) (assert (none? (setv x.eggs "ham")))
(assert (not (hasattr x "eggs"))) (assert (not (hasattr x "eggs")))
@ -443,9 +443,9 @@
(defclass X [object] []) (defclass X [object] [])
(defclass M [object] (defclass M [object]
[meth (fn [self &rest args &kwargs kwargs] (defn meth [self &rest args &kwargs kwargs]
(.join " " (+ (, "meth") args (.join " " (+ (, "meth") args
(tuple (map (fn [k] (get kwargs k)) (sorted (.keys kwargs)))))))]) (tuple (map (fn [k] (get kwargs k)) (sorted (.keys kwargs))))))))
(setv x (X)) (setv x (X))
(setv m (M)) (setv m (M))
@ -1667,7 +1667,7 @@ macros()
(defn test-underscore_variables [] (defn test-underscore_variables []
; https://github.com/hylang/hy/issues/1340 ; https://github.com/hylang/hy/issues/1340
(defclass XYZ [] (defclass XYZ []
[_42 6]) (setv _42 6))
(setv x (XYZ)) (setv x (XYZ))
(assert (= (. x _42) 6))) (assert (= (. x _42) 6)))
@ -1773,24 +1773,24 @@ macros()
(defn test-pep-3115 [] (defn test-pep-3115 []
(defclass member-table [dict] (defclass member-table [dict]
[--init-- (fn [self] (setv self.member-names [])) (defn --init-- [self] (setv self.member-names []))
--setitem-- (fn [self key value] (defn --setitem-- [self key value]
(if (not-in key self) (if (not-in key self)
(.append self.member-names key)) (.append self.member-names key))
(dict.--setitem-- self key value))]) (dict.--setitem-- self key value)))
(defclass OrderedClass [type] (defclass OrderedClass [type]
[--prepare-- (classmethod (fn [metacls name bases] (member-table))) (setv --prepare-- (classmethod (fn [metacls name bases] (member-table))))
--new-- (fn [cls name bases classdict] (defn --new-- [cls name bases classdict]
(setv result (type.--new-- cls name bases (dict classdict))) (setv result (type.--new-- cls name bases (dict classdict)))
(setv result.member-names classdict.member-names) (setv result.member-names classdict.member-names)
result)]) result))
(defclass MyClass [:metaclass OrderedClass] (defclass MyClass [:metaclass OrderedClass]
[method1 (fn [self] (pass)) (defn method1 [self] (pass))
method2 (fn [self] (pass))]) (defn method2 [self] (pass)))
(assert (= (. (MyClass) member-names) (assert (= (. (MyClass) member-names)
["__module__" "__qualname__" "method1" "method2"]))) ["__module__" "__qualname__" "method1" "method2"])))

View File

@ -34,7 +34,7 @@
"NATIVE: test that unary + calls __pos__" "NATIVE: test that unary + calls __pos__"
(defclass X [object] (defclass X [object]
[__pos__ (fn [self] "called __pos__")]) (defn __pos__ [self] "called __pos__"))
(assert (= (+ (X)) "called __pos__")) (assert (= (+ (X)) "called __pos__"))
; Make sure the shadowed version works, too. ; Make sure the shadowed version works, too.
@ -159,8 +159,7 @@
(defclass HyTestMatrix [list] (defclass HyTestMatrix [list]
[--matmul-- (defn --matmul-- [self other]
(fn [self other]
(setv n (len self) (setv n (len self)
m (len (. other [0])) m (len (. other [0]))
result []) result [])
@ -173,7 +172,7 @@
(. other [k] [j])))) (. other [k] [j]))))
(.append result-row dot-product)) (.append result-row dot-product))
(.append result result-row)) (.append result result-row))
result)]) result))
(setv first-test-matrix (HyTestMatrix [[1 2 3] (setv first-test-matrix (HyTestMatrix [[1 2 3]
[4 5 6] [4 5 6]

View File

@ -34,7 +34,7 @@
(assert (= (f) 0)) (assert (= (f) 0))
(defclass C [object] [__pos__ (fn [self] "called __pos__")]) (defclass C [object] (defn __pos__ [self] "called __pos__"))
(assert (= (f (C)) "called __pos__")) (assert (= (f (C)) "called __pos__"))
(assert (= (f 1 2) 3)) (assert (= (f 1 2) 3))
@ -101,9 +101,9 @@
(op-and-shadow-test @ (op-and-shadow-test @
(defclass C [object] [ (defclass C [object]
__init__ (fn [self content] (setv self.content content)) (defn __init__ [self content] (setv self.content content))
__matmul__ (fn [self other] (C (+ self.content other.content)))]) (defn __matmul__ [self other] (C (+ self.content other.content))))
(forbid (f)) (forbid (f))
(assert (do (setv c (C "a")) (is (f c) c))) (assert (do (setv c (C "a")) (is (f c) c)))
(assert (= (. (f (C "b") (C "c")) content) "bc")) (assert (= (. (f (C "b") (C "c")) content) "bc"))
@ -171,11 +171,11 @@
; Make sure chained comparisons use `and`, not `&`. ; Make sure chained comparisons use `and`, not `&`.
; https://github.com/hylang/hy/issues/1191 ; https://github.com/hylang/hy/issues/1191
(defclass C [object] [ (defclass C [object]
__init__ (fn [self x] (defn __init__ [self x]
(setv self.x x)) (setv self.x x))
__lt__ (fn [self other] (defn __lt__ [self other]
self.x)]) self.x))
(assert (= (f (C "a") (C "b") (C "c")) "b"))) (assert (= (f (C "a") (C "b") (C "c")) "b")))

View File

@ -40,8 +40,8 @@
(defn test-pep-487 [] (defn test-pep-487 []
(defclass QuestBase [] (defclass QuestBase []
[--init-subclass-- (fn [cls swallow &kwargs kwargs] (defn --init-subclass-- [cls swallow &kwargs kwargs]
(setv cls.swallow swallow))]) (setv cls.swallow swallow)))
(defclass Quest [QuestBase :swallow "african"]) (defclass Quest [QuestBase :swallow "african"])
(assert (= (. (Quest) swallow) "african"))) (assert (= (. (Quest) swallow) "african")))

View File

@ -27,7 +27,7 @@
cls) cls)
(with-decorator bardec (with-decorator bardec
(defclass cls [] (defclass cls []
[attr1 123])) (setv attr1 123)))
(assert (= cls.attr1 123)) (assert (= cls.attr1 123))
(assert (= cls.attr2 456))) (assert (= cls.attr2 456)))

View File

@ -142,13 +142,13 @@ Call me Ishmael. Some years ago—never mind how long precisely—having little
(defclass C2 [C1] (defclass C2 [C1]
"class docstring" "class docstring"
[attr1 5 attr2 6] (setv attr1 5)
(setv attr3 7)) (setv attr2 6))
(import [contextlib [closing]]) (import [contextlib [closing]])
(setv closed []) (setv closed [])
(defclass Closeable [] (defclass Closeable []
[close (fn [self] (.append closed self.x))]) (defn close [self] (.append closed self.x)))
(with [c1 (closing (Closeable)) c2 (closing (Closeable))] (with [c1 (closing (Closeable)) c2 (closing (Closeable))]
(setv c1.x "v1") (setv c1.x "v1")
(setv c2.x "v2")) (setv c2.x "v2"))

View File

@ -113,7 +113,7 @@ def assert_stuff(m):
assert m.C2.__doc__ == "class docstring" assert m.C2.__doc__ == "class docstring"
assert issubclass(m.C2, m.C1) assert issubclass(m.C2, m.C1)
assert (m.C2.attr1, m.C2.attr2, m.C2.attr3) == (5, 6, 7) assert (m.C2.attr1, m.C2.attr2) == (5, 6)
assert m.closed == ["v2", "v1"] assert m.closed == ["v2", "v1"]