Skip to content
Merged
4 changes: 2 additions & 2 deletions src/pyob/core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ def _win32_input(self, start_time: float, timeout: int) -> str:
time.sleep(0.1)

def _unix_input(self, start_time: float, timeout: int) -> str:
import termios
import tty
import termios # type: ignore
import tty # type: ignore

input_str: str = ""
fd = sys.stdin.fileno()
Expand Down
5 changes: 3 additions & 2 deletions src/pyob/entrance.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,12 @@ def update_ledger_for_file(self, rel_path: str, code: str):
elif isinstance(n.func, ast.Attribute):
potential_refs.add(n.func.attr)
elif isinstance(n, ast.Name) and isinstance(n.ctx, ast.Load):
potential_refs.add(n.id)
if n.id not in ["self", "None", "True", "False"]:
potential_refs.add(n.id)
except Exception as e:
logger.warning(f"Failed to parse Python AST for {rel_path}: {e}")
potential_refs.update(
re.findall(r"\b[a-zA-Z_][a-zA-Z0-9_]{3,}\b", code)
re.findall(r"\b[a-zA-Z_][a-zA-Z0-9_]{4,}\b", code)
)
elif ext in [".js", ".ts"]:
defs = re.findall(
Expand Down
12 changes: 7 additions & 5 deletions src/pyob/pyob_code_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,29 @@ def visit_Import(self, node: ast.Import) -> None:
except Exception:
pass

def visit_ImportFrom(self, node):
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
try:
self.imports.append(ast.unparse(node))
except Exception:
pass

def visit_ClassDef(self, node):
def visit_ClassDef(self, node: ast.ClassDef) -> None:
self.classes.append(f"class {node.name}")
old_class = self.current_class
self.current_class = node.name
for child in node.body:
self.visit(child)
self.current_class = old_class

def visit_FunctionDef(self, node):
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
self.handle_function(node)

def visit_AsyncFunctionDef(self, node):
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
self.handle_function(node)

def handle_function(self, node):
def handle_function(
self, node: ast.FunctionDef | ast.AsyncFunctionDef
) -> None:
args = []
for arg in node.args.args:
if self.current_class and arg.arg == "self":
Expand Down
13 changes: 3 additions & 10 deletions src/pyob/xml_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,13 @@ def _attempt_line_by_line_match(
code_lines = source.splitlines()
for i in range(len(code_lines) - len(search_lines) + 1):
match = True
for j, sline in enumerate(search_lines_stripped):
if sline not in code_lines[i + j].strip():
for j, sline in enumerate(search_lines):
if sline.strip() not in code_lines[i + j].strip():
match = False
break
if match:
# Apply indentation fix to replacement lines
fixed_replace = self._fix_replace_indentation(
"\n".join(code_lines[i : i + len(search_lines)]),
"\n".join(replace_lines),
)
new_code_lines = (
code_lines[:i]
+ fixed_replace.splitlines()
+ code_lines[i + len(search_lines) :]
code_lines[:i] + replace_lines + code_lines[i + len(search_lines) :]
)
new_code = "\n".join(new_code_lines)
if not new_code.endswith("\n") and source.endswith("\n"):
Expand Down
Loading