Name a discovered module for where it sits under the synced directory
`fluksio sync dev` refused a one-file-per-directory layout: s1..s4 each holding `study.py` stopped the walk with "two files would both import as 'study'". The suggested `__init__.py` per study directory fixed discovery and broke the bare `from study import ...` a test beside it does. A file is now named for its path under the directory being synced — `dev/s1/study.py` imports as `s1.study`, the directories between being namespace packages — so nothing collides and nothing has to be added. A file at the root keeps its bare name, and the generated shim follows: `import_root` walks one directory per dotted segment and lands on the same root. The collision message stays for the spelling that can still reach it: two files named on the command line, each rooted at its own directory. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019Hra4ndWMCLU5F3KjUuVAc
This commit is contained in:
@@ -136,8 +136,8 @@ def _import(root: str, dotted: str, expect: Path | None = None) -> None:
|
||||
f" {actual}\n {expect}\n"
|
||||
"Python keeps one module per name, and a node's generated body "
|
||||
"imports by that name, so the second would run the first's code. "
|
||||
"Put an `__init__.py` in each directory — they become "
|
||||
f"'<dir>.{dotted}' and stop colliding — or rename one of the files."
|
||||
"Sync the directory they are both under — each then imports as "
|
||||
"'<dir>.<file>' — or rename one of the files."
|
||||
)
|
||||
|
||||
|
||||
@@ -211,7 +211,14 @@ def discover(targets: list[str], keep_going: bool = False) -> list[Flow]:
|
||||
if entry.is_dir():
|
||||
_import_package(entry)
|
||||
else:
|
||||
_import(*_module_of(entry), entry)
|
||||
# Named for where it sits under the directory being synced,
|
||||
# so `dev/s1/study.py` imports as `s1.study` and one
|
||||
# `study.py` per study collides with nothing. No
|
||||
# `__init__.py` needed: the directories in between are
|
||||
# namespace packages. A file at the root keeps its bare
|
||||
# name, which is what it has always had.
|
||||
dotted = ".".join(entry.relative_to(path).with_suffix("").parts)
|
||||
_import(str(path), dotted, entry)
|
||||
except SyncError:
|
||||
# A name collision is never somebody else's problem: it would
|
||||
# put the wrong file behind a node.
|
||||
|
||||
Reference in New Issue
Block a user