Skip to content

Commit 1fdabfd

Browse files
committed
Check membership
1 parent a5f22b7 commit 1fdabfd

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
@@ -6853,13 +6853,21 @@
68536853
"Generate a proxy class with the given bases."
68546854
[bases]
68556855
(let [methods (apply hash-map (mapcat proxy-base-methods bases))
6856+
method-names (set (map munge (keys methods)))
68566857
base-methods {"__init__" (fn __init__ [self proxy-mappings & args]
68576858
(apply (.- (python/super (.- self __class__) self) __init__) args)
6858-
(set! (.- self -proxy-mappings) proxy-mappings)
6859+
(. self (_set_proxy_mappings proxy-mappings))
68596860
nil)
68606861
"_get_proxy_mappings" (fn _get_proxy_mappings [self]
68616862
(.- self -proxy-mappings))
68626863
"_set_proxy_mappings" (fn _set_proxy_mappings [self proxy-mappings]
6864+
(let [provided-methods (set (keys proxy-mappings))]
6865+
(when-not (.issubset provided-methods method-names)
6866+
(throw
6867+
(ex-info "Proxy override methods must correspond to methods on the declared supertypes"
6868+
{:expected method-names
6869+
:given provided-methods
6870+
:diff (.difference provided-methods method-names)}))))
68636871
(set! (.- self -proxy-mappings) proxy-mappings)
68646872
nil)
68656873
"_update_proxy_mappings" (fn _update_proxy_mappings [self proxy-mappings]
@@ -6876,9 +6884,7 @@
68766884

68776885
;; Remove Python ``object`` from the bases if it is present to avoid errors
68786886
;; about creating a consistent MRO for the given bases
6879-
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
6880-
#_(doseq [[method-name method] methods]
6881-
(println method-name method))
6887+
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
68826888
(python/type (basilisp.lang.util/genname "Proxy")
68836889
(python/tuple proxy-bases)
68846890
(python/dict (merge methods base-methods)))))
@@ -6962,7 +6968,6 @@
69626968
(. proxy (_update-proxy-mappings mappings))
69636969
proxy)))
69646970

6965-
;; TODO: check interface/superclass method membership
69666971
(defmacro proxy
69676972
"Create a new proxy class instance.
69686973

@@ -7033,7 +7038,6 @@
70337038
(assoc m method-name method)))
70347039
{}
70357040
methods)]
7036-
#_(println methods)
70377041
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
70387042

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