diff --git a/CHANGELOG.md b/CHANGELOG.md index 0192d2729..478d21ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Fix an issue with `basilisp test` standard streams output that can lead to failures on MS-Windows (#1080) * Fix an issue where destructuring a vector would throw an exception rather than returning `nil` for invalid key types (#1090) * Fix an issue where destructuring default values would take precedence over falsey values in the source data structure (#1078) + * Fixed a bug where imported Python names containing `-` (in lieu of `_`) could not be referenced using the `-` syntax (#1085) ### Removed * Removed support for Python 3.8 (#1083) diff --git a/src/basilisp/lang/compiler/analyzer.py b/src/basilisp/lang/compiler/analyzer.py index 50ba93b8b..eab37ce76 100644 --- a/src/basilisp/lang/compiler/analyzer.py +++ b/src/basilisp/lang/compiler/analyzer.py @@ -3505,8 +3505,13 @@ def __resolve_namespaced_symbol_in_ns( namespace portion""" assert form.ns is not None + # Import names are always munged by the compiler when they're added to imports, + # but if the user provides an import alias that is left untouched. Check for + # the munged symbol in `Namespace.imports` and the unmunged in + # `Namespace.import_aliases`. ns_sym = sym.symbol(form.ns) - if ns_sym in which_ns.imports or ns_sym in which_ns.import_aliases: + import_sym = sym.symbol(munge(form.ns)) + if import_sym in which_ns.imports or ns_sym in which_ns.import_aliases: # Fetch the full namespace name for the aliased namespace/module. # We don't need this for actually generating the link later, but # we _do_ need it for fetching a reference to the module to check @@ -3516,7 +3521,7 @@ def __resolve_namespaced_symbol_in_ns( assert ns is not None ns_name = ns.name else: - ns_name = ns_sym.name + ns_name = import_sym.name safe_module_name = munge(ns_name) assert ( diff --git a/tests/basilisp/compiler_test.py b/tests/basilisp/compiler_test.py index dcd4fac84..aed9aba9c 100644 --- a/tests/basilisp/compiler_test.py +++ b/tests/basilisp/compiler_test.py @@ -5,6 +5,7 @@ import inspect import logging import os +import pathlib import re import sys import textwrap @@ -6387,6 +6388,19 @@ def test_aliased_namespace_not_hidden_by_python_module( monkeypatch.chdir(cwd) os.unlink(module_file_path) + def test_import_name_with_underscores_resolves_properly( + self, + lcompile: CompileFn, + monkeypatch: pytest.MonkeyPatch, + tmp_path: pathlib.Path, + ): + package = tmp_path / "a_b" + package.mkdir(parents=True) + file = package / "c.py" + file.write_text("val = 10") + monkeypatch.syspath_prepend(str(tmp_path)) + assert lcompile("(import a-b.c) [a-b.c/val a_b.c/val]") == vec.v(10, 10) + @pytest.mark.parametrize( "code", [