Skip to content

Commit

Permalink
Do some coverage omission by file.
Browse files Browse the repository at this point in the history
- Reorganize defer_imports.console slightly.
- Rename ci to "CI".
  • Loading branch information
Sachaa-Thanasius committed Sep 8, 2024
1 parent 5b27bc8 commit 1ba4116
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ci
name: CI

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ defer_imports = ["src"]
[tool.coverage.run]
plugins = ["covdefaults"]
source = ["defer_imports", "tests"]
omit = ["src/defer_imports/_typing.py", "src/defer_imports/console.py"]

[tool.coverage.report]
# It's a work in progress.
fail_under = 90


Expand Down
2 changes: 1 addition & 1 deletion src/defer_imports/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def final(f: object) -> object:
return f


def __getattr__(name: str) -> object: # pragma: no cover # noqa: PLR0911, PLR0912
def __getattr__(name: str) -> object: # noqa: PLR0911, PLR0912
# Let's cache the return values in the global namespace to avoid subsequent calls to __getattr__ if possible.

# ---- Pure imports
Expand Down
32 changes: 16 additions & 16 deletions src/defer_imports/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
__all__ = ("DeferredInteractiveConsole", "interact", "instrument_ipython")


class DeferredCompile(codeop.Compile):
class _DeferredCompile(codeop.Compile):
"""A subclass of codeop.Compile that alters the compilation process with defer_imports's AST transformer."""

def __call__(self, source: str, filename: str, symbol: str, **kwargs: object) -> _tp.CodeType:
Expand All @@ -27,11 +27,11 @@ def __call__(self, source: str, filename: str, symbol: str, **kwargs: object) ->
flags &= ~codeop.PyCF_ALLOW_INCOMPLETE_INPUT # pyright: ignore
assert isinstance(flags, int)

og_ast_node = compile(source, filename, symbol, flags | ast.PyCF_ONLY_AST, True)
orig_ast = compile(source, filename, symbol, flags | ast.PyCF_ONLY_AST, True)
transformer = DeferredInstrumenter(source, filename, "utf-8")
new_ast_node = ast.fix_missing_locations(transformer.visit(og_ast_node))
new_ast = ast.fix_missing_locations(transformer.visit(orig_ast))

codeob = compile(new_ast_node, filename, symbol, flags, True)
codeob = compile(new_ast, filename, symbol, flags, True)
for feature in _features:
if codeob.co_flags & feature.compiler_flag:
self.flags |= feature.compiler_flag
Expand All @@ -54,8 +54,7 @@ def __init__(self) -> None:
"@DeferredImportProxy": DeferredImportProxy,
}
super().__init__(local_ns)
self.compile = codeop.CommandCompiler()
self.compile.compiler = DeferredCompile()
self.compile.compiler = _DeferredCompile()


def interact() -> None:
Expand All @@ -67,6 +66,16 @@ def interact() -> None:
DeferredInteractiveConsole().interact()


class _DeferredIPythonInstrumenter(ast.NodeTransformer):
def __init__(self):
self.actual_transformer = DeferredInstrumenter("", "<unknown>", "utf-8")

def visit(self, node: ast.AST) -> _tp.Any:
self.actual_transformer.data = node
self.actual_transformer.scope_depth = 0
return ast.fix_missing_locations(self.actual_transformer.visit(node))


def instrument_ipython() -> None:
"""Add defer_import's compile-time AST transformer to a currently running IPython environment.
Expand All @@ -79,13 +88,4 @@ def instrument_ipython() -> None:
msg = "Not currently in an IPython/Jupyter environment."
raise RuntimeError(msg) from None

class DeferredIPythonInstrumenter(ast.NodeTransformer):
def __init__(self):
self.actual_transformer = DeferredInstrumenter("", "<unknown>", "utf-8")

def visit(self, node: ast.AST) -> _tp.Any:
self.actual_transformer.data = node
self.actual_transformer.scope_depth = 0
return ast.fix_missing_locations(self.actual_transformer.visit(node))

ipython_shell.ast_transformers.append(DeferredIPythonInstrumenter())
ipython_shell.ast_transformers.append(_DeferredIPythonInstrumenter())
2 changes: 1 addition & 1 deletion tests/test_deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def access_module_attr() -> object:


@pytest.mark.skip(reason="Leaking patch problem is currently out of scope.")
def test_leaking_patch(tmp_path: Path):
def test_leaking_patch(tmp_path: Path): # pragma: no cover
"""Test a synthetic package that demonstrates the "leaking patch" problem.
Source: https://github.com/bswck/slothy/tree/bd0828a8dd9af63ca5c85340a70a14a76a6b714f/tests/leaking_patch
Expand Down

0 comments on commit 1ba4116

Please sign in to comment.