Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adress issue with intern not working #725

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix `(is (= exp act))` should only evaluate its args once on failure (#712).
* Fix issue with `with` failing with a traceback error when an exception is thrown (#714).
* Fix issue with `sort-*` family of funtions returning an error on an empty seq (#716).
* Fix issue with `intern` failing when used (#725).

## [v0.1.0a2]
### Added
Expand Down
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sphinx~=4.4.0
sphinx~=4.4.0
sphinx-rtd-theme==1.3.0
10 changes: 5 additions & 5 deletions src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -4533,13 +4533,13 @@
the root binding to ``val``\\, if provided. The namespace must exist. Return the Var."
([ns name]
(let [ns (the-ns ns)
v (basilisp.lang.runtime/Var ns name)]
(.intern ns v)))
v (basilisp.lang.runtime/Var ns name ** :meta {:ns ns :name name})]
(.intern ns name v)))
([ns name val]
(let [ns (the-ns ns)
v (basilisp.lang.runtime/Var ns name)]
(.set-root v val)
(.intern ns v))))
v (basilisp.lang.runtime/Var ns name ** :meta {:ns ns :name name})]
(.bind-root v val)
(.intern ns name v))))

(defn ^:inline create-ns
"Create a Namespace with the name ``ns-sym`` or return the existing one if it already
Expand Down
2 changes: 1 addition & 1 deletion src/basilisp/lang/compiler/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def __init__(
self._func_ctx: Deque[FunctionContext] = collections.deque([])
self._is_quoted: Deque[bool] = collections.deque([])
self._opts = (
Maybe(opts).map(lmap.map).or_else_get(lmap.PersistentMap.empty()) # type: ignore
Maybe(opts).map(lmap.map).or_else_get(lmap.PersistentMap.empty()) # type: ignore[arg-type, unused-ignore]
)
self._recur_points: Deque[RecurPoint] = collections.deque([])
self._should_macroexpand = should_macroexpand
Expand Down
2 changes: 1 addition & 1 deletion src/basilisp/lang/compiler/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3527,7 +3527,7 @@ def _const_type_to_py_ast(form: IType, ctx: GeneratorContext) -> GeneratedPyAST:

ctor_args = []
ctor_arg_deps: List[ast.AST] = []
for field in attr.fields(tp): # type: ignore[arg-type]
for field in attr.fields(tp): # type: ignore[arg-type, misc, unused-ignore]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for field in attr.fields(tp): # type: ignore[arg-type, misc, unused-ignore]
for field in attr.fields(tp):

I think you can just do this rather than adding unused-ignore code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately ignore by itself does not suffice if there is an unused-ignore error reported in some python versions (e.g. 3.8) but not others (3.11), it has to be added to the list:

please see below how type: ignore reports an unused-ignore error for runtime.py in 3.8 but not in 3.11.

3.8:

py38-mypy: commands[0]> mypy --config-file=C:\src\basilisp/pyproject.toml -p basilisp
src\basilisp\lang\runtime.py:1958: error: Unused "type: ignore" comment  [unused-ignore]
src\basilisp\lang\compiler\generator.py:3530: error: Unused "type: ignore[misc]" comment  [unused-ignore]
src\basilisp\lang\compiler\analyzer.py:336: error: Unused "type: ignore" comment  [unused-ignore]
Found 3 errors in 3 files (checked 47 source files)

3.11

py311-mypy: commands[0]> mypy --config-file=C:\src\basilisp/pyproject.toml -p basilisp
src\basilisp\lang\runtime.py:1958: error: Argument 1 to "intern" of "Var" has incompatible type "Namespace | None"; expected "Namespace | Symbol"  [arg-type]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's frustrating for us. Thanks for the explanation.

field_nodes = _const_val_to_py_ast(getattr(form, field.name, None), ctx)
ctor_args.append(field_nodes.node)
ctor_args.extend(field_nodes.dependencies)
Expand Down
2 changes: 1 addition & 1 deletion src/basilisp/lang/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ def add_generated_python(
which_ns = get_current_ns()
v = Maybe(which_ns.find(sym.symbol(GENERATED_PYTHON_VAR_NAME))).or_else(
lambda: Var.intern(
which_ns, # type: ignore
which_ns, # type: ignore[arg-type, unused-ignore]
sym.symbol(GENERATED_PYTHON_VAR_NAME),
"",
dynamic=True,
Expand Down
33 changes: 33 additions & 0 deletions tests/basilisp/test_core_fns.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,39 @@
;; a valid symbol in that namespace
(is (= (resolve 'basilisp.shell/sh) (requiring-resolve 'basilisp.shell/sh))))

(deftest intern-test
(let [ns-sym (gensym "intern-test-ns")
ns0 (create-ns ns-sym)]
(testing "unbound"
(let [sym (gensym "intern-test")
v (intern ns0 sym)
{:keys [name ns] :as m} (meta v)]
(is (= ns0 ns))
(is (= sym name))
(is (= false (.-is-bound v)))))

(testing "unbound ns-sym"
(let [sym (gensym "intern-test")
v (intern ns-sym sym)
{:keys [name ns] :as m} (meta v)]
(is (= ns0 ns))
(is (= sym name))
(is (= false (.-is-bound v)))))

(testing "bound"
(let [sym (gensym "intern-test")
v (intern ns0 sym 5)
{:keys [name ns] :as m} (meta v)]
(is (= ns0 ns))
(is (= sym name))
(is (= true (.-is-bound v)))
(is (= 5 @v))
(is (= v (find-var (symbol (str ns0) (str sym))))))))


(testing "ns-not-exists"
(is (thrown? python/AttributeError (intern 'ns-non-existant 'xyz)))))

;;;;;;;;;;;;;;;;;
;; Hierarchies ;;
;;;;;;;;;;;;;;;;;
Expand Down
Loading