Skip to content

Commit ff7b92b

Browse files
committed
Check membership
1 parent 2bd6256 commit ff7b92b

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
@@ -6887,13 +6887,21 @@
68876887
"Generate a proxy class with the given bases."
68886888
[bases]
68896889
(let [methods (apply hash-map (mapcat proxy-base-methods bases))
6890+
method-names (set (map munge (keys methods)))
68906891
base-methods {"__init__" (fn __init__ [self proxy-mappings & args]
68916892
(apply (.- (python/super (.- self __class__) self) __init__) args)
6892-
(set! (.- self -proxy-mappings) proxy-mappings)
6893+
(. self (_set_proxy_mappings proxy-mappings))
68936894
nil)
68946895
"_get_proxy_mappings" (fn _get_proxy_mappings [self]
68956896
(.- self -proxy-mappings))
68966897
"_set_proxy_mappings" (fn _set_proxy_mappings [self proxy-mappings]
6898+
(let [provided-methods (set (keys proxy-mappings))]
6899+
(when-not (.issubset provided-methods method-names)
6900+
(throw
6901+
(ex-info "Proxy override methods must correspond to methods on the declared supertypes"
6902+
{:expected method-names
6903+
:given provided-methods
6904+
:diff (.difference provided-methods method-names)}))))
68976905
(set! (.- self -proxy-mappings) proxy-mappings)
68986906
nil)
68996907
"_update_proxy_mappings" (fn _update_proxy_mappings [self proxy-mappings]
@@ -6910,9 +6918,7 @@
69106918

69116919
;; Remove Python ``object`` from the bases if it is present to avoid errors
69126920
;; about creating a consistent MRO for the given bases
6913-
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
6914-
#_(doseq [[method-name method] methods]
6915-
(println method-name method))
6921+
proxy-bases (concat (remove #{python/object} bases) [basilisp.lang.interfaces/IProxy])]
69166922
(python/type (basilisp.lang.util/genname "Proxy")
69176923
(python/tuple proxy-bases)
69186924
(python/dict (merge methods base-methods)))))
@@ -6996,7 +7002,6 @@
69967002
(. proxy (_update-proxy-mappings mappings))
69977003
proxy)))
69987004

6999-
;; TODO: check interface/superclass method membership
70007005
(defmacro proxy
70017006
"Create a new proxy class instance.
70027007

@@ -7067,7 +7072,6 @@
70677072
(assoc m method-name method)))
70687073
{}
70697074
methods)]
7070-
#_(println methods)
70717075
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
70727076

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