Skip to content

Commit 6152cd9

Browse files
committed
Check membership
1 parent a0fff5a commit 6152cd9

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/basilisp/core.lpy

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6993,13 +6993,21 @@
69936993
"Generate a proxy class with the given bases."
69946994
[bases]
69956995
(let [methods (apply hash-map (mapcat proxy-base-methods bases))
6996+
method-names (set (map munge (keys methods)))
69966997
base-methods {"__init__" (fn __init__ [self proxy-mappings & args]
69976998
(apply (.- (python/super (.- self __class__) self) __init__) args)
6998-
(set! (.- self -proxy-mappings) proxy-mappings)
6999+
(. self (_set_proxy_mappings proxy-mappings))
69997000
nil)
70007001
"_get_proxy_mappings" (fn _get_proxy_mappings [self]
70017002
(.- self -proxy-mappings))
70027003
"_set_proxy_mappings" (fn _set_proxy_mappings [self proxy-mappings]
7004+
(let [provided-methods (set (keys proxy-mappings))]
7005+
(when-not (.issubset provided-methods method-names)
7006+
(throw
7007+
(ex-info "Proxy override methods must correspond to methods on the declared supertypes"
7008+
{:expected method-names
7009+
:given provided-methods
7010+
:diff (.difference provided-methods method-names)}))))
70037011
(set! (.- self -proxy-mappings) proxy-mappings)
70047012
nil)
70057013
"_update_proxy_mappings" (fn _update_proxy_mappings [self proxy-mappings]
@@ -7016,9 +7024,7 @@
70167024

70177025
;; Remove Python ``object`` from the bases if it is present to avoid errors
70187026
;; about creating a consistent MRO for the given bases
7019-
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
7020-
#_(doseq [[method-name method] methods]
7021-
(println method-name method))
7027+
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
70227028
(python/type (basilisp.lang.util/genname "Proxy")
70237029
(python/tuple proxy-bases)
70247030
(python/dict (merge methods base-methods)))))
@@ -7102,7 +7108,6 @@
71027108
(. proxy (_update-proxy-mappings mappings))
71037109
proxy)))
71047110

7105-
;; TODO: check interface/superclass method membership
71067111
(defmacro proxy
71077112
"Create a new proxy class instance.
71087113

@@ -7173,7 +7178,6 @@
71737178
(assoc m method-name method)))
71747179
{}
71757180
methods)]
7176-
#_(println methods)
71777181
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
71787182

71797183
(defmacro proxy-super

tests/basilisp/test_proxies.lpy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
(describe-me [] "I'm a proxy")
4343
(describe-me [] "Proxy"))))))
4444

45+
(testing "disallows overriding non-superclass methods"
46+
(is (thrown? basilisp.lang.exception/ExceptionInfo
47+
(proxy [Describable] []
48+
(other-method [] "Proxy")))))
49+
4550
(testing "multi-arity interface methods"
4651
(let [p (proxy [ConcreteToString] [1]
4752
(to-string

0 commit comments

Comments
 (0)