Skip to content

Commit f643d88

Browse files
authored
Fix a bug where import refers would incorrectly be applied to all import modules in the same form (#1285)
Fixes #1274
1 parent ab5b46a commit f643d88

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
* Added support for Python 3.14 (#1282)
1010

11+
### Fixed
12+
* Fix a bug where `import` refers would incorrectly be applied to all import modules in the same form (#1274)
13+
1114
### Removed
1215
* Removed support for Python 3.9 (#1283)
1316

@@ -33,7 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3336
### Fixed
3437
* Fix a bug where protocols with methods with leading hyphens in method names could not be defined (#1230)
3538
* Fix a bug where attempting to `:refer` a non-existent Var from another namespace would throw an unhelpful exception (#1231)
36-
* Fixed a bug where `(range 0)` would return `(0)` rather than than `()` as expected (#1258)
39+
* Fix a bug where `(range 0)` would return `(0)` rather than `()` as expected (#1258)
3740

3841
## [v0.3.8]
3942
### Added

src/basilisp/lang/compiler/analyzer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,8 +2574,9 @@ def _do_warn_on_import_or_require_name_clash(
25742574
def _import_ast(form: ISeq, ctx: AnalyzerContext) -> Import:
25752575
assert form.first == SpecialForm.IMPORT
25762576

2577-
aliases, refers, refer_all = [], [], False
2577+
aliases = []
25782578
for f in form.rest: # pylint: disable=too-many-nested-blocks
2579+
refers, refer_all = [], False
25792580
if isinstance(f, sym.Symbol):
25802581
module_name = f
25812582
module_alias = None
@@ -2678,6 +2679,8 @@ def _import_ast(form: ISeq, ctx: AnalyzerContext) -> Import:
26782679
form=f,
26792680
name=module_name.name,
26802681
alias=module_alias,
2682+
refers=refers,
2683+
refer_all=refer_all,
26812684
env=ctx.get_node_env(),
26822685
)
26832686
)
@@ -2691,8 +2694,6 @@ def _import_ast(form: ISeq, ctx: AnalyzerContext) -> Import:
26912694
return Import(
26922695
form=form,
26932696
aliases=aliases,
2694-
refers=refers,
2695-
refer_all=refer_all,
26962697
env=ctx.get_node_env(pos=ctx.syntax_position),
26972698
)
26982699

src/basilisp/lang/compiler/generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,7 @@ def _import_to_py_ast(ctx: GeneratorContext, node: Import) -> GeneratedPyAST[ast
24112411
)
24122412

24132413
refers: Optional[ast.expr] = None
2414-
if node.refer_all:
2414+
if alias.refer_all:
24152415
key, val = genname("k"), genname("v")
24162416
refers = ast.DictComp(
24172417
key=ast.Call(
@@ -2447,10 +2447,10 @@ def _import_to_py_ast(ctx: GeneratorContext, node: Import) -> GeneratedPyAST[ast
24472447
)
24482448
],
24492449
)
2450-
elif node.refers:
2450+
elif alias.refers:
24512451
refer_keys: list[Optional[ast.expr]] = []
24522452
refer_vals: list[ast.expr] = []
2453-
for refer in node.refers:
2453+
for refer in alias.refers:
24542454
refer_keys.append(
24552455
ast.Call(
24562456
func=_NEW_SYM_FN_NAME, args=[ast.Constant(refer)], keywords=[]

src/basilisp/lang/compiler/nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ class If(Node[SpecialForm]):
613613
class Import(Node[SpecialForm]):
614614
form: SpecialForm
615615
aliases: Iterable["ImportAlias"]
616-
refers: Iterable[str]
617-
refer_all: bool
618616
env: NodeEnv = attr.field(hash=False)
619617
children: Sequence[kw.Keyword] = vec.EMPTY
620618
op: NodeOp = NodeOp.IMPORT
@@ -627,6 +625,8 @@ class ImportAlias(Node[Union[sym.Symbol, vec.PersistentVector]]):
627625
form: Union[sym.Symbol, vec.PersistentVector]
628626
name: str
629627
alias: Optional[str]
628+
refers: Iterable[str]
629+
refer_all: bool
630630
env: NodeEnv = attr.field(hash=False)
631631
children: Sequence[kw.Keyword] = vec.EMPTY
632632
op: NodeOp = NodeOp.IMPORT_ALIAS

tests/basilisp/compiler_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,6 +3572,17 @@ def test_multi_import(self, lcompile: CompileFn):
35723572
)
35733573
)
35743574

3575+
def test_multi_import_with_refer(self, lcompile: CompileFn):
3576+
import sys
3577+
from os import mkdir
3578+
3579+
assert [sys, mkdir] == list(
3580+
lcompile("(import sys [os :refer [mkdir]]) [sys mkdir]")
3581+
)
3582+
assert [sys, mkdir] == list(
3583+
lcompile("(import [os :refer [mkdir]] sys) [sys mkdir]")
3584+
)
3585+
35753586
def test_nested_imports_visible_with_parent(self, lcompile: CompileFn):
35763587
import collections.abc
35773588

0 commit comments

Comments
 (0)