Skip to content

Commit ee4c88c

Browse files
committed
refactor: simplify nested path error handling and improve docs
- Remove redundant pass statements from _check_nested_paths() - Simplify error message logic using is_relative_to() instead of nested try/except - Add examples to docstring for better clarity - Improve inline comments for better code readability Addresses Claude bot review feedback. Signed-off-by: phernandez <paul@basicmachines.co>
1 parent b74855c commit ee4c88c

1 file changed

Lines changed: 17 additions & 25 deletions

File tree

src/basic_memory/services/project_service.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ def _check_nested_paths(self, path1: str, path2: str) -> bool:
9797
9898
Returns:
9999
True if one path is nested within the other, False otherwise
100+
101+
Examples:
102+
_check_nested_paths("/foo", "/foo/bar") # True (child under parent)
103+
_check_nested_paths("/foo/bar", "/foo") # True (parent over child)
104+
_check_nested_paths("/foo", "/bar") # False (siblings)
100105
"""
101106
# Normalize paths to ensure proper comparison
102107
p1 = Path(path1).resolve()
@@ -108,16 +113,14 @@ def _check_nested_paths(self, path1: str, path2: str) -> bool:
108113
p2.relative_to(p1)
109114
return True
110115
except ValueError:
111-
pass
112-
113-
try:
114-
# Check if p1 is under p2
115-
p1.relative_to(p2)
116-
return True
117-
except ValueError:
118-
pass
119-
120-
return False
116+
# Not nested in this direction, check the other
117+
try:
118+
# Check if p1 is under p2
119+
p1.relative_to(p2)
120+
return True
121+
except ValueError:
122+
# Not nested in either direction
123+
return False
121124

122125
async def add_project(self, name: str, path: str, set_default: bool = False) -> None:
123126
"""Add a new project to the configuration and database.
@@ -177,35 +180,24 @@ async def add_project(self, name: str, path: str, set_default: bool = False) ->
177180
existing_projects = await self.list_projects()
178181
for existing in existing_projects:
179182
if self._check_nested_paths(resolved_path, existing.path):
180-
# Determine which path is nested within which
183+
# Determine which path is nested within which for appropriate error message
181184
p_new = Path(resolved_path).resolve()
182185
p_existing = Path(existing.path).resolve()
183186

184-
try:
185-
p_new.relative_to(p_existing)
186-
# New path is nested under existing project
187+
# Check if new path is nested under existing project
188+
if p_new.is_relative_to(p_existing):
187189
raise ValueError(
188190
f"Cannot create project at '{resolved_path}': "
189191
f"path is nested within existing project '{existing.name}' at '{existing.path}'. "
190192
f"Projects cannot share directory trees."
191193
)
192-
except ValueError as e:
193-
# Re-raise if it's the error we just raised, otherwise try the other direction
194-
if "Cannot create project" in str(e):
195-
raise
196-
197-
try:
198-
p_existing.relative_to(p_new)
194+
else:
199195
# Existing project is nested under new path
200196
raise ValueError(
201197
f"Cannot create project at '{resolved_path}': "
202198
f"existing project '{existing.name}' at '{existing.path}' is nested within this path. "
203199
f"Projects cannot share directory trees."
204200
)
205-
except ValueError as e:
206-
# Re-raise if it's the error we just raised
207-
if "Cannot create project" in str(e):
208-
raise
209201

210202
# First add to config file (this will validate the project doesn't exist)
211203
project_config = self.config_manager.add_project(name, resolved_path)

0 commit comments

Comments
 (0)