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]]
[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.__doc__ f.__doc__)
(unless (in f.__name__ (.keys (get self._fns f.__module__)))
@ -18,14 +18,14 @@
(setv values f.__code__.co_varnames)
(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"
(setv com (+ (list args) (list (.keys kwargs))))
(and
(= (len com) (len v))
(.issubset (frozenset (.keys kwargs)) com)))
__call__ (fn [self &rest args &kwargs kwargs]
(defn __call__ [self &rest args &kwargs kwargs]
(setv func None)
(for [[i f] (.items (get self._fns self.f.__module__ self.f.__name__))]
(when (.fn? self i args kwargs)
@ -33,7 +33,7 @@
(break)))
(if func
(func #* args #** kwargs)
(raise (TypeError "No matching functions with this signature"))))])
(raise (TypeError "No matching functions with this signature")))))
(defn multi-decorator [dispatch-fn]
(setv inner (fn [&rest args &kwargs kwargs]

View File

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

View File

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

View File

@ -300,7 +300,7 @@ result['y in globals'] = 'y' in globals()")
(assert-requires-num inc)
(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"))
(defn test-instance []

View File

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

View File

@ -129,8 +129,8 @@
(assert (none? (setv (get {} "x") 42)))
(setv l [])
(defclass Foo [object]
[__setattr__ (fn [self attr val]
(.append l [attr val]))])
(defn __setattr__ [self attr val]
(.append l [attr val])))
(setv x (Foo))
(assert (none? (setv x.eggs "ham")))
(assert (not (hasattr x "eggs")))
@ -443,9 +443,9 @@
(defclass X [object] [])
(defclass M [object]
[meth (fn [self &rest args &kwargs kwargs]
(defn meth [self &rest args &kwargs kwargs]
(.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 m (M))
@ -1667,7 +1667,7 @@ macros()
(defn test-underscore_variables []
; https://github.com/hylang/hy/issues/1340
(defclass XYZ []
[_42 6])
(setv _42 6))
(setv x (XYZ))
(assert (= (. x _42) 6)))
@ -1773,24 +1773,24 @@ macros()
(defn test-pep-3115 []
(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)
(.append self.member-names key))
(dict.--setitem-- self key value))])
(dict.--setitem-- self key value)))
(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.member-names classdict.member-names)
result)])
result))
(defclass MyClass [:metaclass OrderedClass]
[method1 (fn [self] (pass))
method2 (fn [self] (pass))])
(defn method1 [self] (pass))
(defn method2 [self] (pass)))
(assert (= (. (MyClass) member-names)
["__module__" "__qualname__" "method1" "method2"])))

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@
cls)
(with-decorator bardec
(defclass cls []
[attr1 123]))
(setv attr1 123)))
(assert (= cls.attr1 123))
(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]
"class docstring"
[attr1 5 attr2 6]
(setv attr3 7))
(setv attr1 5)
(setv attr2 6))
(import [contextlib [closing]])
(setv closed [])
(defclass Closeable []
[close (fn [self] (.append closed self.x))])
(defn close [self] (.append closed self.x)))
(with [c1 (closing (Closeable)) c2 (closing (Closeable))]
(setv c1.x "v1")
(setv c2.x "v2"))

View File

@ -113,7 +113,7 @@ def assert_stuff(m):
assert m.C2.__doc__ == "class docstring"
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"]