Skip to content

Commit 8456d38

Browse files
authored
Fix import alias issue after defining new ns (#7)
1 parent b2dd50b commit 8456d38

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Set minimum Basilisp version to >=0.4.0
6+
- Fix import alias resolution after defining a new namespace (#6)
7+
58
## 1.2.0
69

710
- Added nREPL Server support.

basilisp_kernel/kernel.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
ctx = compiler.CompilerContext(filename="basilisp-kernel", opts=opts)
1818
eof = object()
1919

20-
user_ns = runtime.Namespace.get_or_create(sym.symbol("user"))
2120
core_ns = runtime.Namespace.get(runtime.CORE_NS_SYM)
22-
cli.eval_str("(ns user (:require clojure.core))", ctx, core_ns, eof)
21+
ns_dyn_var = runtime.Var.find_safe(sym.symbol("*ns*", ns=runtime.CORE_NS))
2322

2423
_DELIMITED_WORD_PATTERN = re.compile(r"[\[\](){\}\s]+")
2524

@@ -29,7 +28,7 @@ def do_execute(code):
2928
runtime.Var.find_safe(sym.symbol("*err*", ns=runtime.CORE_NS)) : sys.stderr
3029
}):
3130
try:
32-
return cli.eval_str(code, ctx, user_ns, eof)
31+
return cli.eval_str(code, ctx, ns_dyn_var.deref(), eof)
3332
except reader.SyntaxError as e:
3433
msg = "".join(format_exception(e, reader.SyntaxError, e.__traceback__))
3534
raise reader.SyntaxError(msg) from None
@@ -54,6 +53,10 @@ class BasilispKernel(IPythonKernel):
5453
def __init__(self, **kwargs):
5554
super().__init__(**kwargs)
5655
self.imported = False
56+
runtime.set_current_ns("user")
57+
with runtime.ns_bindings("user") as ns:
58+
ns.refer_all(core_ns)
59+
5760

5861
def do_complete(self, acode, cursor_pos):
5962
code = acode[:cursor_pos]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ requires-python = ">=3.9"
2323
dependencies = [
2424
"ipykernel",
2525
"jupyter_client",
26-
"basilisp>=0.3.2",
26+
"basilisp>=0.4.0",
2727
"basilisp-nrepl-async>=0.1.0",
2828
]
2929

tests/basilisp_kernel/integration/notebook_test.lpy

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
[{:metadata {},
4343
:output_type "execute_result",
4444
:execution_count 1,
45-
:data {(keyword nil "text/plain") "13"}}]
45+
:data {:text/plain "13"}}]
4646
:execution_count 1,
4747
:source "(+ 4 9)",}]
4848
(for [cell cells]
49-
(reduce dissoc cell [:id :metadata]))))))
49+
(reduce dissoc cell [:id :metadata]))))))
5050
(finally
5151
(when (.is-alive km) (.shutdown-kernel km ** :now true))))))
5252
#_(pp/pprint (notebook-execute-test))
@@ -85,12 +85,12 @@
8585
(is *notebook*)
8686
(is *client*)
8787
(let [{:keys [cells]} (py->lisp (.execute *client*))]
88-
(is (= {(keyword nil "text/plain") "39"} (get-in cells [0 :outputs 0 :data])) cells))))
88+
(is (= {:text/plain "39"} (get-in cells [0 :outputs 0 :data])) cells))))
8989
#_(pp/pprint (with-notebook-test))
9090

9191
(defn code-output-text-get
9292
[cell]
93-
(get-in cell [:outputs 0 :data (keyword nil "text/plain")]))
93+
(get-in cell [:outputs 0 :data :text/plain]))
9494

9595
(defn cell-output-get
9696
"Returns the `CELL`'s :outputs in the following format according to their `output_type`:
@@ -108,7 +108,7 @@
108108
(let [{nm :name text :text} output]
109109
(assoc-in acc [:stream nm] text))
110110
"execute_result"
111-
(let [text (get-in output [:data (keyword nil "text/plain")])]
111+
(let [text (get-in output [:data :text/plain])]
112112
(assoc-in acc [:result :text] text))
113113
))
114114
{} (:outputs cell)))
@@ -126,6 +126,18 @@
126126
#_(pp/pprint (basilisp-notebook-test))
127127

128128

129+
(deftest ns-change-import-test
130+
(testing "defining a new namespace and using import aliases"
131+
(with-notebook '[(ns test.test)
132+
(import [math :as m])
133+
(m/sqrt 4)]
134+
(let [{:keys [cells]} (py->lisp (.execute *client*))]
135+
(are [i result] (= result (cell-output-get (aget cells i)))
136+
0 {}
137+
1 {}
138+
2 {:result {:text "2.0"}})))))
139+
#_(pp/pprint (ns-change-import-test))
140+
129141
(deftest nrepl-server-test
130142
(testing "starting server in notebook and remote changing atom"
131143
(with-notebook (concat [(list 'def 'tempdir *tempdir*)]

0 commit comments

Comments
 (0)