Skip to content

Commit 018e935

Browse files
committed
Check membership
1 parent 3090f18 commit 018e935

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
@@ -6944,13 +6944,21 @@
69446944
"Generate a proxy class with the given bases."
69456945
[bases]
69466946
(let [methods (apply hash-map (mapcat proxy-base-methods bases))
6947+
method-names (set (map munge (keys methods)))
69476948
base-methods {"__init__" (fn __init__ [self proxy-mappings & args]
69486949
(apply (.- (python/super (.- self __class__) self) __init__) args)
6949-
(set! (.- self -proxy-mappings) proxy-mappings)
6950+
(. self (_set_proxy_mappings proxy-mappings))
69506951
nil)
69516952
"_get_proxy_mappings" (fn _get_proxy_mappings [self]
69526953
(.- self -proxy-mappings))
69536954
"_set_proxy_mappings" (fn _set_proxy_mappings [self proxy-mappings]
6955+
(let [provided-methods (set (keys proxy-mappings))]
6956+
(when-not (.issubset provided-methods method-names)
6957+
(throw
6958+
(ex-info "Proxy override methods must correspond to methods on the declared supertypes"
6959+
{:expected method-names
6960+
:given provided-methods
6961+
:diff (.difference provided-methods method-names)}))))
69546962
(set! (.- self -proxy-mappings) proxy-mappings)
69556963
nil)
69566964
"_update_proxy_mappings" (fn _update_proxy_mappings [self proxy-mappings]
@@ -6967,9 +6975,7 @@
69676975

69686976
;; Remove Python ``object`` from the bases if it is present to avoid errors
69696977
;; about creating a consistent MRO for the given bases
6970-
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
6971-
#_(doseq [[method-name method] methods]
6972-
(println method-name method))
6978+
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
69736979
(python/type (basilisp.lang.util/genname "Proxy")
69746980
(python/tuple proxy-bases)
69756981
(python/dict (merge methods base-methods)))))
@@ -7053,7 +7059,6 @@
70537059
(. proxy (_update-proxy-mappings mappings))
70547060
proxy)))
70557061

7056-
;; TODO: check interface/superclass method membership
70577062
(defmacro proxy
70587063
"Create a new proxy class instance.
70597064

@@ -7124,7 +7129,6 @@
71247129
(assoc m method-name method)))
71257130
{}
71267131
methods)]
7127-
#_(println methods)
71287132
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
71297133

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