Skip to content

Commit

Permalink
- changed to always convert relative imports to absolute, as GPT
Browse files Browse the repository at this point in the history
  sometimes repeats relative includes seen in modules verbatim in
  tests, where they aren't valid;

- fixed some broken tests;
  • Loading branch information
jaltmayerpizzorno committed Sep 4, 2024
1 parent bc6094f commit 93c3e01
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 182 deletions.
6 changes: 6 additions & 0 deletions src/coverup/codeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ def get_imports(n: ast.AST):
if alias_name in names:
if not new_imp:
new_imp = copy.copy(imp)
if isinstance(new_imp, ast.ImportFrom):
# make all imports absolute, as GPT, seeing relative imports in
# modules, may want to repeat them in tests as well
new_imp.module = _resolve_from_import(module.path, new_imp)
new_imp.level = 0

new_imp.names = []
imports.append(new_imp)
new_imp.names.append(alias)
Expand Down
5 changes: 2 additions & 3 deletions src/coverup/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from .utils import *
import ast
from .codeinfo import get_global_imports
from .codeinfo import get_global_imports, parse_file


class CodeSegment:
Expand Down Expand Up @@ -89,8 +89,7 @@ def find_enclosing(root, line):
return (node, begin, node.end_lineno+1) # +1 for range() style

for fname, fcov in coverage['files'].items():
with open(fname, "r") as src:
tree = ast.parse(src.read(), fname)
tree = parse_file(Path(fname))

missing_lines = set(fcov['missing_lines'])
executed_lines = set(fcov['executed_lines'])
Expand Down
46 changes: 42 additions & 4 deletions tests/test_codeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,46 @@ def func(x: C):
)


def test_get_global_imports():
code = textwrap.dedent("""\
def test_get_info_included_imports_always_absolute(import_fixture):
tmp_path = import_fixture
code = tmp_path / "code.py"
code.write_text(textwrap.dedent("""\
import foo
"""
))

(tmp_path / "foo").mkdir()
(tmp_path / "foo" / "__init__.py").write_text(textwrap.dedent("""\
import sys
from .bar import Bar
class Foo(Bar):
pass
"""
))

(tmp_path / "foo" / "bar.py").write_text(textwrap.dedent("""\
class Bar:
pass
"""
))

tree = codeinfo.parse_file(code)
assert codeinfo.get_info(tree, 'foo.Foo') == textwrap.dedent("""\
in foo/__init__.py:
```python
from foo.bar import Bar
class Foo(Bar):
pass
```"""
)


def test_get_global_imports(import_fixture):
tmp_path = import_fixture
code = tmp_path / "code.py"
code.write_text(textwrap.dedent("""\
import a, b
from c import d as e
import os
Expand All @@ -1029,7 +1067,7 @@ class Foo:
def f():
if os.path.exists("foo"):
sha1(a.x, b.x, e.x)
""")
"""))

print(code)

Expand All @@ -1038,7 +1076,7 @@ def find_node(tree, name):
if isinstance(n, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)) and n.name == name:
return n

tree = ast.parse(code)
tree = codeinfo.parse_file(code)
# print(ast.dump(tree, indent=2))

f = find_node(tree, 'f')
Expand Down
60 changes: 0 additions & 60 deletions tests/test_coverup_22.py

This file was deleted.

Loading

0 comments on commit 93c3e01

Please sign in to comment.