Skip to content

Commit 97e6069

Browse files
committed
Check membership
1 parent b73ef0d commit 97e6069

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
@@ -6821,13 +6821,21 @@
68216821
"Generate a proxy class with the given bases."
68226822
[bases]
68236823
(let [methods (apply hash-map (mapcat proxy-base-methods bases))
6824+
method-names (set (map munge (keys methods)))
68246825
base-methods {"__init__" (fn __init__ [self proxy-mappings & args]
68256826
(apply (.- (python/super (.- self __class__) self) __init__) args)
6826-
(set! (.- self -proxy-mappings) proxy-mappings)
6827+
(. self (_set_proxy_mappings proxy-mappings))
68276828
nil)
68286829
"_get_proxy_mappings" (fn _get_proxy_mappings [self]
68296830
(.- self -proxy-mappings))
68306831
"_set_proxy_mappings" (fn _set_proxy_mappings [self proxy-mappings]
6832+
(let [provided-methods (set (keys proxy-mappings))]
6833+
(when-not (.issubset provided-methods method-names)
6834+
(throw
6835+
(ex-info "Proxy override methods must correspond to methods on the declared supertypes"
6836+
{:expected method-names
6837+
:given provided-methods
6838+
:diff (.difference provided-methods method-names)}))))
68316839
(set! (.- self -proxy-mappings) proxy-mappings)
68326840
nil)
68336841
"_update_proxy_mappings" (fn _update_proxy_mappings [self proxy-mappings]
@@ -6844,9 +6852,7 @@
68446852

68456853
;; Remove Python ``object`` from the bases if it is present to avoid errors
68466854
;; about creating a consistent MRO for the given bases
6847-
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
6848-
#_(doseq [[method-name method] methods]
6849-
(println method-name method))
6855+
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
68506856
(python/type (basilisp.lang.util/genname "Proxy")
68516857
(python/tuple proxy-bases)
68526858
(python/dict (merge methods base-methods)))))
@@ -6930,7 +6936,6 @@
69306936
(. proxy (_update-proxy-mappings mappings))
69316937
proxy)))
69326938

6933-
;; TODO: check interface/superclass method membership
69346939
(defmacro proxy
69356940
"Create a new proxy class instance.
69366941

@@ -7001,7 +7006,6 @@
70017006
(assoc m method-name method)))
70027007
{}
70037008
methods)]
7004-
#_(println methods)
70057009
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
70067010

70077011
(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)